本文介绍了无法在Silverlight中的样式中添加系统颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XAML中为SystemColors定义了资源。它是工作伟大的,如果我设置Foregroung属性直接到TextBlock。但是,如果我在样式中分配了前景属性,我得到下面的错误。我不知道是什么问题和如何解决它。任何想法都非常感激!

I defined resource in XAML for SystemColors. It is working great if I set Foregroung property directly to the TextBlock. However, I am getting an error shown below if I assign foreground property in style. I am not sure what is the issue and how to solve it. Any ideas are highly appreciated!

当我直接在texblock中设置前景时的代码。

Code when I set foreground directly in the texblock. It is working perfectly

<TextBlock Text="WindowTextColor" Foreground="{Binding WindowTextColor, Source={StaticResource SystemColors}, Converter={StaticResource colorConverter}}" />

当我通过样式设置前景属性时的代码。我的应用程式崩溃:

Code when I set foreground property through the style. My app crashes:

<UserControl.Resources>
  <local:ColorToBrushConverter x:Key="colorConverter" />
  <local:SystemColorsWrapper x:Key="SystemColors" />
  <Style x:Key="TextBlockStyle1" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{Binding WindowColor, Source={StaticResource SystemColors}, Converter={StaticResource colorConverter}}"/>
  </Style>
</UserControl.Resources>

<Grid Background="#FFB8B8B8">
    <TextBlock Text="WindowColor" Style="{StaticResource TextBlockStyle1}" />
</Grid>

我得到的错误:

System.Windows.Markup.XamlParseException发生
Message = Set属性''抛出异常。 [Line:11 Position:41]
LineNumber = 11
LinePosition = 41
StackTrace:
在System.Windows.Application.LoadComponent(Object组件,Uri resourceLocator)
在SilverlightSysColors.MainPage.InitializeComponent()
在SilverlightSysColors.MainPage..ctor()
InnerException:System.NotSupportedException
消息=无法设置只读属性。
StackTrace:
at MS.Internal.XamlMemberInfo.SetValue(Object target,Object value)
at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType,XamlQualifiedObject& inObj,XamlPropertyToken inProperty,XamlQualifiedObject& inValue)
InnerException:

System.Windows.Markup.XamlParseException occurred Message=Set property '' threw an exception. [Line: 11 Position: 41] LineNumber=11 LinePosition=41 StackTrace: at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at SilverlightSysColors.MainPage.InitializeComponent() at SilverlightSysColors.MainPage..ctor() InnerException: System.NotSupportedException Message=Cannot set read-only property ''. StackTrace: at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value) at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue) InnerException:

推荐答案

您不能在a Setter 。绑定应用于 Setter ValueProperty 依赖属性,这不是你的意图。发生什么是在Xaml解析(在绑定可以应用其值)之前应用的样式,这反过来导致样式被密封。当尝试通过绑定调整 Setter 的值时,它会失败,因为在使用该样式后值将变为只读。

You can't include a binding in the a Setter. The binding is applied to the ValueProperty dependency property of the Setter which isn't really your intention. What happens is the style is applied during Xaml parsing (before the binding can apply its value) which in turn cause the style to be sealed. When an attempt by the binding is made to adjust the value of the Setter it fails because values become read only once the style has been used.

编辑

根据我的猜测,动态绑定不是真正必要的,方便的访问 SystemColors 静态类的成员的方法。我的方法是创建一个 ResourceDictionary 的导数,以便向 SystemColors 类包含一个完整的资源集,包括每个属性的颜色和画笔,并相应命名。一点反思是有帮助的: -

A work around based on my guess that dynamic binding isn't really that necessary you just want a convenient means of accessing the members of the SystemColors static class. My approach would be to create a derivative of ResourceDictionary to carry a complete set of resources to the SystemColors class including both a Color and a Brush for each property and named accordingly. A little bit of reflection is helpful:-

public class SystemColorsResources : ResourceDictionary
{
    public SystemColorsResources()
    {
        foreach (PropertyInfo pi in typeof(SystemColors).GetProperties())
        {
            Color c = (Color)pi.GetValue(null, null);
            Add(pi.Name, c);
            Add(pi.Name.Replace("Color", "Brush"), new SolidColorBrush(c));
        }
    }
}

在App.Xaml中的 MergedDictionaries 列表中包含一个实例: -

With this class in your app include an instance of it in your MergedDictionaries list in the App.Xaml:-

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <local:SystemColorsResources />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

现在您可以使用系统颜色属性名称及其Brush变体作为直接静态资源: -

Now you can use the system color property names with their "Brush" variants as straight forward static resources:-

<UserControl.Resources>
  <Style x:Key="TextBlockStyle1" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource WindowBrush}" />
  </Style>
</UserControl.Resources>

<Grid Background="#FFB8B8B8">
    <TextBlock Text="WindowColor" Style="{StaticResource TextBlockStyle1}" />
</Grid>

这篇关于无法在Silverlight中的样式中添加系统颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:05