2016-05-12 64 views
0

我正在用Xamarin Studio在Mac機器上使用PCL創建我的第一個xamarin表單應用程序。 我讀了很多文章來創建應用程序的全局風格,但我無法訪問App.xaml.cs文件中聲明應用程序內部任何文件的樣式。我提到了我使用的一些代碼。或者如果有人有其他選擇讓它變得容易或無論如何可能,請給我建議。提前致謝。如何爲Xamarin表單應用程序創建全局樣式

App.xaml.cs-

<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DinePerfect.App"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <Style x:Key="btnStyle" TargetType="Button"> 
       <Setter Property="HorizontalOptions" Value="Center" /> 
       <Setter Property="VerticalOptions" Value="CenterAndExpand" /> 
       <Setter Property="BorderColor" Value="Lime" /> 
       <Setter Property="BorderRadius" Value="5" /> 
       <Setter Property="BorderWidth" Value="5" /> 
       <Setter Property="WidthRequest" Value="200" /> 
       <Setter Property="TextColor" Value="Teal" /> 
      </Style> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

呼叫風格在其他文件 -

<Button Text="Login" Style="{StaticResource btnStyle} TextColor="Black" BackgroundColor="#9C661F" Clicked="btnLoginClicked" /> 

回答

1

你確實需要設置BACKGROUNDCOLOR在樣式藏漢使邊框顏色的工作。

From the Button.BorderColor Documentation

此屬性,如果Button.BorderWidth設置爲0,在Android上,除非VisualElement.BackgroundColor設爲非默認的顏色這個屬性不會有效果沒有影響。

風格:

<ResourceDictionary> 
    <Style x:Key="btnStyle" TargetType="Button"> 
    <Setter Property="HorizontalOptions" Value="Center" /> 
    <Setter Property="VerticalOptions" Value="CenterAndExpand" /> 
    <Setter Property="BackgroundColor" Value="Black" /> 
    <Setter Property="BorderColor" Value="Lime" /> 
    <Setter Property="BorderRadius" Value="5" /> 
    <Setter Property="BorderWidth" Value="5" /> 
    <Setter Property="WidthRequest" Value="200" /> 
    <Setter Property="TextColor" Value="Teal" /> 
    </Style> 
</ResourceDictionary> 

使用XAML中:

<Button Text="Login" Style="{StaticResource btnStyle}" /> 

這對我的作品。

編輯: Example Project

+0

嗨@Marius - 我不知道,但我掙扎着它從兩天,我收到錯誤,StaticResource btnStyle沒有找到。如果我在每個單獨的文件中聲明上面的樣式,我會得到它。你能告訴我你遵循了什麼程序嗎? – Dipak

+0

嗨,我添加了一個項目鏈接,我在那裏複製你的問題。你可以發佈一個完整的XAML頁面,你引用的風格?我不知道爲什麼這不適合你。 –

5

看起來像,而你的App類轉換爲XAML爲基礎的,你已經錯過了在構造函數中調用InitializeComponent();

2

我想你忘了添加InitializeComponent();在你的App.cs中。當你有一個xaml代碼時,你必須調用InitializeComponent();在構造函數中。

相關問題