WPF對象級資源的定義與尋找,wpf對象定義尋找
文章概述:
本示範介紹了如何定義WPF對象級的資源,並通過XAML代碼和C#訪問和使用對象級資源。
相關下載(代碼、螢幕錄影):http://pan.baidu.com/s/1hqvJNY8
線上播放:http://v.youku.com/v_show/id_XODA1NTU2Mzky.html
溫馨提示:如果螢幕錄影和代碼不能正常下載,可站內留言,或發郵件到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">沉舟側畔千帆過,病樹前頭萬木春。</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">沉舟側畔千帆過,病樹前頭萬木春。</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;
如果知道資源位於哪個對象的資源字典中可以使用如下的方式直接存取:
string text = this.Resources["str"].ToString();this.TextBlock1.Text = text;
我要搶第一
你已經不行了,只搶道了第三
WPF中的xaml引用不同項目間資源的問題
Thread t1 = null;
Thread t2 = null;
private void Form1_Load(object sender, EventArgs e)
{
t1 = new Thread(new ThreadStart(T1));
t1.Start();
t2 = new Thread(new ThreadStart(T2));
t2.Start();
}
private void button1_Click(object sender, EventArgs e)
{
if (t1.ThreadState == ThreadState.Aborted||t1.ThreadState==ThreadState.Stopped)
{
MessageBox.Show("線程1結束了");
t1.Abort();
}
if (t2.ThreadState == ThreadState.Aborted||t1.ThreadState==ThreadState.Stopped)
{
MessageBox.Show("線程2結束了");
t2.Abort();
}
}
private void T1()
{
MessageBox.Show("進程1");
}
private void T2()
{
MessageBox.Show("進程2");
}