[英]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.