http://schemas.microsoft.com/winfx/2006/xaml/presentation definition - c#

When you create a new WpfApplication project in Visual Studio you get the following XAML. Copying and pasting the URL http://schemas.microsoft.com/winfx/2006/xaml/presentation into the browser I expected to see the XSD file definition but I get an error. Why?
Thanks.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>

The problem is most of wpf developer knows how it works but when you going to explain, it's become much difficult .. below is my try ... due to simplification it become large but i hope if you read to the end, you will understand how the definition thing works ..
Scenario:
I am a wpf beginner developer and searching for a wpf spinner on goggle. i got a link of font.awesome.wpf .. so i started to trying it. below code is written in document to add the spinner ..
<Window x:Class="DemoFontAwesome.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fa="http://schemas.fontawesome.io/icons/"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="20">
<fa:ImageAwesome Icon="AlignCenter" Spin="False" Height="48" Width="48" />
</Grid>
</Window>
Wow great .... It's working fine !!! ...
Suddenly!! i discover that i added a line there
xmlns:fa="http://schemas.fontawesome.io/icons/"
Not something like
xmlns:fa="clr-namespace:FontAwesome.WPF;assembly=FontAwesome.WPF"
then how visual studio knew which dll contain the ImageAwesome class!!! ... I added only FontAwesome.WPF.dll through nuget ..nothing else i did.. no additional xsd or xml file is there.. The schema link(http://schemas.fontawesome.io/icons/) is not available ...then how??
...Strange!!
However after 1 hours i ended up with below code..
<Window x:Class="DemoFontAwesome.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fa="http://schemas.fontawesome.io/icons/"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<fa:CssClassNameConverter Mode="FromIconToString" x:Key="sdfsdf"></fa:CssClassNameConverter>
</ResourceDictionary>
</Window.Resources>
<Grid Margin="20">
<fa:ImageAwesome Icon="AlignCenter" Spin="False" Height="48" Width="48" />
</Grid>
</Window>
The noticable part is fa:ImageAwesome and fa:CssClassNameConverter classes... They are from different namespace (using code behind i already checked it).. and i did not specified one extra line to specify any of FontAwesome.WPF or FontAwesome.WPF.Convertersnamespace.. then how the magic going on!! ..
Solution:
So i downloaded the source code of font.awesome.wpf .. and started search for the text http://schemas.fontawesome.io/icons/ there ... and finally i found the below lines in assembly.cs of font.awesome.wpf project..
[assembly: AssemblyVersion("4.5.0.*")]
[assembly: AssemblyFileVersion("4.5.0.7")]
[assembly: XmlnsPrefix("http://schemas.fontawesome.io/icons/", "fa")]
[assembly: XmlnsDefinition("http://schemas.fontawesome.io/icons/", "FontAwesome.WPF")]
[assembly: XmlnsDefinition("http://schemas.fontawesome.io/icons/", "FontAwesome.WPF.Converters")]
And the whole thing (the magic trick!!) revealed to me ..
In assembly.cs file the component defined the http://schemas.fontawesome.io/icons/ namespace .. so when i add fontawesome.wpf dll ... visual studio got it's namespace definition using refection .. and so how vs knows where the fa tag refers to ... So this is how it resolved to me... :)
Some theory
XML namespace name doesn’t match any particular .NET namespace. There are a
couple of reasons the creators of XAML chose this design. By convention, XML namespaces are often
uniform resource identifiers (URIs) as they are here. These URIs look like they point to a location on the
Web, but they don’t. The URI format is used because it makes it unlikely that different organizations will
inadvertently create different XML-based languages with the same namespace. Because the domain
schemas.microsoft.com is owned by Microsoft, only Microsoft will use it in an XML namespace name.
The other reason that there isn’t a one-to-one mapping between the XML namespaces used in XAML
and .NET namespaces is because it would significantly complicate your XAML documents. The problem
here is that WPF encompasses well over a dozen namespaces (all of which start with System.Windows). If
each .NET namespace had a different XML namespace, you’d need to specify the right namespace for each
and every control you use, which quickly gets messy. Instead, the creators of WPF chose to combine all of
these .NET namespaces into a single XML namespace. This works because within the different .NET
namespaces that are part of WPF, there aren’t any classes that have the same name.
The namespace information allows the XAML parser to find the right class. For example, when it looks
at the Window and Grid elements, it sees that they are placed in the default WPF namespace. It then
searches the corresponding .NET namespaces until it finds System.Windows.Window and System.
Windows.Controls.Grid

A namespace is a URI (a URN or a URL), but a URI is not always a URL. The URI used for namespaces is meant to uniquely identify names to prevent clashed. The XML Namespaces Working Group at the time decided to use a technique already know to uniquely identify things: URIs.
As a result, many people think it should actually point to something real. Occasionally that is true, but more often it is not, and it is not meant to be. It is an identitifier, not a location.
In the case of schemas, it is can be used to denote the target namespace, which is the namespace that must be used in documents that need to validated against the schema. To obtain the schema, you will have to ask the vendor. In this case, the schema can be found at a location similar or equal to C:\Program Files (x86)\Microsoft Visual Studio 10.0\Xml\Schemas, look for wpfe.xsd (however, to confuse matters more, Microsoft has decided to create an alias for the target namespace, which is why you will not see the same namespace you mentioned).

Related

The name "ImageViewModel" does not exist in the namespace "clr-namespace:AdminControlCenter.ViewModel"

As the titel say i have the problem that Xaml don´t now my clr namespace, I read the other posts and tried their solution without any success, I rebuild, Start as Admin tried the build conviguration etc.
My Project looks so:
My XAML:
<Controls:MetroWindow x:Class="AdminControlCenter.View.MainView"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resx="clr-namespace:AdminControlCenter.Properties;assembly=AdminControlCenter"
xmlns:local="clr-namespace:AdminControlCenter;assembly=AdminControlCenter"
xmlns:vm="clr-namespace:AdminControlCenter.ViewModel;assembly=AdminControlCenter"
Title="MainWindow" Height="400" Width="600">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="White">
<Grid.DataContext>
<vm:ImageViewModel x:Name="imageViewModel"/>
</Grid.DataContext>
And the Error:
And the ImageViedModel:
namespace AdminControlCenter.ViewModel
{
public class ImageViewModel : ViewModel
{
public ImageViewModel()
{
}
.....
The classes are public and show up at the autoformat, when I type for example
"vm:" It also go to the Definition of the Class by using F12.
When I use some MVVM tutorial tests, they are working. But when I try in my Project all ViewModels are not found.
Why doesen´t found the XAMl my ViewModels ?
Edit:
I tried every variation of the assemblies with and without, I also try to delete the suo and try every build configuration, and I restart VS as Admin etc. I also make a new Project with another name and rewrite all classes (Not Copied them) also without any success
I get this error too sometimes, but I can build/deploy anyway.. I think it's a Visual Studio bug.. Are you able to deploy your solution?
Maybe you can try to copy Model and ViewModel's content somewhere else , delete from the project these two files and recreate the files again.. Or.. Do you know how to use the ViewModelLocator class? Since I started use it I never seen that issue again
See here for details about ViewModelLocator: link

XAML Converter in different namespace

I'm developing a c# Windows Phone 8.1 App in VC2013 and stumbled over an odd issue.
To make my Code more 'clean', I decided to put different parts of my App into different folders. There is no problem with calling XAML pages from these folders in the c# code.
BUT I seem to have issues linking these folders in XAML Code itself. For example I have following structure:
root, Files: App.cs+xaml, Mainpage.cs+xaml
|
-- Folder: Login
|
-- Files: LoginPage.cs+xaml
-- Folder: Converters
|
-- Files: converterClass.cs
To use the IValueConverter from the converterClass.cs like I always did, I put the following in the Header of my XAML file:
<Page
x:Class="myApp.Login.LoginPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:meinStundenplaner.Login"
xmlns:myConverter="using:myApp.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Styles/standardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<myConverter:DateTimeConverter x:Key="DateTimeConverter" />
... some more converters
</ResourceDictionary>
</Page.Resources>
...
Now it happens, that if I type <myConverter: the autocomplete does find my converter classes like usually, but when compiling (it is compiling) and testing on device none of the converters works and also I get an error in the Error-list saying
The name 'DateTimeConverter' in namespace 'using:myApp.Converters' does not exist.
Where did I go wrong?
wow ... after using VS2015 and the same error persisted, I played a little bit around with settings and stuff.
I kept being curious, why it would compile and deploy even it triggered so many errors. The problem appeared to be inconsistent.
The solution was different than expected:
You have to set Solution Platforms to x86 instead of ARM (which I needed to deploy on device). Luckily VS2015 has this option in a handy shortcut, what you can't say about VS2013.
Nevertheless it's solved...

DataContext is not bind in the XAML file

I am new to WPF + MVVM architecture. In my application, I am trying to implement "DataContext" in the XAML itself as below
<Window x:Class="MyWPF.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MyWPF.UI.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:Northwind_DataViewModel></vm:Northwind_DataViewModel>
</Window.DataContext>
<Grid>
</Grid>
</Window>
I am getting "The name 'Northwind_DataViewModel' does not exists in the namespace 'clr-namespace:MyWPF.UI.ViewModel".
I have ViewModel file. Before using "DataContext", I built this application.
Could you please let me know what is wrong with my code?
thanks
The name 'Northwind_DataViewModel' does not exists in the namespace 'clr-namespace:MyWPF.UI.ViewModel".
Chances are, you compiler is right. There is no class named MyWPF.UI.ViewModel.Northwind_DataViewModel in your project. Make sure it exists, has a public, parameterless constructor, is not an inner class of something and not generic. If that's the case, it will work.
In case namespace is declared in separate assembly from where your XAML resides, you have to provide assembly name as well.
Sample:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Similarly, provide your assembly name where this namespace resides (Assuming assembly name is MyWPF.UI):
xmlns:vm="clr-namespace:MyWPF.UI.ViewModel;assembly=MyWPF.UI"
Thanks a lot your responses. Finally I got the solution. Actually, this project was placed in a shared folder. So, I moved it to my local drive (C:). Now, it is working fine without any issues.
I guess, in shared path / shared folder, it may not work. Please let me know if I am wrong.
thanks again.
In order to solve this problem, you need to understand how the DataContext works. This answer contains a lot of hints and links to supporting tutorials:
ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext"

TextBox custom control header problems

I'm currently trying to use this TextBox AutoComplete custom control :
http://www.codeproject.com/Articles/26535/WPF-Autocomplete-Textbox-Control?msg=3484969#xx3484969xx
This is the first time I'm adding such external things in my project and when I add the header line in my Xaml file like explained in the article :
<UserControl x:Class="Maha.Gestion.Note_de_frais.SaisieNoteDeFrais"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WPFAutoCompleteTextbox"
>
VisualStudio tell me that clr-namespace is not included in the assembly.
I'm sure this is something totally trivial but after few hours still can't find any solution.
All files asked by the author of the article has been added of course.
Someone can explain me the good way to add such external resources ?
Thanks
xmlns stands for XML NameSpace. It basically links a prefix of your choice to a namespace. In your case, it links the local prefix with the namespace declared in the file that you want to use.
You can find out more by looking at the XAML Namespaces and Namespace Mapping for WPF XAML article at MSDN.
Please check that the file that you want to use has this exact namespace declared.
Please remove all white space from your xmlns declarations - you may have extraneous characters hidden there.
Like this:
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WPFAutoCompleteTextbox">

Visual Studio refactor-rename causes inexplicable global replace of attributes in XAML file

I feel like I must be going crazy, but I just changed the name of a property in a view model (a C# file) from Width to Size using Visual Studio's refactor-rename feature. When I was done, this error appeared in the Error window:
Error 2: The property 'Size' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
The error was referring to a XAML UserControl file. When I checked the file to see what was up, I realized that every attribute named Width had been changed to Size.
Example:
<UserControl x:Class="ApbSymbolGenerator.Views.Symbol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Size="{Binding Size}"
Height="{Binding Size}">
This is the first time I've ever had refactor-rename cause a change to a XAML file (besides the x:class value). (Note: I did not do a global find/replace, I did refactor-rename. I undid everything and performed the rename again, and it did the same thing.)
Strangely, it only affected one of several XAML files in my app that has a Width property.
Any explanation what could be going on here?
Looks like this is a bug (that won't be fixed) - Refactor Rename Bug

Categories