WPF對象級資源的定義與尋找,wpf對象定義尋找

來源:互聯網
上載者:User

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");

}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.