Skip to main content
 首页 » 编程设计

c#之是什么阻止了样式在运行时应用于 Silverlight 控件

2024年02月23日15Leo_wl

我写了一个简单的 Silverlight 应用程序。我的样式在设计时正确显示,但是当我尝试运行应用程序时,合并到 app.xaml 文件中的资源字典文件中的任何样式在运行时都不会应用于任何控件。

实际上,只有 UserControl 样式似乎不适用。但其余的都在工作(如页面上的 Button )。是什么导致了这个问题,我该如何解决?

我的代码是这样的:

样式.xaml:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
 
    <Style TargetType="UserControl"> 
        <Setter Property="FlowDirection" Value="RightToLeft" /> 
        <Setter Property="FontFamily" Value="Tahoma" /> 
        <Setter Property="Background" Value="Aqua" /> 
    </Style> 
    <Style TargetType="Button" > 
        <Setter Property="Background" Value="Aqua" /> 
    </Style> 
</ResourceDictionary> 

应用程序.xaml:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
             x:Class="Silverlight.Test._01.App" 
             > 
    <Application.Resources> 
        <ResourceDictionary> 
            <ResourceDictionary.MergedDictionaries> 
                <ResourceDictionary Source="Styles.xaml" /> 
            </ResourceDictionary.MergedDictionaries> 
        </ResourceDictionary> 
    </Application.Resources> 
</Application> 

主页.xaml:

<UserControl x:Class="Silverlight.Test._01.MainPage" 
    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" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 
 
    <Grid x:Name="LayoutRoot" Background="White"> 
        <Button Content="This is a test" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="220"   /> 
        <sdk:Label   Height="28" HorizontalAlignment="Left" Margin="12,6,0,0" Name="label1" VerticalAlignment="Top" Width="351" Content="Test label" /> 
    </Grid> 
</UserControl> 

请您参考如下方法:

这不起作用的至少一个原因是因为您从未实际创建 UserControl 的实例。您实际上创建了 Silverlight.Test._01.MainPage 的实例。

此外,与 Button 不同,UserControl 实际上并未将控件上的 DefaultStyleKey 属性设置为 UserControl试图在代码后面的 DefaultStyleKey 中设置一个值将导致异常。

对此没有通用的解决方法。最接近的是将默认样式更改为标准键控资源:-

<Style x:Key="UserControlDefaultStyle" TargetType="UserControl"> 
    <Setter Property="FlowDirection" Value="RightToLeft" /> 
    <Setter Property="FontFamily" Value="Tahoma" /> 
    <Setter Property="Background" Value="Aqua" /> 
</Style>  

现在将您的用户控件 xaml 更改为:-

<UserControl x:Class="Silverlight.Test._01.MainPage" 
    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" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    Style="{StaticResource UserControlDefaultStyle}"     
> 
 
    <Grid x:Name="LayoutRoot" Background="{Binding Parent.Background, ElementName=LayoutRoot}"> 
        <Button Content="This is a test" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="220"   /> 
        <sdk:Label   Height="28" HorizontalAlignment="Left" Margin="12,6,0,0" Name="label1" VerticalAlignment="Top" Width="351" Content="Test label" /> 
    </Grid> 
</UserControl> 

请注意,这不是一个通用的解决方案,因为您需要向您创建的每个 UserControl 添加额外的 Style 属性。

还要注意 LayoutRoot Background 属性上的绑定(bind)。 UserControl.Background 属性实际上什么都不做,您将此值传递给子控件,因为它有任何作用。