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"
Related
I have written a dll in C# which has five .cs files. ControlsOnForm.cs has a public enum defined in it.
public enum FormControls {
Button,
Label,
DataGrid,
TextBox
}
Now, I'm using this dll in a Windows app which is shown in attached Image1 and when I click Generate button it generates the ControlsOnForm.cs C# file which is same as the enum in dll.
Now how do I refer/use this dynamically generated C# file with enum values in dll.
Or in another words the enum values in ControlsOnForm.cs of dll should get replaced with the newly generated C# file's enum values.
Thanks,
Although it is trivial to get your code to generate a cs file. I suspect what you then want to do is run the resulting code. That step is highly difficult and there are many things you need to understand in terms of the limitation.
However as another user has commented, it sounds like what you want to change is data, and not code.
You should understand the distinction between hardware, firmware, code, configuration and data. The lines are much more blurred than you might first think. But at the end of the day, each is a step in a continuum of changeability. For your purpose, code should be the thing that changes the least often. This in our tool chain it is the hardest to change. Remember at the end of the day everything is ones and zeros... and your data should also change how your program works.
Enums are a collection of named constants. In the same sense that you cannot change a constant, you cannot change an enum (at least without jumping through a ton of hoops).
What you really want to do here is use an external dataset, be it a datatable, an XML configuration file, etc.
On a side note, you would probably want to create an enum outside of a form, but in the same namespace. You can add a module to a project and drop all your enums in there.
From MSDN:
The enum keyword is used to declare an enumeration, a distinct type
that consists of a set of named constants called the enumerator
list.
Usually it is best to define an enum directly within a namespace so
that all classes in the namespace can access it with equal
convenience. However, an enum can also be nested within a class or
struct.
Reference: MSDN Enum Entry
I have a class let's say it's called "Apples". Let's say that class is in a .cs file in a folder called "Apples"
Project
-Apples
-Apples.cs
-main.cs
How can i reference it without writing this:
Main.cs
Apples.Apples.testVar = 2;
and just write
Main.cs:
Apples.testVar = 2;
Is there a way to do this, or will i have to put up with it or change the folder name?
put this at the top of your file -
using Apples = Apples.Apples;
If you have a folder in visual studio and create a class in it, Visual Studio (and possibly other IDEs) will automatically append the folder's name to the namespace of any file created from that folder.
That DOES NOT mean in any way that the namespace has to stay that way. The "folder" is purely for organizational purposes and plays no part in the compilation of your code. The filename doesn't matter to the actual code either. If you look in your Apples.cs just change:
namespace Apples.Apples
{
//....
to
namespace Apples
{
//...
It's simply the rule that your IDE is using as an assumption to what you want. You're not required to follow it.
EDIT: At least that's what I assume to be the misunderstanding here as you're focusing on the folder name - which means nothing to the code and only matters to you. Additionally as previously mentioned you might be hung up on ambiguity between namespace name and class name. As mentioned in other answers it's a bad idea to name a class the same as it's namespace.
By default the global namespace for your project will be the same name as your project. So if you named your project "Apples" and then made a folder called "Apples" and created a class within that folder called "Apples" - You'd then navigate to your class by following the chain of namespaces:
Apples.Apples.Apples
You're drawing the conclusion that you have to do this based on the folder/file names but it's really the namespaces/classes. You have several options here but the bottom line is in order to remove confusion and mess 2 or all 3 of those "Apples" need to change. Changing the file name or folder name will not work. you have to change the class name and at least one of the namespace names. Something more appropriate:
AppleProject.Apples.AppleBase
or even as simple as:
Apples.Apple // removed the extra namespace in the middle
You could alias "Apples" in your using statements:
using Apples = Apples.Apples.testVar;
Or whatever you'd like to make it easier to read.
Yes if you are using C# you just add it in the using references up top of your code file you are on:
EG:
using Apples.Apples;
You just need to ensure you have the reference to the project set if it is not in your current project.
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.
In my persistence layer, I've declared a load of Enums to represent tables containing reference data (i.e. data never changes).
In Linq2SQL, I am able to set the type of an entity property to an enum type and all is well, but as soon as I set a second entity's property to use the same enum type, the Code Generator (MSLinqToSQLGenerator) start generating an empty code file.
I assume that MSLinqToSQLGenerator is quietly crashing. The question is why, and are there any work-arounds? Anyone else experienced this problem?
Is your enum by any chance in a file named the same as the dbml? There is a bug in 3.5 (fixed in 4.0) where conflicts cause an empty file. Oddly, usually moving the using directives (and right-click; run custom tool) fixes it.
So if you have "foo.dbml" and your own "foo.cs" (in the same folder) with:
using System;
namespace MyNamespace {
}
it will break (generate an empty foo.designer.cs). If you have:
namespace MyNamespace {
using System;
}
it will work. I'm not kidding. Likewise, renaming "foo.cs" to "bar.cs" (and right-click, run custom tool) will fix it.
Oddly, I've discovered that this behavior only occured with an Enum named "GrantType". As soon as changed the name of the enum, the generator started working again.
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