嗯,我有一个黑色和白色的应用程序,我需要一个降低亮度的功能,我该怎么办?所有白色都来自保存在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;
}
Run Code Online (Sandbox Code Playgroud)
在您对Brush被冻结的评论后编辑:
如果您使用其中一个内置画笔(通过Brushes类),它将被冻结.而不是使用其中之一,声明自己Brush而不冻结它:
<SolidColorBrush x:Key="Brush">White</SolidColorBrush>
Run Code Online (Sandbox Code Playgroud)
在Robert对应用程序级资源发表评论后编辑:
罗伯特是对的.Application如果可以冻结,则在该级别添加的资源将自动冻结.即使你明确要求他们不要被冻结:
<SolidColorBrush x:Key="ForegroundBrush" PresentationOptions:Freeze="False" Color="#000000"/>
Run Code Online (Sandbox Code Playgroud)
我可以看到有两种解决方法:
Window的Resources集合.这使得分享更难.作为#2的示例,请考虑以下内容.
App.xaml:
<Application.Resources>
<FrameworkElement x:Key="ForegroundBrushContainer">
<FrameworkElement.Tag>
<SolidColorBrush PresentationOptions:Freeze="False" Color="#000000"/>
</FrameworkElement.Tag>
</FrameworkElement>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
Window1.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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
它不是那么漂亮,但它是我现在能想到的最好的.
| 归档时间: |
|
| 查看次数: |
4470 次 |
| 最近记录: |