Assembly Reference in Loose XAML - xaml

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!

Related

Loading Loose Xaml with custom controls on WinRT fails unless dummy DataTemplate exists

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.

What does this xmlns attribute do

<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.

How to reference external assemblies in WinRT XAML?

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"

WinRT XAML apps + missing [XmlnsDefinition] attribute

There is not [XmlnsDefinition] attribute in XAML metro style applications, based on WinRT.
How my custom namespace mappings from WPF/SL apps should be migrated to WinRT XAML apps?
Looks like XmlnsDefinitionAttribute is missing. There is XmlnsDefinition struct but that is of not much use since there is no way to use it to set custom namespace mappings.
A discussion at https://social.msdn.microsoft.com/Forums/windowsapps/en-US/026baea0-6324-46ee-956a-72dbb4c90ca1/xmlnsdefinition-replacement-in-winrt?prof=required says:
there is no analogous attribute.
You might be able to provide similar data in a custom
IXamlMetadataProvider implementation, but since that is automatically
generated for you I'm not sure it can be overridden.

WP7 Silverlight: Data binding with the same XAML at design & runtime

I'd like to use the same data at design and runtime in my WP7 application. The relevant part of the XAML looks like this:
<phone:PhoneApplicationPage ...
d:DataContext="{d:DesignData Source=data.xaml}"
DataContext="{Binding Source=???}"
This way I can see the data at design time. Is there a way to use the same data at runtime?
Was not aware of the existence of d:DataContext. I dare to say:
<phone:PhoneApplicationPage ...
d:DataContext="{d:DesignData Source=data.xaml}"
DataContext="{Binding Source=data.xaml}"
--- Edit added -------------------
Well... checking here and there found that, as expected, "DesignData" is not the same as "Binding" (I was misreading this, sorry). "DesignData" has the ability to instance, at design time, an object based on a xml file and this has no effect at runtime. The Binding we are try to build will never work, as its purpose is to use already instanced objects.
When you run the default template for panorama applications it instances its own data (check the code for MainViewModel.cs)... and that's why it shows is different from that in the xml file. All you have to do is, manually, open the xml in the "LoadData" method and load the file data so you can display the same at runtime and design time.