What's wrong with this copy of an xaml+cs file? - c#

I've made a copy of an xaml+cs file and I get a lot of errors.. all or most of them have the same message. I've made a printscreen in order to explain the problem better.
I think it's about some elements having the same name and this needs to be changed.. as far as I understand.
Can you help me solve this?
EDIT:
xaml code:
x:Class="KinectInfoPortal.ImagesGridCopy"
cs code:
public partial class ImagesGridCopy : Window
EDIT2:
cs code:
public partial class ImagesGrid : UserControl
xaml code:
x:Class="Microsoft.Samples.Kinect.ControlsBasics.ImagesGrid"

You should rename your copy's class to something else. It's named ImagesGrid which already exists in ImagesGrid.xaml / ImagesGrid.cs

The problem is that you have to change the name of the class inside the xaml and the cs file. Currently, the both pack of files (xaml+cs) got the same class definition.
So, the members and their events are defined twice.
In order to fix it you have to rename the class inside the copied files.
Xaml:
<Window x:Class="KinectInfoPortal.ImagesGrid"
Cs:
public partial class ImagesGrid : Window

You have to replace every occurrence of ImagesGrid in the 2 new files with cat1, otherwise you got duplicate class names.

Related

Error `script class cannot be found` when trying to add a camera change component

this pops up when I try to add the script to the gameobject like in the tuturial:
Can't add script component 'CameraChange' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.
How do I fix this? Here is the tuturial that I am following:
https://www.youtube.com/watch?v=nR5P7AH4aHE&t=106s&ab_channel=JimmyVegas
First, make sure that there are no compiling errors.
Second, this error is most common when class name inside .cs file is not matching file name. Make sure class name perfectly matches file name, case-sensitive. Or class is not inherited from Component or MonoBehaviour.
Correct minimal file content for CameraChange.cs would be:
using UnityEngine;
public class CameraChange : MonoBehaviour
{
...
}
thank you it was changing the monobehavior to camera change

Binding To Class Data Without Namespace

Scenario
I have a class which looks something like this:
class GlobalAssemblyInfo
{
public const string AssemblyName = "MyAppName";
}
The class is NOT contained in a namespace.
I tried unsuccessfully to bind to this using the following code:
Text="{Binding GlobalAssemblyInfo.AssemblyName}"
Question
Is this possible, and if so, how would I accomplish this?
Why I Need This
Before I get any comments about only using bindings for dynamically changing content, let me explain why I need this.
I am creating an app which may need to undergo a rename due to trademark issues. I need to use the name in several places, such as "MyApp Contributors" or "MyApp Help." Using a binding would enable me to say merely Text="{Binding AppName, StringFormat='{}{0} Contributors'}".
You have two problems here. The first is that the way you're using the class in the binding is assigning it to the Path so it is looking for a property named GlobalAssemblyInfo on whatever your DataContext is. You instead need to use a static source and also expose the value as a property instead of a const. This uses the form:
{Binding Source={x:Static ns:Class.StaticProperty}}
The other problem is that you have no namespace to create your xmlns from in XAML. You should consider if you really need the class to not be namespaced but if you do you should be able to use the weird construction of
xmlns:myGlobal="clr-namespace:;assembly="
where you would then use myGlobal in the place of ns in the first example.
If you created a class that has no namespace, why don't you just put your assembly name in Resources in XAML or Resources in project so you can do binding in it with StringFormat
In your App.xaml.
Define something like this
<x:String x:Key="AssemblyName">Your Value</x:String>
or add a new resource file in your project and define it there.
So you can use it something like this
Text="{Binding Source={StaticResource AssemblyName}, Path=., StringFormat="{}}"

Use separate cs file WPF

