【发布时间】:2017-02-20 01:36:45
【问题描述】:
我想将样式应用于控件。这就是风格
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="BackgroundColor" Color="Yellow" />
<Style TargetType="Button" x:Name="myNewButtonStyle">
<Setter Property="Background" Value="{StaticResource BackgroundColor}" />
</Style>
</ResourceDictionary>
</Application.Resources>
,可以在 App.xaml(UWP 项目)中找到。这是自定义渲染器:
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (this.Element != null)
{
var style = Windows.UI.Xaml.Application.Current.Resources["myNewButtonStyle"] as Windows.UI.Xaml.Style;
this.Control.Style = style;
}
}
这个想法是基于this answer。但是没有应用样式。在代码中设置背景颜色确实有效:
this.Control.BackgroundColor = new SolidColorBrush(Windows.UI.Colors.Yellow);
如何将样式应用于自定义渲染器中的控件?
【问题讨论】:
-
-
Setter 属性可能是“BackgroundColor”,而不仅仅是背景。试试看
-
元素“按钮”上的未知成员“背景颜色”。 成员“BackgroundColor”无法识别或无法访问。
-
您的自定义渲染器派生自哪个类?渲染器是为普通的 Button 控件构建的吗?
-
@MZetko:控件是
Xamarin.Forms.Button的子类形式,渲染器是Xamarin.Forms.Platform.UWP.ButtonRenderer的子类。在这个例子中,它应该是一个普通的按钮控件,但我想为不同的控件设置样式。按钮仅在其中。
标签: xamarin xamarin.forms uwp windows-10-universal