2015-05-19 98 views

回答

4

據我所知在Xamarin.Forms中沒有內置的主題支持,但是你可以實現一個。 您需要這樣做: 1.使用相同的樣式列表將多個ResourceDictionaries添加到App.xaml。

<Application 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="ThemeTest.App"> 
    <Application.Resources> 
    </Application.Resources> 
    <ResourceDictionary x:Name="Default"> 
    <Style x:Key="labelStyle" TargetType="Label"> 
     <Setter Property="TextColor" Value="Green" /> 
    </Style> 
    </ResourceDictionary> 
    <ResourceDictionary x:Name="Second"> 
    <Style x:Key="labelStyle" TargetType="Label"> 
     <Setter Property="TextColor" Value="Yellow" /> 
    </Style> 
    </ResourceDictionary> 
</Application> 

2.在你的App.xaml.cs中添加代碼以切換樣式。

public partial class App : Application 
{ 
    public App() 
    { 
     InitializeComponent(); 
     SetDefaultStyle(); 
     MainPage = new TestPage(); 
    } 

    public void SetDefaultStyle() 
    { 
     Resources = Default; 
    } 

    public void SetSecondStyle() 
    { 
     Resources = Second; 
    } 
} 

3.在XAML中使用DynamicResource標記擴展引用您的樣式。

<Label Text="Test text" Style="{DynamicResource labelStyle}" /> 

我創建了示例應用程序,您可以找到here。 殼牌您有任何問題歡迎您提問。

相關問題