2016-08-22 2 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.

Что случилось? Источник Xamarin на github все использует BindableProperty не DependencyProperty, так почему я не могу?

(я использую Visual Studio Community 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(); 
    } 
} 

И объявлено Style в App.xaml:

<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 от Key, потому что он не работал без Key. И на OnColorChanged У меня красный цвет от Style.

Edited

Или вы можете создать базовую страницу для навигации:

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

Спасибо, но мне нужно глобально стилю мои страницы. Я не могу просто задать явный стиль на каждой странице. Поэтому мне нужна версия без ключа. – Simon

+0

@Simon Добавлено еще одно предложение. –

+0

Спасибо. Это то, что я делаю в данный момент, но это не устойчиво, поскольку количество страниц, которые у меня есть, увеличивается. Мне действительно нужно установить глобальные стили. – Simon

Смежные вопросы