WPF Resource Common Problem - No App.xaml, No Shares

In the application I'm working on, I ran into a small (big problem).

I am working on an application module for my company. The application is a WinForm application, but I was working on a WPF application (not really an application, as you will see) that will be hosted in this WinForm application when it is complete.

To do this, I use a WinForm control element, and I created a custom shell element, and then other custom control windows inside this custom control. Thus, it appears as a WPF application and uses the WinForm application as a launch, since the WPF application is actually just a collection of WPF controls.

The problem I am facing is that since I did not create the actual โ€œWPF applicationโ€, there is no App.xaml. This disconnected me from using shared resources as I wish, especially for XAML shares.

Is there a way that I can still process my collection of custom WPF controls as a WPF application and somehow use the App.xaml file for my resources. If not, what are my options for using shared resources in my application.

+4
source share
1 answer

Add the ResourceDictionary (xaml) file to the project (suppose this is a class library - the WPF user management library), merge it on top of Generic.xaml , then you can access it and your StaticResource will work.

You can also include the resource in the Generic.xaml file (or any of its own xaml file).

Here's what your current dictionary looks like:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfCustomControlLibrary1"> <sys:String x:Key="myString">sdfasdf</sys:String> <Style TargetType="{x:Type local:CustomControl1}"> <Setter Property="Text" Value="{StaticResource myString}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomControl1}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <TextBlock Text="{TemplateBinding Text}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> 

I initialized a run-time instance of the above control in VS2010, and it showed the text ( Text is a property of the string DP that I manually added to CustomControl1 ), which means it is reading the resource myString .

You can find more information here and here .

+1
source

All Articles