If I have a UserControl: Foo.Bar.MyClass, I know I can reference it in XAML by declaring:
xmlns:foobar="clr-namespace:Foo.Bar"
and then using the reference
<foobar:MyClass />
But if I declare only the Foo namespace:
xmlns:foo="clr-namespace:Foo"
is there a way for me to reference MyClass as Foo:Bar.MyClass in my XAML?
As per my knowledge this is not possible.
You must add assembly reference for the type is being used in XAML.
Similarly it is not possible with C#. You are not able to access type which assembly reference is not included in using list.
Related
In ReactiveUI, I run this code at a certain point:
const string template = "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:routing='using:ReactiveUI.Routing'>" +
"<routing:ViewModelViewHost ViewModel=\"{Binding}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" IsTabStop=\"False\" />" +
"</DataTemplate>";
var theTemplate = XamlReader.Load(template);
On other platforms, this works great (the xmlns declaration is different of course), but on {WinRT / Metro / Windows Store}, this throws an Unspecified Error:
WinRT information: The type 'ViewModelViewHost' was not found. [Line: 1 Position: 253]
The Twist
However, if you include a dummy resource on the page:
<Page.Resources>
<DataTemplate x:Name="Foo">
<routing:ViewModelViewHost ViewModel="{Binding}" />
</DataTemplate>
</Page.Resources>
...then it works! What gives?
The "twist" makes me think this must be because the application does not have correct XAML metadata for the type being instantiated - rather than using reflection to resolve types in XAML files like WPF/Silverlight, WinRT uses code generation to resolve via the IXamlMetadataProvider interface (there's a decent description here; this sounds like what you're doing, see also the followup). Adding the reference forces this metadata code to be generated properly. If this is the case, you should be able to achieve the same effect by simply adding the type itself to the resources under some unused key, without the data template.
Have a look in your application's "obj" directory, Visual Studio generates a XamlTypeInfo.g.cs file to implement IXamlMetadataProvider. This should contain an entry for the type that is failing - in the case where you have added a dummy reference, there should be full details required to instantiate the type. Without this, I've found it's possible to have some reference to type type, but insufficient information - however this prevents the fallthrough behaviour (looking up the type in a dependent DLL which might have a custom metadata provider).
Other than adding a dummy reference to the library type in the final application itself, the only solution I found for this is to apply the Bindable attribute to the type. While this is supposed to relate to C++, I found this can be used in C# to force a type to always appear in the code generated for XAML type metadata.
<Window x:Class="Template.MainWindow"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<Grid>
</Grid>
What does xmlns:System="clr-namespace:System;assembly=mscorlib means?
My ideas:
Include System from mscorlib? What about clr-namespace? So I can
use all functionality in mscorlib in Template.MainWindow.xaml.
Thanks for replies
Is the namespace associated to Window tag
namespaces are used to avoid collisions when two different systems use the same tag name.
Refer to Microsoft Documentation about this:
http://msdn.microsoft.com/en-us/library/ms747086.aspx
See the two topics:
The WPF and XAML Namespace Declarations
Mapping to Custom Classes and Assemblies
You will get idea little clear.
I'm trying to add a reference to an external assembly inside XAML
the syntax is the following:
xmlns:my_namespace="using:CompanyName.AssemblyName"
The problem is that the actual class I'd like to use resides under CompanyName.AssemblyName.InnerName namespace.
What's the appropriate way?
Try:
xmlns:my="using:CompanyName.AssemblyName.InnerName"
I have scenario where my loose xaml file can contain the custom control from another assembly. How do i make a reference to that assembly. My Loose XAML and assembly are at the same path.
I know the embedded xaml or xaml with in a project, the reference is added like this:
xmlns:WpfToolKit="http://schemas.microsoft.com/wpf/2008/toolkit"
Now how can i give similar type of reference in the loose xaml file.
Like so:
xmlns:Awesome="clr-namespace:MyAwesomeNameSpace"
And then use your controls as such:
<Awesome:MyAwesomeControl />
For nice looking schemas, please read this article on MSDN:
http://msdn.microsoft.com/en-us/library/ms747086.aspx
You will need the XmlnsDefinitionAttribute definition on your class.
Hope this helps!
VB.NET automatically prefixes the root namespace set in the project properties to the namespace of each class. This is different from C#, where the full namespace must be declared each time.
Is it possible to override this behaviour, creating a namespace outside of the root namespace?
If I understand you correctly you just need to set a blank namespace in the project properties dialog and then set namespaces within each source file using Begin/End Namespace commands.
From VS2012 onwards it's possible to get around this, see stackoverflow.com/a/17360357/233095
Defining the default for the project as blank and then taking total control in each class allows you to do what c# does. However certain project types (Library I believe) do not allow you to change the default namespace to blank.
Use of the Global keyword does not allow you to jump out of the root namespace either:
http://msdn.microsoft.com/en-us/library/16czfx55.aspx
They now implemented it:
Namespace Global.MyNamespace
End Namespace
You can change the namespace of the entire project by going to properties on the project.
else you will have to have a empty root namespace and set the name space in each file with the
Namespace test
class.....
End Namespace