背水一戰 Windows 10 (11) - 資源: CustomResource, ResourceDictionary, 加載外部的 ResourceDictionary 文件


[源碼下載]


背水一戰 Windows 10 (11) - 資源: CustomResource, ResourceDictionary, 加載外部的 ResourceDictionary 文件



作者:webabcd


介紹
背水一戰 Windows 10 之 資源

  • CustomResource
  • ResourceDictionary
  • 加載外部的 ResourceDictionary 文件



示例
1、演示“CustomResource”相關知識點
Resource/CustomResourceTest.cs

/*
 * 本例是一個自定義 CustomXamlResourceLoader,用於演示 CustomResource 的使用
 */

using Windows.UI.Xaml.Resources;

namespace Windows10.Resource
{
    // 如果要在 xaml 中使用 CustomResource,那么需要在 C# 端自定義一個 CustomXamlResourceLoader
    public class CustomResourceTest : CustomXamlResourceLoader
    {
        /// <summary>
        /// 返回 xaml 中的 CustomResource 請求的資源
        /// </summary>
        /// <param name="resourceId">xaml 端的 CustomResource 中的 ResourceKey</param>
        /// <param name="objectType">使用了 CustomResource 的對象類型</param>
        /// <param name="propertyName">使用了 CustomResource 的屬性名稱</param>
        /// <param name="propertyType">使用了 CustomResource 的屬性類型</param>
        /// <returns>返回指定的資源</returns>
        protected override object GetResource(string resourceId, string objectType, string propertyName, string propertyType)
        {
            return $"resourceId: {resourceId}, objectType: {objectType}, propertyName: {propertyName}, propertyType: {propertyType}";
        }
    }
}

Resource/CustomResourceDemo.xaml

<Page
    x:Class="Windows10.Resource.CustomResourceDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Resource"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <!--
                下面演示如何使用 CustomResource,相關的自定義資源類參見 CustomResourceTest.cs
            -->
            <TextBlock Margin="5" Text="{CustomResource}" />
            <TextBlock Margin="5" Text="{CustomResource Key1}" />
            <TextBlock Margin="5" Text="{CustomResource Key2}" />

        </StackPanel>
    </Grid>
</Page>

Resource/CustomResourceDemo.xaml.cs

/*
 * 演示“CustomResource”相關知識點
 */

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Resources;

namespace Windows10.Resource
{
    public sealed partial class CustomResourceDemo : Page
    {
        public CustomResourceDemo()
        {
            // 這是必須的,需要先要指定當前使用的自定義 CustomXamlResourceLoader 實例
            CustomXamlResourceLoader.Current = new CustomResourceTest();

            this.InitializeComponent();
        }
    }
}


2、演示“ResourceDictionary”相關知識點
Resource/ResourceDictionary1.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--
        以 ResourceDictionary 為根節點
    -->
    
    <Color x:Key="ColorRed">#FFFF0000</Color>
    <SolidColorBrush x:Key="BrushRed" Color="{ThemeResource ColorRed}" />
    
</ResourceDictionary>

Resource/ResourceDictionary2.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--
        以 ResourceDictionary 為根節點
    -->
    
    <Color x:Key="ColorGreen">#FF00FF00</Color>
    <SolidColorBrush x:Key="BrushGreen" Color="{ThemeResource ColorGreen}" />
    
</ResourceDictionary>

Resource/ResourceDictionary3.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--
        以 ResourceDictionary 為根節點
    -->
    
    <Color x:Key="ColorBlue">#FF0000FF</Color>
    <SolidColorBrush x:Key="BrushBlue" Color="{ThemeResource ColorBlue}" />

</ResourceDictionary>

Resource/ResourceDictionaryDemo.xaml

<Page
    x:Class="Windows10.Resource.ResourceDictionaryDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Resource"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        
        <!--
            FrameworkElement.Resources 就是一個 ResourceDictionary 對象
        
            1、在 ResourceDictionary 中可以一條一條地定義資源
            2、可以設置 ResourceDictionary 的 Source 屬性來引用一個以 ResourceDictionary 為根節點的 xaml 文件
            3、通過 MergedDictionaries 可以集成多個 ResourceDictionary
            4、通過 ThemeDictionaries 可以設置不同主題下的 ResourceDictionary,詳見:ThemeResourceDemo.xaml
        -->
        
        <ResourceDictionary>
            
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ResourceDictionary1.xaml" />
                <ResourceDictionary Source="ms-appx:///Resource/ResourceDictionary2.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Color x:Key="ColorOrange">#FFFFA500</Color>
            <SolidColorBrush x:Key="BrushOrange" Color="{ThemeResource ColorOrange}" />
            
        </ResourceDictionary>

    </Page.Resources>

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <TextBlock Name="textBlock1" Margin="5" Text="我是 TextBlock" Foreground="{StaticResource BrushRed}" />
            <TextBlock Name="textBlock2" Margin="5" Text="我是 TextBlock" Foreground="{StaticResource BrushGreen}" />
            <TextBlock Name="textBlock3" Margin="5" Text="我是 TextBlock" Foreground="{StaticResource BrushOrange}" />

            <!--
                演示如何通過 C# 端引入新的 ResourceDictionary 並使用其中的資源
            -->
            <TextBlock Name="textBlock4" Margin="5" Text="我是 TextBlock" />

            <!--
                演示如何獲取指定資源的值
            -->
            <TextBlock Name="lblMsg" Margin="5" TextWrapping="Wrap" />
            <Button Name="btnGetResourceValue" Margin="5" Content="獲取指定資源的值" Click="btnGetResourceValue_Click" />
            
        </StackPanel>
    </Grid>
