Ramesh
Ramesh

Reputation: 422

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.

Upvotes: 1

Views: 1850

Answers (2)

Tom
Tom

Reputation: 1749

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>

Upvotes: 4

Abdul Gani
Abdul Gani

Reputation: 689

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"   />

Upvotes: 1

Related Questions