如何向Xamarin页面添加可以以默认样式使用的属性?

本文关键字:样式 默认 属性 Xamarin 添加 | 更新日期: 2023-09-27 18:03:26

我已经为我的自定义Xamarin页面添加了一个BarTextColor属性,如下所示:

    public static readonly BindableProperty BarTextColorProperty = BindableProperty.Create("BarTextColor", typeof(Color), typeof(MyCustomPage), Color.Default);
    public Color BarTextColor
    {
        get { return (Color)GetValue(BarTextColorProperty); }
        set { SetValue(BarTextColorProperty, value); UpdateInnerViews(); }
    }

但是当我尝试在App.xaml中设置全局样式时,就像这样:

    <Style TargetType="myapp:MyCustomPage">
        <Setter Property="BarTextColor" Value="{StaticResource BarTextColor}"/>
    </Style>

我得到这个错误:

属性'BarTextColor'必须是一个DependencyProperty来设置Setter

:

属性BarTextColor不是一个DependencyProperty。用于…标记、非附加属性必须在目标类型上公开带有一个可访问的实例属性"BarTextColor"。为附加属性,声明类型必须提供静态的"GetBarTextColor"和SetBarTextColor方法。

怎么了?github上的Xamarin源都使用BindableProperty而不是DependencyProperty,所以为什么我不能?

(我使用的是Visual Studio Community 2013与Xamarin 2.3.1,以防万一)

如何向Xamarin页面添加可以以默认样式使用的属性?

我创建了BasePage.cs:

public class BasePage : ContentPage
{
    public static readonly BindableProperty BarTextColorProperty =
        BindableProperty.Create(nameof(BarTextColor),
                                typeof(Color),
                                typeof(BasePage),
                                Color.White,
                                propertyChanged: OnColorChanged);
    private static void OnColorChanged(BindableObject bindable, object oldValue, object newValue)
    {
    }
    public Color BarTextColor
    {
        get { return (Color)GetValue(BarTextColorProperty); }
        set { SetValue(BarTextColorProperty, value); }
    }
}

然后创建一些ContentPage:

<local:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:SuperForms.Samples.CustomPropertySample;assembly=SuperForms.Samples"
                x:Class="SuperForms.Samples.CustomPropertySample.Page1"
                Style="{StaticResource BasePageStyle}">
    <Label Text="Alloha" VerticalOptions="Center" HorizontalOptions="Center" />
</local:BasePage>
public partial class Page1 : BasePage
{
    public Page1()
    {
        InitializeComponent();
    }
}

并在App.xaml中声明Style:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:customPropertySample="clr-namespace:SuperForms.Samples.CustomPropertySample;assembly=SuperForms.Samples"
         x:Class="SuperForms.Samples.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="BasePageStyle" TargetType="customPropertySample:BasePage">
                <Setter Property="BarTextColor" Value="#ff0000"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

但我使用Style by Key,因为它没有Key不起作用。在OnColorChanged上,我使用了Style的红色。

或者您可以创建导航基页:

public class BaseNavigationPage : NavigationPage
{
    public BaseNavigationPage(ContentPage root) 
        : base(root)
    {
    }
    public BaseNavigationPage()
    {
        BarBackgroundColor = Color.Red;
    }
}

并将其用于导航:

    public App()
    {
        InitializeComponent();
        MainPage = new CustomPropertySample.BaseNavigationPage(new CustomPropertySample.Page1());
    }