2015-05-18 103 views
2

我正在使用資源字典了很多,但它是繁瑣的,不得不將它們從一個Xaml複製並粘貼到另一個Xaml以使它們全部更新。我看到完成的唯一方法是繼承一個樣式類,但我不喜歡在C#中聲明屬性的方式(我更喜歡XAML的更多可視化樹語法)。有沒有辦法讓一個XAML從另一個繼承?或者我可以調用每個Xamarin.Forms.ContentPage創建的從StylePage繼承的LoadXaml()方法?XamarinForms XAML:如何從ResourceDictionaries等其他XAML文件繼承屬性?

這是我的風格XAML頁面:

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Demo.StylePage"> 
    <!-- Shared Properties --> 
    <ContentPage.Resources> 
     <ResourceDictionary> 
      <!-- TitleBar Styles --> 
      <Style x:Key="TitleBarStyle" TargetType="StackLayout"> 
       <Setter Property="BackgroundColor" Value="#5db3d6"/> 
       <Setter Property="Padding" Value="15,6"/> 
       <Setter Property="HeightRequest" Value="44"/> 
      </Style> 

      <Style x:Key="TitleBarHeaderStyle" TargetType="Label"> 
       <Setter Property="TextColor" Value="White"/> 
       <Setter Property="VerticalOptions" Value="CenterAndExpand"/> 
<!--    <Setter Property="FontSize" Value="18"/>--> 
       <Setter Property="HorizontalOptions" Value="FillAndExpand"/> 
      </Style> 

      <Style x:Key="EmptyFrameStyle" TargetType="Frame"> 
       <Setter Property="HasShadow" Value="false"></Setter> 
       <Setter Property="BackgroundColor" Value="Transparent"></Setter> 
       <Setter Property="Padding" Value="0,0"></Setter> 
      </Style> 
     </ResourceDictionary> 
    </ContentPage.Resources> 
</ContentPage> 

我怎麼可能從另一個文件裝載該資源字典?謝謝你的時間,謝爾蓋。

+0

http://developer.xamarin.com/guides/c ross-platform/xamarin-forms/working-with/styles /#Global_Styles_using_Application_Resources –

+0

我已經檢查過了,它使用C#創建資源字典,正是我不想要的。我正在尋找一種方法來從XAML導入資源字典 –

回答

1

您可以替換您的應用程序。cs與所示的XAML/cs組合here

App.xaml中定義的資源將適用於您的應用中加載的每個視圖。

確保您的App.xaml文件具有「嵌入式資源」構建操作。另外,您可以從UI添加它作爲一個ContentView with XAML,然後用Application

enter image description here

更換ContentView的App.xaml

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

App.xaml.cs

public partial class App : Application 
{ 
    public App() 
    { 
     InitializeComponent(); 
     MainPage = YourContentPage(); // change as required 
    } 
} 
+0

我要試試,謝謝 –

+0

我得到一個錯誤的InitializeComponent()行說:「它在當前上下文中不存在「。你是怎麼做的? –

+0

您是否使.xaml成爲嵌入式資源?再次檢查答案,我添加了有關如何避免此錯誤的建議。嘗試在解決方案上運行「乾淨」或刪除bin和obj文件夾,然後嘗試重建 –

3

ContentPages支持繼承。您可以隨時定義一個BasePage,在那裏實現您的資源,然後讓每個新的ContentPage繼承它。儘管我從來沒有通過繼承xaml來完成這項工作,但我曾經在BasePage的代碼隱藏中共享分析,日誌記錄等(如下所示)。

我會建議嘗試一種類似的方法,並在BasePage中實例化您的資源字典。

BaseFormPage:

public class BasePage : ContentPage 
    { 
     public BasePage() : base() { } 

     protected override void OnAppearing() 
     { 
      base.OnAppearing(); 

      AnalyticsApi.LogPageView(); 
      AnalyticsApi.LogEvent(Title + " Page Loaded"); 

     } 
    } 

ChildFormPage:

<?xml version="1.0" encoding="UTF-8"?> 
<local:BasePage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:local="clr-namespace:MyApp;assembly=MyApp" 
      x:Class="MyApp.LoginPage" 
      Title="{x:Static local:Strings.SignIn}" 
      BackgroundColor="{x:Static local:Colors.ThemeQuaternary}"> 
    <StackLayout> 
     ... 
    </StackLayout> 
</local:BasePage> 
+1

我嘗試過這種方法,但嘗試從BaseFormPage繼承時出現錯誤,稱「部分聲明不能指定不同的基類」。有任何想法嗎? –

+0

我剛剛意識到我寫BaseForm而不是BasePage(子頁面期望)。您可能還需要進入每個ContentPage的生成文件,以便爲其啓動一個「踢開始」,以便從哪個類繼承。即從生成的文件中刪除:ContentPage並將其替換爲:BasePage – eckyzero

+1

我有點遺失,您能否提供一個非常簡單的示例,其中一個ChildPage ContentPage類是從BasePage ContentPage類繼承的,並且可以訪問它的資源字典?對不起...我正在嘗試,並在我的項目上做了很多版本,但沒有工作 –

0

Xamarin.Forms,你可以聲明每個VisualElement資源。然後您可以使用{StaticResource}{DynamicResource}來訪問這些資源。

應用的解析規則大致如下:

  • {StaticResource}
    • 穿這VisualElement.Resources或在同一XAML文件中的任何其父
    • 然後看看在Application.Current.Resources
  • for {DynamicResource}
    • 看這個VisualElement.Resources或任何其母公司,即使在同一個XAML文件中定義的那些,最多到Application.Resources
    • 注意{DynamicResources}支持後添加資源,或者改變父母

要回答你的問題,你確實可以在XAML的基類中定義你的樣式並使用XAML繼承,或者將所有的資源添加到你的Application類中。如果您的應用程序沒有XAML文件,只需添加一個,然後從Application構造函數中調用InitializeComponents()

+0

你能提供一個簡單的例子來說明如何將一個...「BaseStylePage.xaml」加載到ChildPage.xaml中嗎?我試圖這樣做,但我得到「部分聲明不能指定不同的基類」當試圖繼承 –