简体   繁体   English

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

[英]How do I add a property to my Xamarin page that I can use in a default style?

I have added a BarTextColor property to my custom Xamarin page, like so: 我已经将BarTextColor属性添加到了自定义Xamarin页面,如下所示:

    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(); }
    }

But when I try to set a global style in my App.xaml like so: 但是,当我尝试在App.xaml中设置全局样式时,如下所示:

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

I get this error: 我收到此错误:

Property 'BarTextColor' must be a DependencyProperty to be set with a Setter 属性“ BarTextColor”必须是DependencyProperty,才能使用设置器设置

and this: 和这个:

The property "BarTextColor" is not a DependencyProperty. 属性“ BarTextColor”不是DependencyProperty。 To be used in markup, non-attached properties must be exposed on the target type with an accessible instance property "BarTextColor". 要在标记中使用,非附加属性必须使用可访问的实例属性“ BarTextColor”在目标类型上公开。 For attached properties, the declaring type must provide static "GetBarTextColor" and "SetBarTextColor" methods. 对于附加属性,声明类型必须提供静态的“ GetBarTextColor”和“ SetBarTextColor”方法。

What's going wrong? 怎么了 The Xamarin source on github all uses BindableProperty not DependencyProperty , so why can't I? github上的Xamarin源全部使用BindableProperty而不是DependencyProperty ,那为什么不能呢?

(I'm using Visual Studio Community 2013 with Xamarin 2.3.1, in case it matters) (如果需要,我将Visual Studio Community 2013与Xamarin 2.3.1结合使用)

I created BasePage.cs : 我创建了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); }
    }
}

Then created some ContentPage : 然后创建一些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();
    }
}

And declared Style in App.xaml : 并在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>

But I used Style by Key , because it didn't work w/o Key . 但是我使用Key by Style ,因为它在没有Key不起作用。 And on OnColorChanged I've got red color from Style . OnColorChanged我从Style获得了红色。

Edited 编辑

Or you could create base page for navigation: 或者,您可以创建基础页面进行导航:

public class BaseNavigationPage : NavigationPage
{
    public BaseNavigationPage(ContentPage root) 
        : base(root)
    {

    }

    public BaseNavigationPage()
    {
        BarBackgroundColor = Color.Red;
    }
}

And use it for Navigation: 并将其用于导航:

    public App()
    {
        InitializeComponent();

        MainPage = new CustomPropertySample.BaseNavigationPage(new CustomPropertySample.Page1());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为我的所有页面(如Zebble for Xamarin中的默认页面)制作一个母版页面? - How can I make a master page for all of my pages like default page in Zebble for Xamarin? 如何更改 DataGrid 标题的默认样式? - How can I change the default style of my DataGrid's headers? 如何在xamarin中将xib设置为应用程序的第一页? - How do I set an xib as the first page in my app in xamarin? 如何在我的C#字典中添加采用默认值的Python样式的Get()方法? - How do I add a Python-style Get() method which takes a default value to my C# dictionaries? 如何为Xamarin自定义键盘使用android:keyOutputText? - How do I use android:keyOutputText for my Xamarin custom keyboard? 如何在ResourceDictionary中设置可用于背景属性的主要颜色 - How do I set a primary color that I can use for my background property in ResourceDictionary 如何将标签绑定到Xamarin中的属性? - How do I bind a Label to a property in Xamarin? 如果我的属性是数据库中的字符串(xamarin.forms),我如何将颜色传递给我的 UI - How can i pass a color to my UI if my property is a string in my database (xamarin.forms) 如何在.aspx网页上使用默认的siteLayout? - How can I use my default siteLayout with my .aspx webpage? 如何更改原始选项卡页面栏的样式(android with xamarin.forms)? - How can I change style of the original tabbed page bar (android with xamarin.forms)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM