无法在 Silverlight 样式中添加系统颜色?

发布于 2024-11-04 03:54:56 字数 1596 浏览 0 评论 0原文

我在 XAML 中为 SystemColors 定义了资源。如果我将 Foregroung 属性直接设置为 TextBlock,效果会很好。但是,如果我在样式中分配前景属性,则会收到如下所示的错误。我不确定问题是什么以及如何解决。任何想法都受到高度赞赏!

当我直接在 texblock 中设置前景时的代码。 它运行完美

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

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

<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 消息 = 设置属性 '' 引发异常。 [线路:11 位置:41] 行号=11 线位置=41 堆栈跟踪: 在System.Windows.Application.LoadComponent(对象组件,Uri资源定位器) 在 SilverlightSysColors.MainPage.InitializeComponent() 在 SilverlightSysColors.MainPage..ctor() 内部异常:System.NotSupportedException 消息=无法设置只读属性“”。 堆栈跟踪: 在 MS.Internal.XamlMemberInfo.SetValue(对象目标,对象值) 在MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType,XamlQualifiedObject&inObj,XamlPropertyToken inProperty,XamlQualifiedObject&inValue) 内部异常:

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!

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>

The error I am getting:

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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2024-11-11 03:54:56

您不能在 Setter 中包含绑定。绑定应用于 SetterValueProperty 依赖属性,这并不是您真正的意图。所发生的情况是在 Xaml 解析期间应用样式(在绑定可以应用其值之前),这反过来又导致样式被密封。当绑定尝试调整 Setter 的值时,它会失败,因为只有在使用样式后,值才会变为只读。

编辑

根据我的猜测,动态绑定实际上并不是那么必要,您只是需要一种方便的方法来访问 SystemColors 静态类的成员。我的方法是创建 ResourceDictionary 的派生类,以将一整套资源携带到 SystemColors 类,包括每个属性的 Color 和 Brush 并相应命名。一点点反思是有帮助的:-

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 列表中:-

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

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

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

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.

Edit

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));
        }
    }
}

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文