在UWP中,如何在代码中更改样式的设置器?

问题描述 投票:0回答:2

我正在尝试在UWP的TeachingTip中更改ActionButton样式。

教学提示具有以下属性:

public Style ActionButtonStyle { get; set; }

我正在尝试做这样的事情:

mytip.ActionButtonStyle.SetValue(Button.ForegroundProperty, "Red");

但是它失败,并出现System.NullReferenceException。我还尝试了不同类型的值(Windows.UI.Xaml.Media.Brush和Windows.UI.Color),而不是“红色”,但都失败了,但有相同的例外。

我也尝试了以下方法:

var setter = mytip.ActionButtonStyle.Setters.OfType<Setter>().FirstOrDefault(X => X.Property == Control.ForegroundProperty);
setter.Value = "Red";

但是也失败了,但是有一个不同的例外-System.UnauthorizedAccessException

c# uwp
2个回答
0
投票

我曾经遇到过同样的问题,我设法做到了这一点:在App.xaml文件中添加样式

<Page.Resources>
  <SolidColorBrush x:Key="Orange" Color="#F49600" />
</Page.Resources>

然后您可以从代码中的任何位置访问它,就像这样

Button.Background = (SolidColorBrush)Application.Current.Resources["Orange"]; 

希望此帮助


0
投票

弄清楚了,这是最终成功的方法:

Style style = new Style(typeof(Button));
Setter setter = new Setter(Button.ForegroundProperty, new SolidColorBrush(Windows.UI.Colors.Red));
style.Setters.Add(setter);
mytip.ActionButtonStyle = style;
© www.soinside.com 2019 - 2024. All rights reserved.