wpf silverlight多目标ResourceDictionary

时间:2012-09-09 11:10:19

标签: c# wpf silverlight

我正在编写一些多目标应用程序(.net 4.0)。我有ResourceDictionay的问题。 的情景: 创建WPF CustomControlLibrary - 将其命名为“WpfLib”,默认命名空间“test” 在WpfLib中创建UserControl - 将其命名为“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>

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

现在一切都很棒......按钮是红色

现在让我们添加新的Project Silverlight应用程序并将其命名为“SlApp”(Silverlight 5)(命名空间“test”) 让我们从“WpfLib”中添加“uc”作为链接(“uc.xaml”“uc.xaml.cs”)。 在“SlApp”中创建新的ResourceDictionary,并将其命名为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="Button">
            <Setter Property="Background" Value="Blue" />
        </Style>
    </ResourceDictionary>

新的ResourceDictionary具有TargetType =“Button”,因为SilverLight不支持x:Type。背景设置为蓝色而不是红色。

这就是问题所在 我如何使它工作?控制uc在合并时显示错误(在Silverlight版本中)。我需要在Wpf和Silverlight版本的usercontrol上使用不同的ResourceDictionary。 ClassLibrary不支持app.xaml,我不能使用默认样式。

0 个答案:

没有答案
相关问题