WPF对象级资源的定义与查找
文章概述:
本演示介紹了怎樣定義WPF對象級的資源,并通過XAML代碼和C#訪問和使用對象級資源。
相關(guān)下載(代碼、屏幕錄像):http://pan.baidu.com/s/1hqvJNY8
在線播放:http://v.youku.com/v_show/id_XODA1NTU2Mzky.html
溫馨提示:假設(shè)屏幕錄像和代碼不能正常下載,可站內(nèi)留言。或發(fā)郵件到524130780@QQ.COM
一、完整的定義和使用資源
<Window x:Class="Demo008.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:sys="clr-namespace:System;assembly=mscorlib"Title="Resource" FontSize="16" Loaded="Window_Loaded"><!--完整的寫法--><Window.Resources><ResourceDictionary><sys:String x:Key="str">沉舟側(cè)畔千帆過,病樹前頭萬木春。</sys:String> <sys:Double x:Key="dbl">3.1415926</sys:Double> </ResourceDictionary> </Window.Resources> <StackPanel> <TextBlock Margin="5" Text="{StaticResource ResourceKey=str}" /> </StackPanel> </Window>
二、簡寫的資源定義和使用
<Window x:Class="Demo008.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:sys="clr-namespace:System;assembly=mscorlib"Title="Resource" FontSize="16" Loaded="Window_Loaded"><!--簡寫--><Window.Resources><sys:String x:Key="str">沉舟側(cè)畔千帆過。病樹前頭萬木春。
</sys:String> <sys:Double x:Key="dbl">3.1415926</sys:Double> </Window.Resources> <StackPanel> <TextBlock x:Name="TextBlock1" Margin="5" Text="{StaticResource str}" /> </StackPanel> </Window>
三、代碼查找資源
通常的做法例如以下所看到的:
string text = this.FindResource("str").ToString(); this.TextBlock1.Text = text;
假設(shè)知道資源位于哪個對象的資源字典中能夠使用例如以下的方式直接訪問:
string text = this.Resources["str"].ToString(); this.TextBlock1.Text = text;
轉(zhuǎn)載于:https://www.cnblogs.com/llguanli/p/8880117.html
總結(jié)
以上是生活随笔為你收集整理的WPF对象级资源的定义与查找的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端笔记
- 下一篇: asp.net ajax控件工具集 Au