Cannot find namespace - silverlight

I found this article when challenged with a problem in one of my applications. I understand the usage, but cannot find the namespace for the local element where we find local:SetterValueBindingHelper.PropertyBinding. So what namespace does this reference? Google has not found the class either, nor MSDN. Thanks for the help!

Local just means an alias for a namespace in your own (or someone else's) code. E.g. say you took the SettingValueBingingHelper and saved it in your project, in the namespace MyNamespace. To use it in xaml you would reference the namespace by adding to your UserControl (or Page)
xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly"
It does't have to be local - it could be named anything that is relevant to your code, e.g. helpers.
Another example would be using the Triggers from System.Windows.Interactivity - you would reference the namespace:
xmlns:i="clr-namespace:System.Windows.Interactivity;
assembly=System.Windows.Interactivity"
and then use the triggers in your xaml:
<i:Interaction.Triggers>
BTW, the link in your original post has been updated.

Related

WPF markupExtension only works if it's in the same namespace

I'm following a tutorial from here:
http://www.wpftutorial.net/LocalizeMarkupExtension.html
and everything works fine....Untill I devide my code in to different projects
So I have a class (TranslateExtension) that inherits from MarkupExtension. this allows me to add markup like this to WPF:
<TextBlock Text="{Mynamespace:Translate key1}" Margin="8" ></TextBlock>
and the textblock gets filled in with the necessary text
But when i want to put the front end in a different project I get the following exceptions:
The name "Translate" does not exist in the namespace
The type "Translate" was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built
The tag "Translate" does not exist in XML namespace ....
I already have added a reference to the namespace on top of my XAML file and the namespace exists inside of the new project. I also have a reference from my Front end to my old project.
So everything works fine untill I extract the front end in to a different project....
IMPORTANT: If I take TranslateExtensions and Put it in the front end, with everything else in the old project, things magically work...
any ideas?
XAML namespaces starting with clr-namespace: are specific to an assembly. If you don't specify the assembly name, the current assembly is assumed.
The simplest option will be to use an assembly-qualified namespace:
xmlns:MyNamespace="clr-namespace:MyNamespace;assembly=MyAssembly"
For a more robust solution, you might want to look at the XmlnsDefinitionAttribute, which can be used to map multiple CLR namespaces across different assemblies to a single XML namespace URI.

Why are XAML namespaces URIs and what are they for?

I am skeptical regarding the XML namespaces. I don't know why we need these namespaces in all XAML applications.
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I understand the concept of namespaces as in System.Text, System.LINQ, etc. We use that in our classes because we have class libraries that sit on the same computer.
Questions:
It represents some URI. So WPF is standalone application. If the user of the app is not connected to Internet, whats the significance of it? First of all why do we need this?
What's the difference between the first and second line? What does xmlns:x represent?
Also, I see this error upon trying to access that URI
An error occurred while processing your request
What does this mean?
The URLs mentioned in the definitions of the namespaces have nothing to do with actual internet-connection or real existing URLs; this is just a convention used so the namespaces can be properly distinguished.
A XAML-namespace is just like a namespace in C++ or C#/VB.NET, it serves the purpose of logically grouping classes and other items.
In WPF/XAML you have a namespace xmlns=http://... which is the namespace for the controls WPF offers you by default. xmlns:x is a namespace to provide some special values like x:Null to represent a null-value.
You can define your own controls and name them TextBox, too, but you should create an own namespace for these in order to not interfer with the global namespace.
There is a nice article on MSDN about this topic.
It's the way those namespaces were named. That's all. You should not treat them as uri.
As for xmlns vs xmlns:x - the first one is the default namespace, which contains wpf controls, the second one is namespace whcih contains some additional declarations (x:Type, x:Name, etc).
I think the information provided in the accepted answer is not totally correct. The url which you see is not a namespace but a collection of namespaces grouped into one. Below are some of the namespaces from .net which are mapped by default namespace (url)
System.Windows, System.Windows.Automation, System.Windows.Controls, System.Windows.Controls.Primitives, System.Windows.Data, System.Windows.Documents

XAML Resx localization not working as expected

I'm attempting to use a resx file to localize some strings I am using in a XAML file. I've looked around at other documentation on the web, and they all seem to recommend a two part process:
Add a clr-namespace to your window, like this:
xmlns:props="clr-namespace:PJConfiguration.Properties"
Use that namespace to localize your string like this:
Content="{x:Static props:Resources.SharedSettings}"
I've done this, and also made sure that my resource classes are public, but I still get the following error from the XAML in step 2:
Cannot find the type 'Resources'.
Does anyone know what else might be causing this problem? Thanks in advance.
In order to make the Resources visible to XAML, you have to make sure that the code generation mode for the resources is set to public. In VS, you find that setting in a ComboBox near the top of the Resources designer window.
For more information on using .Net resources in XAML, you might want to refer to these blog posts: http://wpfglue.wordpress.com/category/localization/
Check if your .resx file is the default Resources.resx file inside the Properties directory of the Application assembly. If that is, there is no reason XAML couldn't find the public class Resources from the correct namespace under local assembly.
Try to specify the assembly name in Step 2 as recommended in this answer.

Remove an old namespace from a g.cs File?

I previously had a subfolder in my WPF application project called "Controls". It contained a WPF user control. I decided to move that user control to my "Views" folder. Since the original folder was empty, I decided to delete it from the project.
Because the user control and folder is removed I receive a compilation error because the user control used the ProjectName.Folder namespace and now nothing references it. MainWindow.g.cs is what references ProjectName.Controls in a using statement.
I know that *.g.cs are generated by VS and can't be edited because it will be overwritten. What do I do to not allow that namespace to be written to the g.cs file? I tried cleaning my solution/project and rebuilding but nothing has worked.
I had a local reference to the Controls namespace in my Xaml code (MainWindow.xaml). I removed the reference, cleaned the project and produced a successful build.
In your user control file,
In your ClassName.xaml, you must change the namespace as shown below
<UserControl
x:Class="YourOldNamespace.ClassName"
...
...
/>
And in your ClassName.xaml.cs, you must change the namespace as shown below
using System;
using System.Windows;
namespace YourOldNamespace{
public class ClassName{ ....
}
In both the files, you must replace YourOldNamespace to some new namespace as needed.
I have had problems with g.cs files in my projects before too. Since they are auto generated, I tend to just delete the file manually and rebuild.
Dont forget too, that you must check to see if the Build Action property when you click on the affected XAML file is set to PAGE (instead of resource). This is useful to know when you copy a XAML from another project using copy-paste to save time.
Also look at App.xaml and all of your resource dictionaries. For whatever reason, VS 2012's replace in files / "Entire Solution" option didn't find the old namespace reference in App.xaml, had to manually change that. Fixed it for me.
Don't forget to change your Generic.xaml file too,
<ResourceDictionary
xmlns:local="clr-namespace:MyOldNameSpace">
</ResourceDictionary>

WPF Creating an instance of a class in xaml

I have a problem with creating the instance of a class in xaml file. I thought you can do it like this:
<src:MyDataClass x:Key="data"/>
in the resource part of the user control and then use it in the xaml file (for example bind to it).
But even tough the class I created is located in the same namespace it says that:
"The type was not found. Verfiy that all assemblies were built, etc".
How to get it right? is there another method?
Thanks for suggestions.
Well obviously you have to add the right namespace to your xaml. The namespace which contains the class you want instantiate. After reading this, you should be able to solve your problem.

Resources