Monday, September 10, 2012

wpf silverlight multi-targeting ResourceDictionary

wpf silverlight multi-targeting ResourceDictionary

I'm writing some multi-targeting application (.net 4.0). I have problems with ResourceDictionay. Scenario: Create WPF CustomControlLibrary - name it "WpfLib", default Namespace "test" In WpfLib create UserControl - name it "uc"

<UserControl x:Class="test.uc"              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"               d:DesignHeight="300" d:DesignWidth="300">     <UserControl.Resources>         <ResourceDictionary>             <ResourceDictionary.MergedDictionaries>                 <ResourceDictionary Source="res.xaml"/>             </ResourceDictionary.MergedDictionaries>         </ResourceDictionary>     </UserControl.Resources>        <Grid>         <Button Style="{StaticResource bStyle}" Content="Button" Height="23"  Width="75" />     </Grid> </UserControl> 

Code for "res.xaml"

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <Style x:Key="bStyle" TargetType="{x:Type Button}">         <Setter Property="Background" Value="Red" />      </Style> </ResourceDictionary> 

Everything is great now....Button is Red

Now lets add new Project Silverlight Application and name it "SlApp" (Silverlight 5)(namespace "test") Lets add "uc" from "WpfLib" as link("uc.xaml" "uc.xaml.cs" ). Create new ResourceDictionary in "SlApp" and name it res.xaml like this:

 <ResourceDictionary         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">         <Style x:Key="bStyle" TargetType="Button">             <Setter Property="Background" Value="Blue" />         </Style>     </ResourceDictionary> 

New ResourceDictionary has TargetType="Button" because x:Type is not supported in SilverLight. Background is set to color Blue not red.

And here starts the problem.
How do i make it work? Control uc displays error on merging(in Silverlight version). I need different ResourceDictionary on Wpf and Silverlight version of usercontrol. ClassLibrary doesn't support app.xaml and i can't use default style.

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog