【问题标题】:Apply style to control in custom renderer将样式应用于自定义渲染器中的控件
【发布时间】: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


【解决方案1】:

我已经填写了一份错误报告。它应该在即将发布的版本之一中得到修复 (> XF 2.3.3.166-pre4)。

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 2017-11-06
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2018-12-20
    • 2012-10-16
    相关资源
    最近更新 更多