We have a structure roughly as below:
namespace a.b.c.d.e {
Master file
namespace f {
Page file
Enum Colours
Enum Fruits
}
}
The master file is made up of 3 files: .Master, .Master.cs & .Master.designer.cs.
Several functions within Page refer to Colours or Fruits. Within the .Master.cs file I have using a.b.c.d.e.f - this should mean that I can reference Colours & Fruits without fully referencing them. This works as expected in the .Master.cs file - however putting <%=Page.GetTitle(Colours.Red)%> in the .Master file does not work. It just says "The name 'Colours' does not exist in the current context". Instead I have to put <%=Page.GetTitle(a.b.c.d.e.f.Colours.Red)%>.
Where am I going wrong? If Colours.Red works fine in the .Master.cs file, surely it should work in the .Master file?
As per this question you can move the enum outside the namespace so you don't have to reference it in your code. Otherwise, you need to keep using the full qualified name of the enum.
Related
In ASP.NET Core MVC we can put a file with the exact name of _ViewStart.cshtml inside a folder to contain the common C# code to be run before every razor view/page in that folder. Something like this:
#{
const string SomeConstant = "some value";
}
Similarly a file with the exact name of _ViewImports.cshtml inside a folder can hold all the common razor directives to be shared among the razor views/pages in that folder. Like this:
#layout _Layout
#using MyApp.Models
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
But here's a question that I couldn't google, no matter how I rephrased it:
Can somebody please explain to me why we have both a _ViewStart.cshtml and a _ViewImports.cshtml to define common code & directives? Why aren't these functionalities (which don't seem to be conflicting with each-other) defined in a single file?
The _ViewStart file
It is used to set up shared-memory (public static variables) across all view files.
For example, the common practice for ViewStart is to set up a default value that you can override for the Layout and the ViewData / ViewBag dictionary.
The _ViewImports file
In this file you can summarize (abstract) all using statements that you commonly use in all your views.
Why to use _ViewImports file for common "using directives" instead of ViewStart?
Because using directives has the scope of the body of the current view file. So, putting #using statements inside ViewStart file won't make them available for any other view file except the body of the viewStart file itself. Therefore, comes the special ViewImports file which is designed to serve this scope extension purpose of the #using statements and other useful things, such as the tag helper, which without this special file, would be repeated inside each view file which violates the DRY (Don't Repeat Yourself) Principle.
One thing that has been overlooked in the other answers is that according to the official documentation:
Code in the _ViewStart.cshtml file will only be run for non-layout pages.
Code in the _ViewImports.cshtml file will be run for both layout and non-layout pages.
I've tested this by moving the default Application Insights JavaScript snippet (the code below) from the imports file to the start file and it causing a build error on my layout page as it can no longer find the defined variable JavaScriptSnippet.
The code I moved:
#inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet
Given this, the difference between the files is probably 'code I want to run everywhere' vs 'code I want to only run for full views', similar to the difference between .bashrc and .bash_profile.
Code that needs to be executed before each page should be placed _ViewStart.cshtml file.
For _ViewImport.cshtml - the contents of this file applied to all the files present in the same folder and subfolder.
So _ViewStart is execution whereas _ViewImport applies its content to each file.
TEST1
Placing both "Layout [Correct]" reference and "using statement[Incorrect]" at _ViewStart will give compiler Error.
TEST2
Placing both "Layout [InCorrect]" reference and "using statement[Correct]" at _ViewImport will not apply _Layout to other pages
As per MSDN ViewImport Support following directives
#addTagHelper, #removeTagHelper: all run, in order.
#tagHelperPrefix: the closest one to the view overrides any others
#model: the closest one to the view overrides any others
#inherits: the closest one to the view overrides any others
#using: all are included; duplicates are ignored
#inject: for each property, the closest one to the view overrides any others with the same property name
I followed the instructions in Put strings into resource files, instead of putting them directly in code or markup in Put UI strings into resources (except I don't understand step 4f). The structure in the Solution Explorer of the resources in my project is:
That is the hierarchy for the project's shared node. I opened the Resources1.resw file and added a couple of strings.
Then Add string resource identifiers to code and markup in that article has the following:
var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
When I have that I get:
WinRT information: ResourceMap Not Found.
I have tried many other possibilities that I have found from searching but either I get that error or the class or method (in other solutions) do not exist for my project. I assume there is something relevant missing from that article.
Using C#, how do I get strings from that resources file ?
You dont need to rename your resource file. If the name of the resource file isnt the default (Resources.resw), you can add the special name in the GetForCurrentView method.
In your case the call should be:
var loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView("Resources1.resw");
Source:GetForCurrentView(System.String)
Did you tried to specify default language in app.xaml for your actual frame?
myFrame = new Frame
{
Language = Windows.Globalization.ApplicationLanguages.Languages[0]
};
Or more specific:
Windows.Globalization.ApplicationLanguages.Languages['en']
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 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 have an ASP.net MVC 3 project with resource files setup in folders like:
/Resources/EntityName/Views/
/Resources/EntityName/Models/
This means the namespace to access the strongly typed resource values is:
Resources.EntityName.Models.ModelA.Property1
Visual studio gives a compile time error if I try to include the namespace "resources.xxx" and it won't allow the using alias syntax either.
Is there anyway to include or atleast alias the strongly typed namespace of a resource file like it was a normal namespace?
There should be no reason why you can't use one of these objects within a class:
using YourProject.Resources.EntityName.Models;
You might try something like this (not tested, just an idea)
public class myModelA : Resources.EntityName.Models.ModelA
{ /*Leave empty here, nothing to do*/ }
Then you may be able to call the shortest myModelA instead of the complete, verbose name of the resource. Watch out because you'll not be able to access private members of your original model if you inherit it like thi.