I know a little bit in C# with Winforms, but I am currently working with WPF for the first time.
There's the problem : I created a class file named ConnectDB.cs, who contains a class named ConnectDB with a few methods in it, used to connect to a Database.
In my MainWindow.xaml.cs I need to appeal this class, but it doesn't work ! The compiler says "The name 'ConnectDB' does not exist in the current context". But when I add my ConnectDB class directly in the MainWindow.cs it runs smoothly !
So, basically, my question is : how to use a class from another cs file in WPF ?
(I already tested the same thing with Winforms and it works, so I'm a little bit lost)
if you can not call your class then I think about that :
1* Check if you have the same namespaces
2* Check if your class is public
3* if all Ok then try to Clean and rebuild your solution

Visual Studio 2010 keeps changing my winforms control

I have an odd situation with a user control in VS 2010. The form designer keeps changing my entry and then telling me it does not exist! It compiles and runs the first time, and then if I change something unrelated, it gives me an error in the designer.cs file (Cannot resolve symbol SomeEntry).
private SomeEntry someEntry;
// ...
this.someEntry = new **MyNameSpace**.SomeEntry();
if I remove the MyNameSpace.
this.someEntry = new SomeEntry();
it works, until I make a change again. If I look at the class when the mouse is over the changed designer file, SomeEntry shows SomeEntry.SomeEntry()
instead of MyNameSpace.SomeEntry()
Basically, the definition is something like this:
namespace MyNameSpace
{
public partial class SomeEntry : FormValidatingUserControl
{
public SomeEntry()
{
InitializeComponent();
}
}
}
So, what do I do?
It turns out that I should have posted my code EXACTLY as it appeared, as I am sure someone would have figured this out for me sooner.
The issue was that I was trying to simplify for the post, and changed the names of MyNameSpace and SomeEntry to keep it generic.
The project is something the namespace is something (so far, normal) and there was a generated class something from the entity framework.
To reproduce the problem, I created a new project called WinFormTestX. So, the solution is WinFormTestX and the project WinFormTestX. I added a class called WinFormTestX, but did nothing with it.
namespace WinFormTestX
{
public class WinFormTestX
{
public int ID { get; set; }
}
}
Now, I create a UserControl (UserControl1) and drop a simple button on it. Compile it, and the toolbox adds this control, as expected. Now, I drop it on Form1 and compile it and get an error:
Error 1 The type name 'UserControl1'
does not exist in the type
'WinFormTestX.WinFormTestX' D:\Data\Projects\Temp\WinFormTestX\Form1.Designer.cs 31 51 WinFormTestX
As soon as I right click on the class WinFormTestX and Exclude From Project (or delete it), everything works.
So, just having the class in the project of the same name as the namespace, even when it is not used, causes an issue. Obviously, now that I know the cause, there is a simple way around this. But, is this something that is a "bug" that should be submitted to Microsoft?
I've had stuff like this happen when manually moving a class into a new namespace. If you're not careful, your class can become defined multiple times. For example, there may be a residual designer .cs file that still defines the class in its old namespace or some other .cs/.designer.cs mismatch. In other words, your assembly may contain both a SomeEntry.SomeEntry and MyNameSpace.SomeEntry class.
Check the Visual Studio object browser to see if you have SomeEntry defined in multiple namespaces.

Adding a custom namespace to XAML

I am trying to add my own namespace to my xaml file in order to use my own class easily -I guess the reason is this-
I wrote the following code in window tag for this:
xmlns:myns="clr-namespace:LibNameSpace"
Where my window tag also starts with the following definition:
<Window x:Class="LibNameSpace.MainWindow"
I want to use the LibNameSpace:Class1 class, and I was hoping to write myns:Class1 for this. However, that command causes this error:
Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'LibNameSpace' that is not included in the assembly.
How can I fix this?
The name LibNameSpace sounds like its a library in another assembly. If this is the case, you must add the name of the assembly:
xmlns:myns="clr-namespace:LibNameSpace;assembly=MyLibAssembly
Update:
The name of the assembly can be found in project-explorer in the properties-screen of the project (of the library-assembly). In general also the file-name of the dll without the dll-suffix represents the assembly name.
Because for me it's not really clear what you want to do, here another try:
If MyLibAssembly is the main namespace of your application and there in you have a Window named MainWindow and a class named Class1 that you want to instantiate in your MainWindow-class:
Make sure, that in Class1 is no
error, the project must
compile without errors. Remove first the
namespace-declaration from the xaml and compile your
project till you have no compilation errors.
Make sure that Class1 is public and
has a paramterless constructor
Make sure that in the code behind
your MainWindow is also in the
MyLibAssembly-namcespace.
Add then the namspace-declaration
xmlns:local="clr-namespace:LibNameSpace
into your xaml. local is generally
used to declare the same namespace as your current element, in your case the window, is in.
Insert your Class1 with the
<local:Class1/> -tag in the xaml. If Class1 does not derive from FrameworkElement or a higher level control, you must add it into the resources-section of your window. If this is true, give it a key. <local:Class1 x:Key="KeyToYourClass"/>
Maybe vs is out of sync. Click in the solution-explorer on the root-node Clean Solution and then Rebuild Solution. Maybe that helps.
I hope this helped. If not, try to reformat your question (use the code-symbol to make the question more readable and try to rephrase to make more clear what your desire is).
Use Intellisense. In my case one space mattered. instead of
xmlns:local="clr-namespace:DataAccess;assembly=DataAccess"
I hand typed
xmlns:local="clr-namespace:DataAccess; assembly=DataAccess"
Notice the space after ';'. This made the difference. So use visual studio Intellisense and it will render you correct xaml markup.
I found this answer while I was struggling with problems in Windows 8. I was trying to use a User Control and I had several errors. The last ones where:
Error 9 Cannot add 'ScrollControl' into the collection property 'Children', type must be 'UIElement'
and:
Error 10 Unknown type 'ScrollControl' in XML namespace 'clr-namespace:EventTests.Controls;assembly=EventTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
ScrollControl is my user control.
I ended up replacing this:
xmlns:Controls="clr-namespace:EventTests.Controls"
For this:
xmlns:Controls="using:EventTests.Controls"
I hope this saves the time I spent with this issue.

Categories

Resources