资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件
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>
资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件的更多相关文章
- 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件
[源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...
- UNITY_资源路径与加载外部文件
UNITY_资源路径与加载外部文件 https://www.tuicool.com/articles/qMNnmm6https://blog.csdn.net/appppppen/article/de ...
- 如何点击按钮后在加载外部的Js文件
或许有朋友遇到过,想等自己点击按钮之后才执行某一个js文件,那么,你运气好,看到了我的代码了哈哈, <html> <head> <title></title& ...
- 使用js加载器动态加载外部Javascript文件
原文:http://www.cnblogs.com/xdp-gacl/p/3927417.html 今天在网上找到了一个可以动态加载js文件的js加载器,具体代码如下: JsLoader.js var ...
- JavaScript学习总结(十九)——使用js加载器动态加载外部Javascript文件
今天在网上找到了一个可以动态加载js文件的js加载器,具体代码如下: JsLoader.js 1 var MiniSite=new Object(); 2 /** 3 * 判断浏览器 4 */ 5 M ...
- Silverlight实用窍门系列:2.Silverlight动态加载外部XML指定地址的WebService---(动态加载外部XML文件中指定的WebService地址)【附带实例源码】
接上节所讲的,Silverlight可以加载外部的XML文件里面的内容,那么我们可不可以在外部XML里面配置一个WebService地址,并且以此加载这个地址来动态加载WebService呢?这样子就 ...
- Error #2044: 未处理的 IOErrorEvent:。 text=Error #2035: 找不到 URL这是flash加载外部资源时有时会遇到的问题,对于此问题解决如下
导致这个错误的主要原因是未添加IOErrorEvent事件监听,或者添加了监听,但是加载时使用了unload() 参考资料: http://blog.csdn.net/chjh0540237/arti ...
- 转载:Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式
Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式 出自:http://www.cnblogs.com/top5/archive/2012/08/04/2623464.html 关 ...
- iframe 加载外部资源,显示隐藏loading,onload失效
在项目中使用iframe 来加载外部资源,需要在iframe请求外部资源的时候,需要显示一个loading,在加载完成后,将这个loading隐藏掉,刚开始看到W3C中 iframe有一个 onloa ...
随机推荐
- php的一些小细节
1.今天看见 $arr3 = array_filter($arr, create_function('$v', 'return strlen($v);')); 作用就是去掉为空的元素,其实当callb ...
- h1/title,b/strong,i/em 的区别
< strong > 表示html页面上的强调(emphasized text), < em > 表示句子中的强调(即强调语义) 1.b和strong的区别 盲人朋友使用阅读设 ...
- QT 常用控件二
QT提供QHBoxLayout类.QVBoxlayout类及QGridLayout类等的基本布局管理,分别是水平排列布局,垂直排列布局和网格排列布局 addWidget()方法用于向布局中加入需要布局 ...
- Beta版本项目展示要求
项目评审的定在1月5日上午9:00在新主楼D225进行. 在Beta阶段项目评审会上, 每个团队有12分钟展示时间,10分钟问答和机动时间,我们的展示也不需要PPT,大家把要展现的东西写成博客(可以有 ...
- http缓存提高性能
秋招也算是正式结束了,现在整理一下笔记,当作巩固一下知识,也希望这个对大家有帮助 http 缓存 和 cdn 缓存可以说是面试必问的问题,竟然是必问的问题,那就总结全面一点- http缓存机制 缓存分 ...
- 高性能JavaScript 达夫设备
前言 在<高性能JavaScript>一书的第四章算法和流程控制中,提到了减少迭代次数加速程序的策略—达夫设备(Duff's device).达夫设备本身很好理解,但是其效果是否真的像书中 ...
- .NET技术在中国为什么老被人嫌弃
这个话题有点自黑的意思,我从.NET 1.1开始玩.NET,到现在已经11年了,我是看着.NET成长起来,在中国壮大的,也见证了近几年.NET被各种嫌弃,其实说到底还是中国的架构师太少,我是说真正懂行 ...
- <实训|第六天>偷偷让新手的Linux无限重启附linux主机名称不是随便乱改的!
先说个事情:这几天我正在忙一个项目的设计,8月1号之前要弄出来,所以每天都要弄到很晚,可能更新就有点跟不上了,不过我如果有时间的话,我就更新,没时间的话,我会在8月1号之后统一更新出来,希望大家谅解! ...
- web安全——数据库(mysql)
简介 数据安全是现在互联网安全非常重要一个环节.而且一旦数据出现问题是不可逆的,甚至是灾难性的. 有一些防护措施应该在前面几个博文说过了,就不再赘述.比如通过防火墙控制,通过系统的用户控制,通过web ...
- 从Evernote迁移到Wiz
实在受不了evernote没完没了的弹出广告和让升级账号,我不过就是想安安静静的记个笔记,真不想看您的广告好吧.于是无奈这下,只能选择转换到别的笔记工具阵营. 由于以前一直听说OneNote是仅次于E ...