简体   繁体   中英

What is the way to include styles from other XAML file using Xamarin Forms latest update

I have defined styles in my Xamarin Forms app seperately in file Style.xaml. Now I would like to include all styles present in Style.xaml in my Home.xaml.

Please let me know the solution with respect to latest Xamarin Forms update as the earlier solution working with earlier xamarin forms version 3.0 does not work with latest update 3.4.

I will not be able to define styles in my App.xaml as I am following modules concept using PRISM framework.

What you want to do is create a ResourceDictionary file, then reference it in both Style.xaml and Home.xaml . There is an example over at the Xamarin Forms Docs for ResourceDictionary .

Fundamentally, you create your ResourceDictionary like so:

<ResourceDictionary 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="ResourceDictionaryDemo.MyResourceDictionary">
    <Color x:Key="PrimaryColor">#2196F3</Color>
</ResourceDictionary>

and save it in a file called MyResourceDictionary.xaml . Then you call it in another XAML file like so:

<ContentPage ...>
    <ContentPage.Resources>

        <!-- Place other resources here -->

        <ResourceDictionary Source="MyResourceDictionary.xaml" />

    </ContentPage.Resources>

    <Label
        Text="MyLabel"
        TextColor="{StaticResource PrimaryColor}"/>
    ...
</ContentPage>

If you are using Cascading style sheet (CSS), you can use like this in each Xaml page.

<ContentPage.Resources>
    <StyleSheet Source="/Resx/Css/Styles.css" />
</ContentPage.Resources>

and in the controls, you can call the styles like this,

<Button Text="Login" BorderRadius="28" Command="{Binding UserLoginCommand}" VerticalOptions="Center" StyleClass="blueButton"   />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM