【问题标题】:What could be preventing styles from applying to a Silverlight control at runtime?什么会阻止样式在运行时应用于 Silverlight 控件?
【发布时间】:2011-01-02 06:25:22
【问题描述】:

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

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

我的代码是这样的:

Styles.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>

App.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>

MainPage.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>

【问题讨论】:

  • 告诉我们您之前问过这个问题但没有任何结果,只有在您具体告诉我们之前的建议是什么以及为什么它们对您不起作用时才会有帮助。

标签: c# silverlight xaml silverlightcontrols


【解决方案1】:

这不起作用的至少一个原因是因为您从未真正创建过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>

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

还要注意 LayoutRoot Background 属性上的绑定。 UserControl.Background 属性实际上什么都不做,你将这个值传递给子控件,因为它有任何效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2016-03-22
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多