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

标签 c# silverlight xaml silverlightcontrols

我写了一个简单的 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 属性实际上什么都不做,您将此值传递给子控件,因为它有任何作用。

关于c# - 是什么阻止了样式在运行时应用于 Silverlight 控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4577419/

相关文章:

c# - sql中不能减

c# - 控制台应用程序中的进度条

c# - 获取 ListBox 中的所有选中项(包括重复项)

c# - GeoCoordinateWatcher 没有在模拟器中获取 GeoCoordinate Speed?

wpf - 在wpf中调整父窗口大小时弹出窗口超出父窗口

c# - 在 UWP XAML 中动态绑定(bind) NavigationViewItem && NavigationViewItemHeader

c# - UWP 在代码隐藏中设置主题资源丙烯酸

c# - 为我的 asp.net 页面添加自定义基类会产生错误

c# - NHibernate 中不区分大小写的排序顺序

Silverlight RichTextBox/ListBox/ScrollViewer 奇怪的行为