styles


```csharp
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:appstyles="clr-namespace:AppForm.Styles"
             x:Class="AppForm.App">
    <!--
        Define global resources and styles here, that apply to all pages in your app.
    -->
    <Application.Resources>
        <ResourceDictionary Source="Styles/ColorStyle.xaml">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles/ButtonStyle.xaml"></ResourceDictionary>
                <appstyles:TabStyle></appstyles:TabStyle>
            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>        
    </Application.Resources>
</Application>

```csharp
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AppForm.Styles.TabStyle">
    
</ResourceDictionary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace AppForm.Styles
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TabStyle : ResourceDictionary
    {
        public TabStyle()
        {
            InitializeComponent();
        }
    }
}
Step 1:
Create a ContentPage or ContentView file in the solution and give the file name like this. -> MyInnerResourceDictionary.xaml.
	1.1: Remove the code behind file.
	1.2: in the XAML file change the name of the base class from ContentView or ContentPage to ResourceDictionary. 
	In addition, remove the x:Class attribute from the root tag of the file.

Step 2:
Consume Resource Dictionary file in the MainPage.xaml [Check Source Code].

If we want to develop external style inside the .Net standard Class library.

Step 3:
Create .Net Standard Class Library. 
	3.1 Select the solution and Unload the Project.
	3.2 Select the solution and Edit Project file.
	3.3 Add the following reference in the .csproj file project.
	<ItemGroup>
		<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
		<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
	</ItemGroup>
    3.4 
	Create a ContentPage or ContentView file in the solution and give the file name like this. -> MyOuterResourceDictionary.xaml [Check Source Code]
		Do not remove the code behind file.
		in the XAML file change the name of the base class from ContentView or ContentPage to ResourceDictionary. 
		add x:Class attribute from the root tag of the file.

Step 4:
Add the project refereance in Main the Solution.

Step 5:
Go to MainPage.xaml file.
	5.1 
	Add the external style referance in root tag
	xmlns:mystyles="clr-namespace:StylesLib.MyStyles;assembly=StylesLib"

	5.2
	Using Merge Dictionary call the external style referance
	 <ContentPage.Resources>
        <!--Internal Style-->
        <ResourceDictionary Source="RootStyles/MyInnerResourceDictionary.xaml">
            <!--For External Library-->
            <ResourceDictionary.MergedDictionaries>
                <mystyles:MyOuterResourceDictionary></mystyles:MyOuterResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </ContentPage.Resources>
<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>netstandard2.0</TargetFramework>
		<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
	</PropertyGroup>

	<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
		<DebugType>portable</DebugType>
		<DebugSymbols>true</DebugSymbols>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
		<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
	</ItemGroup>

	<ItemGroup>
	  <ProjectReference Include="..\..\StylesLib\StylesLib.csproj" />
	</ItemGroup>

	<ItemGroup>
		<EmbeddedResource Update="RootStyles\MyInnerResourceDictionary.xaml">
			<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
		</EmbeddedResource>
		<EmbeddedResource Update="MyStyles\MyOuterResourceDictionary.xaml">
			<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
		</EmbeddedResource>
	</ItemGroup>
</Project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Farmwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值