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
Related
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.
Here's the setup I'd like to have for my Windows Phone app, using c# in visual studio 2010:
**MainPage.xaml** contains elements that have an attached property, whose values will be modifiable/savable by the user.
**MainPage.xaml.cs**
first Namespace is PhoneApp ,inside it is a nested namespace called MyNamespace that declares the dependency property. it works(Thanks, Daniel)
**SettingsSample.xaml** that will allow users to change the values of the attached property in MainPage.xaml for any element and automatically save the change.
**AppSettings.cs** a class that exactly reproduces the first listing in this tutorial:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510%28v=vs.105%29.aspx
That page declares the same NameSpace as the MainPage.xaml.cs (PhoneApp), then a public class called AppSettings that is exactly like in the tutorial.
To join everything together, I did:
**MainPage.xaml**
xmlns:local="clr-namespace:PhoneApp.MyNamespace"
I needed this to use the attached property
<phone:PhoneApplicationPage.Resources>
<local:AppSettings x:Key="appSettings"></local:AppSettings>
</phone:PhoneApplicationPage.Resources>
Confusion begins. On the tutorial, they put this on the settings page, but I guess because their settings page is also the one including the elements with the properties that are bound to the saved settings. Mine are on the mainpage, so I put this here. To recap, My settings page will only use methods to change/save these values(and the methods are in AppSettings.cs). Also in the tutorial they add this:
xmlns:local="clr-namespace:SettingsSample"
to the Setting Page(where "SettingsSample" is the Namespace containing declaration/get-Set methods of savable settings) but, for the same reason, I tried to put it on the mainpage, but only one declaration of xmlns:local can be done. I tried several things to put them one after the other, but it doesn't work. This is the key to the two errors I'll list below.
Some elements of mainpage have this, for exemple:
local:MyClass.Son="{Binding Source={StaticResource appSettings}, Path=son1, Mode=TwoWay}" Style="{StaticResource pad}"
"Son" is the attached property
Ok, so I tried different different things but it never worked. The best I could get was in MainPage.xaml that it couldn't create an instance of AppSettings. Now it's different, I have the two errors.
-the type local:AppSettings was not found
-the tag AppSettings does not exist in xml namespace PhoneApp.MyNamespace.
I think this is because I didn't put the
xmlns:local="clr-namespace:PhoneApp"
But I already have
xmlns:local="clr-namespace:PhoneApp.MyNamespace"
and can't put both.(and to me, one is included in the other...) The reason I listed all the ins and out of the situation is because I kind of expect other troubles after I get through this.
I hope this message is clear enough for someone to help me. I spent so much time on it that I begin to loose my mind, so I hope there's no stupid mistake. Of course, I can add any information needed. Thank you for reading anyway!
These are XML namespace mappings. With the following:
xmlns:local="clr-namespace:PhoneApp"
The local part is the XML namespace, whilst PhoneApp is the namespace from your .NET code. With this definition in place you can then reference classes from this namespace in XML as follows:
<local:MyClassInPhoneAppNamespace/>
Because the local part is simply a name, you can change it to whatever you like:
xmlns:fish="clr-namespace:PhoneApp"
And use as follows:
<fish:MyClassInPhoneAppNamespace/>
This should mean that you no longer have collisions.
"local" in this case is simply a friendly name for the namespace you are referencing. It is completely interchangeable.
I was in need to import two local in same file as below
xmlns:local="clr-namespace:Generique.Views.Assets.Entries"
xmlns:local="clr-namespace:Generique.Views.Assets"
I just change the name and it works fine
xmlns:local="clr-namespace:Generique.Views.Assets.Entries"
xmlns:footer="clr-namespace:Generique.Views.Assets"
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.
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.
Right now everything falls in this namespace:
XXX.YYY.(varies)
It's an open source project and I'm refactoring it to suit our needs. That part works fine, but I need to add another namespace after YYY for organizational reasons. So every single class will read XXX.YYY.ZZZ.(varies) How can I do that?
In your code file, just change it:
namespace XXX.YYY.ZZZ
{
/* your types that go in that namespace */
}
(typically still only one class / etc per file)
If you are moving all of them, then find+replace may help (ctrl+h)
You should also be able to use the "class view" to help track them: View => Class View, or ctrl+w,c