在獨立的檔案中定義WPF資源,獨立定義wpf資源
一、文章概述
本示範介紹如何在單獨的檔案中定義WPF資源,並在需要的地方調用相關資源檔。
相關下載(代碼、螢幕錄影):http://pan.baidu.com/s/1sjO7StB
線上播放:http://v.youku.com/v_show/id_XODExODg0MzIw.html
溫馨提示:如果螢幕錄影和代碼不能正常下載,可站內留言,或發郵件到524130780@QQ.COM
二、首先在單獨的檔案中定義資源
XAML代碼如下所示:
<ResourceDictionary 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"> <sys:String x:Key="text">路漫漫其修遠兮,吾將上下而求索!</sys:String></ResourceDictionary>
三、在使用資源的地方串連到獨立的資源檔上
XAML代碼如下:
<Window x:Class="Demo009.MainWindow" xmlns="http://sche1mas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Resource Files" FontSize="30"> <Window.Resources> <ResourceDictionary Source="ShinyRed.xaml" /> </Window.Resources> <StackPanel> <TextBlock Text="{StaticResource text}" /> </StackPanel></Window>
按照如上的代碼操作,就算是搞定了!
wpf中,怎引用其他xaml檔案中的Resources
把樣式資源寫到APP.XAML中。定義資源名稱,在使用時綁定就可以了。
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");
}