嗯,我有一个黑白应用程序,我需要一个降低亮度的功能,我该怎么做?所有白色都来自保存在 ResourceDictionary(Application.xaml) 中的 SolidColorBrush,我当前的解决方案是放置一个空窗口,其不透明度为 80%,但这不允许我使用底层窗口。

有帮助吗?

解决方案

如果您的所有 UI 元素都使用相同的 Brush, ,为什么不直接修改 Brush 降低亮度?例如:

public void ReduceBrightness()
{
    var brush = Application.Resources("Brush") as SolidColorBrush;
    var color = brush.Color;
    color.R -= 10;
    color.G -= 10;
    color.B -= 10;
    brush.Color = color;
}

在您发表评论后进行编辑 Brush 被冻结:

如果您使用内置画笔之一(通过 Brushes class) 那么它将被冻结。不要使用其中之一,而是声明您自己的 Brush 不冻结它:

<SolidColorBrush x:Key="Brush">White</SolidColorBrush>

在罗伯特对应用程序级资源的评论后进行编辑:

罗伯特是对的。资源添加于 Application 如果可以冻结,级别会自动冻结。即使您明确要求不要冻结它们:

<SolidColorBrush x:Key="ForegroundBrush" PresentationOptions:Freeze="False" Color="#000000"/>

我可以看到有两种解决方法:

  1. 正如 Robert 建议的那样,将资源放在资源树中的较低级别。例如,在一个 WindowResources 收藏。但这使得分享变得更加困难。
  2. 将资源放入不可冻结的包装器中。

作为 #2 的示例,请考虑以下内容。

应用程序.xaml:

<Application.Resources>
    <FrameworkElement x:Key="ForegroundBrushContainer">
        <FrameworkElement.Tag>
            <SolidColorBrush PresentationOptions:Freeze="False" Color="#000000"/>
        </FrameworkElement.Tag>
    </FrameworkElement>
</Application.Resources>

窗口1.xaml:

<StackPanel>
    <Label Foreground="{Binding Tag, Source={StaticResource ForegroundBrushContainer}}">Here is some text in the foreground color.</Label>
    <Button x:Name="_button">Dim</Button>
</StackPanel>

Window1.xaml.cs:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        _button.Click += _button_Click;
    }

    private void _button_Click(object sender, RoutedEventArgs e)
    {
        var brush = (FindResource("ForegroundBrushContainer") as FrameworkElement).Tag as SolidColorBrush;
        var color = brush.Color;
        color.R -= 10;
        color.G -= 10;
        color.B -= 10;
        brush.Color = color;
    }
}

它不是那么漂亮,但这是我现在能想到的最好的。

其他提示

通过改变我的根元素的不透明度,而不是试图修改刷,但它仍然会是很好,如果一些告诉我,如果我能做到这解决了一些如何或其不可能的。

肯特的解决方案将工作,如果被SolidColorBrush在一个较低的水平加入到该资源。当他们加入到Application.Resources Freezables自动冻结。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top