2016-08-22 93 views
1

我添加了一個BarTextColor屬性,我的自定義頁面Xamarin,像這樣:如何將屬性添加到我可以在默認樣式中使用的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(); } 
    } 

但是,當我嘗試設置一個全球性的風格,我的App.xaml像這樣:

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

我得到這個錯誤:

Property 'BarTextColor' must be a DependencyProperty to be set with a Setter

這:

The property "BarTextColor" is not a DependencyProperty. To be used in markup, non-attached properties must be exposed on the target type with an accessible instance property "BarTextColor". For attached properties, the declaring type must provide static "GetBarTextColor" and "SetBarTextColor" methods.

怎麼回事? github上的Xamarin源碼全部使用BindableProperty而不是DependencyProperty,所以爲什麼我不能?

(我使用Visual Studio 2013社區與Xamarin 2.3.1,如果它事項)

回答

2

我創建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> 

但我使用StyleKey,因爲它沒有工作w/o 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()); 
    } 
+0

謝謝,但我需要在全球範圍內的風格我的網頁。我不能只在每個頁面上設置明確的樣式。所以我需要沒有Key的版本。 – Simon

+0

@Simon增加了一個建議。 –

+0

謝謝。這就是我目前正在做的事情,但隨着頁面數量的增加,它不可持續。我真的需要設置全球風格。 – Simon

相關問題