Suppose I have a top-level ResourceDictionary element in a XAML file with no C# code-behind. How can I put this element in a specific namespace?
I'm not sure if you can -- but I've never seen this done.
What I have seen is putting ResourceDictionary XAML files in a directory structure in an assembly, and getting it via the Pack URI syntax.
Dr. WPF has an interesting post on getting ResourceDictionaries in code here.
It is much simpler than I had thought:
<ResourceDictionary x:Class="SomeNamespace.SomeClassName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>
Related
I have a UWP application for Windows 10 with MVVM Light plugged in.
I store a ViewModelLocator in the App.Resources.
When I have just a ViewModelLocator in my App.Resources, everything works fine.
<Application.Resources>
<viewModel:ViewModelLocator x:Key="Locator" />
</Application.Resources>
As soon as I add a String, Converter or something similar, application doesn't crash but the ViewModelLocator constructor is not being called anymore.
No errors or exceptions being thrown, just ResourceDictionary is not being loaded or fails during load.
<Application.Resources>
<viewModel:ViewModelLocator x:Key="Locator" />
<x:String x:Key="SampleString">Hello</x:String>
</Application.Resources>
If I add a Style, DataTemplate, Brush, Color - everything works fine.
Haven't noticed that behavior on Windows Phone 8, Silverlignt or WPF before.
Moving styles or objects to separate ResourceDictionaries and load them using MergedDictionaries didn't help.
I would like to have a list of objects in ResourceDictionary so that all constructors of these objects are being called automatically on the app start.
Please advise.
P.S.:
Even two similar converters don't work, while one is created without any problem
<Application.Resources>
<!--<viewModel:ViewModelLocator x:Key="Locator" />-->
<converters:StringFormatConverter x:Key="StringFormat1" />
<converters:StringFormatConverter x:Key="StringFormat2" />
</Application.Resources>
Looking for an example of ResourceDictionary usage, found similar question:
Merged ResourceDictionary initalization in UWP app
Try adding them in a Resource Dictionary like this
<Application.Resources>
<ResourceDictionary>
<viewModels:ViewModelLocator x:Key="Locator"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="XAMLResources/Styles.xaml" />
<ResourceDictionary Source="XAMLResources/DataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
If you want to take a look at a complete example the code snippet is from this repository on GitHub https://github.com/AppCreativity/Kliva
You will notice we add our Converters in the Styles.xaml and that is working fine...
That's all because of lazy initialization. I've made some experiments and figured out this picture. I hope you will catch the idea.
http://screencast.com/t/mxyBGBDuZ
I have a CustomControl derived class in a dll (MyNamespace.UI). How can I use this control in my main application? I've tried adding a reference to the project and then using a custom XAML namespace to point to my namespace, but it can't find it:
<Page ...
xmlns:ui="using:MyNamespace.UI"
...>
<Canvas>
<ui:MyControl />
</Canvas>
</Page>
I get an error:
The name "MyControl" does not exist in the namespace "using:MyNamespace.UI".
Searching around, I managed to find the following quote:
The name of the assembly that defines the backing types for a XAML
namespace is not specified in the mapping. The logic for which
assemblies are available is controlled at the app-definition level and
is part of basic app deployment and security principles. Declare any
assembly that you want included as a code-definition source for XAML
as a dependent assembly in project settings.
http://msdn.microsoft.com/en-gb/library/windows/apps/jj150588.aspx
Can someone point me in the direction of the "dependent assembly" setting in the project settings?
Edit After making sure the root namespace project setting matched the namespace of my control (MyNamespace.UI) I could get the project to compile. But I can't get it to run because I get loads of crashes with the following information:
WinRT information: Cannot create instance of type 'MyNamespace.UI.MyControl' [Line: 44 Position: 37]
No information on why it couldn't make the control though... I stepped through the constructor of MyControl and the exception is thrown in InitializeComponent(). This is the XAML of my UserControl (MyControl):
<UserControl
x:Class="MyNamespace.UI.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
</Grid>
</UserControl>
Edit 2 I deleted the Grid control, built and ran, and it worked, so then I put the Grid control back, and it still worked. Very weird.
You need to make sure the MyNamespace.UI assembly has a RootNamespace property that matches MyNamespace.UI. This property can't be changed in the project settings pages, but you can do it via notepad or equivalent. My RootNamespace was set to UI, and when I changed it, it started working.
<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 have a solution with 2 projects. First is the primary application, the second has controls in it. In the Generic.xaml, in a Silverlight project, one would do
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyLibNamespace;component/Themes/SomeFolder/MyControl.xaml" />
</ResourceDictionary.MergedDictionaries>
This doesn't seem to work in a Windows Store application class library. ReSharper wants the filepath to be "/Themes/SomeFolder/MyControl.xaml" but I still get a XamlParseException: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'. [Line: 7 Position: 36]
There's no such thing as a "Xaml file within other class/assembly" in WinRT. In other words: "EmbeddedResource" or "Resource" simply doesn't exist in a WinRT component (and yes this is a pain). This is also why \Themes\Generic.xaml is copied out into the output directory).
See answer here on msdn
If you have a dll named "CustomControl" with a ResourceDictionary named "Styles.xaml", the App.xaml should contain
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///CustomControls/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Can reproduce the same behavior if the ResourceDictionary Source is set to a XAML that is a UserControl. However, if the XAML file referenced is a dedicated ResourceDictionary, there is no problem accessing internal styles with the ms-appx:/// scheme, following the ms-appx:///PROJECT_NAME/PATH_TO_XAML.xaml pattern.
Any chance the ResourceDictionary can be separated from the UserControl, if that is the case?
The situation:
"Shell" project with App.xaml and a resource dictionary in Styles/Default.xaml with the interesting parts thus:
Default.xmal
<ResourceDictionary
<Style x:Key="StandardTextBox" TargetType="TextBox">
...
</Style>
</ResourceDictionary
App.xaml
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Default.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
In a module project I've got a form with:
<TextBox Style="{StaticResource StandardTextBox}" />
At runtime I get the infamous "Error HRESULT E_FAIL has been returned from a call to a COM component." exception. The funny thing is during design time, in VS, the style is applied just fine in design mode. (and how VS.Net works the magic of knowing there's a resource in App.xaml in the Shell project - which is not referenced by the module project AT ALL - is baffling ... but I digress)
My overall goal is to have resources defined in files separate from App.xaml, in the Shell project, and have the styles applied intrinsically across the Module projects.
Update: Yeah, I was totally on the wrong war path here. The TextBox style that Blend generates references another style for the ValidationToolTip. Failing to include that will cause the issue described above. Unfortunately the error message was quite unhelpful and the squiggle underline in VS is easily missed when it's deep in the middle of the XAML definition and way off to the right. Live and learn.
The real issue was not including another referenced style. See this.