</Page>

Resource/ResourceDictionaryDemo.xaml.cs

/*
 * 演示“ResourceDictionary”相關知識點
 */

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Windows10.Resource
{
    public sealed partial class ResourceDictionaryDemo : Page
    {
        public ResourceDictionaryDemo()
        {
            this.InitializeComponent();

            this.Loaded += ResourceDictionaryDemo_Loaded;
        }

        private void ResourceDictionaryDemo_Loaded(object sender, RoutedEventArgs e)
        {
            // 實例化一個 ResourceDictionary
            ResourceDictionary rd = new ResourceDictionary
            {
                Source = new Uri("ms-appx:///Resource/ResourceDictionary3.xaml", UriKind.Absolute)
            };

            // 將指定的 ResourceDictionary 集成到 Page.Resources 內的資源字典中
            this.Resources.MergedDictionaries.Add(rd);

            // 使用上面集成進來的資源字典中的資源
            textBlock4.Foreground = (SolidColorBrush)this.Resources["BrushBlue"];

            /*
             * 上面的例子演示的是如何處理指定的 FrameworkElement 中的資源
             * 如果需要處理 application 級的資源的話,可以通過 Application.Current.Resources 來獲取 application 級的資源(對應的 xaml 為 App.xaml)
             */
        }

        private void btnGetResourceValue_Click(object sender, RoutedEventArgs e)
        {
            // 獲取 application 級的指定資源的值
            lblMsg.Text = "SystemAccentColor: " + Application.Current.Resources["SystemAccentColor"].ToString();
            lblMsg.Text += Environment.NewLine;

            // 獲取指定 ResourceDictionary 中的指定資源的值
            lblMsg.Text += "Page.Resources 中的 ColorRed 的值: " + this.Resources["ColorRed"].ToString();
            lblMsg.Text += Environment.NewLine;

            // 獲取指定 ResourceDictionary 中的指定資源的值
            ResourceDictionary resourceDictionary1 = this.Resources.MergedDictionaries[0];
            lblMsg.Text += "Page.Resources.MergedDictionaries[0] 中的 ColorRed 的值: " + resourceDictionary1["ColorRed"].ToString();
        }
    }
}


3、演示如何加載並使用外部的 ResourceDictionary
Resource/RemoteResource.xaml

<Page
    x:Class="Windows10.Resource.RemoteResource"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Resource"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
            
        <Color x:Key="ColorOrange">#FFFFA500</Color>
        <SolidColorBrush x:Key="BrushOrange" Color="{ThemeResource ColorOrange}" />

    </Page.Resources>
    
    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <TextBlock Name="textBlock" Margin="5" Text="我是 TextBlock" Foreground="{StaticResource BrushOrange}" />

            <!--
                加載並使用遠程 ResourceDictionary 中的資源
            -->
            <Button Name="btnLoadRemoteResource" Content="加載並使用遠程 ResourceDictionary 中的資源" Margin="5" Click="btnLoadRemoteResource_Click" />

        </StackPanel>
    </Grid>
</Page>

Resource/RemoteResource.xaml.cs

/*
 * 演示如何加載並使用外部的 ResourceDictionary
 */

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using Windows.Web.Http;

namespace Windows10.Resource
{
    public sealed partial class RemoteResource : Page
    {
        // 需要加載的 ResourceDictionary 的 http 地址
        string resourceDictionaryUrl = "http://localhost:44914/xaml/ResourceDictionary.txt";

        public RemoteResource()
        {
            this.InitializeComponent();
        }

        private async void btnLoadRemoteResource_Click(object sender, RoutedEventArgs e)
        {
            // 下載遠程的 ResourceDictionary 文件
            HttpClient client = new HttpClient();
            string resourceDictionaryString = await client.GetStringAsync(new Uri(resourceDictionaryUrl, UriKind.Absolute));

            // 將字符串轉換為 ResourceDictionary 對象
            ResourceDictionary resourceDictionary = XamlReader.Load(resourceDictionaryString) as ResourceDictionary;

            // 將指定的 ResourceDictionary 集成到 Page.Resources 內的資源字典中
            this.Resources.MergedDictionaries.Add(resourceDictionary);

            // 使用遠程 ResourceDictionary 中的資源
            textBlock.Foreground = (SolidColorBrush)this.Resources["BrushGreen"];
        }
    }
}

http://localhost:44914/xaml/ResourceDictionary.txt

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Color x:Key="ColorGreen">#FF00FF00</Color>
    <SolidColorBrush x:Key="BrushGreen" Color="{ThemeResource ColorGreen}" />
    
</ResourceDictionary>



OK
[源碼下載]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM