如果你打算使用winfx extensions to visual studio 2005来开发xaml和wpf程序,那么你需要在安装microsoft windows sdk前安装visual studio 2005。为此它的正确安装顺序为:
1. .net framework 3.0
2. visual studio 2005
3. microsoft windows sdk
4. winfx extensions to visual studio 2005
最后在visual studio 2005中通过"文件"-"新建项目"命令,在弹出的"新建项目"对话框中有一个项目类型列表,选择window(winfx),这样我们就可以使用这些模板来开发相应类型的wpf程序了。
如果我们使用Visual Studio 2005来开发一个XAML程序,那与上述的手动创建XAML程序过程来说是相对便捷的。你可以直接选择一个WinFX模板来创建特定类型的WPF程序,Visual Studio会自动为该WPF程序创建相应的项目文件和应用程序清单。现在我们可以创建的WPF程序类型有如下的四种:
1. WinFX Wndows Application
2. WinFX Web Browser Application
3. WinFX Service Library
4. WinFX Customer Control Library
然而,针对WinFX开发的Visual Studio版本还没有正式推出,所以Visual Studio 2005缺乏对WinFX程序开发这方面的一些完善支持,为此要编辑XAML和相应的代码后置文件(.xaml.cs)时,有时还是需要开发人员手动地进行编辑。
在这里假设你已经安装好了运行XAML所需的软件环境,现在我们来使用Visual Studio 2005创建一个WPF程序。至于是选择WinFX Wndows Application模板还是WinFX Web Browser Application模板取决于你所要创建的是宿主于浏览器的程序还是窗体程序。现在我们在"新建项目"对话框中的WinFX Web Browser Application模板,输入该WPF程序的项目名称,并选择好程序保存在硬盘上的位置,点击"确定"按钮,Visual Studio就自动为该WPF程序生成默认的XAML和代码后置文件。
提示:无论你是否使用Visual Studio,在创建一个WinFX Wndows Application和一个WinFX Web Browser Application时,各自的.xaml文件代码还是存在一些细微差别的。在一个Browser Application中,默认起始页的根元素为Page。而一个Wndows Application中,默认起始页的根元素为Window。而Window类是无法在Browser Application中使用的,这是因为运行在浏览器中的WPF程序是在部分信任的沙箱内操作(沙箱能够防止客户端计算机受到恶意应用程序的侵害),为此它没有打开一个新Windows窗体的权限。
在解决方案资源管理器窗口中,可以看到Visual Studio为当前WPF程序所自动生成的XAML和代码后置文件。由于在Visual Studio中会自动生成相应的项目文件,使用我们不必详细了解该Browser Application在生成时其底层的实现细节。但是由于WPF程序和应用程序定义文件和相关的XAML页面文件戚戚相关,为此最好还是详细了解下各个文件的具体内容,下面我们通过双击解决方案资源管理器中的文件图标打开相应的文件,在默认状态下,一个.xaml文件存在三个试图方式,分别为设计试图、XAML试图和源码试图。如果要查看相应的XAML代码的话,你可以点击XAML按钮切换到XAML试图状态下。这是打开应用程序定义文件App.xaml的相应界面:
点击XAML试图按钮后,App.xaml页面对应的XAML代码如下
01 <Application x:Class="BrowserDemo.App" 02 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 03 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 04 StartupUri="Page1.xaml" 05 > 06 <Application.Resources> 07 08 </Application.Resources> 09 </Application> |
01 using System; 02 using System.Windows; 03 using System.Windows.Navigation; 04 using System.Data; 05 using System.Xml; 06 using System.Configuration; 07 08 namespace BrowserDemo 09 { 10 /// <summary> 11 /// Interaction logic for App.xaml 12 /// </summary> 13 14 public partial class App : Application 15 { 16 17 } 18 } |
01 <Page x:Class="BrowserDemo.Page1" 02 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 03 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 04 Title="Page1" 05 > 06 <Grid> 07 </Grid> 08 </Page> |
01 using System; 02 using System.Collections.Generic; 03 using System.Text; 04 using System.Windows; 05 using System.Windows.Controls; 06 using System.Windows.Data; 07 using System.Windows.Documents; 08 using System.Windows.Input; 09 using System.Windows.Media; 10 using System.Windows.Media.Imaging; 11 using System.Windows.Navigation; 12 using System.Windows.Shapes; 13 14 namespace BrowserDemo 15 { 16 /// <summary> 17 /// Interaction logic for Page1.xaml 18 /// </summary> 19 20 public partial class Page1 : Page 21 { 22 23 public Page1() 24 { 25 InitializeComponent(); 26 } 27 28 } 29 } |
01 <Page x:Class="BrowserDemo.Page1" 02 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 03 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 04 Title="Page1" 05 > 06 <Grid> 07 <Button Height="100" Width="300" FontSize="24" Background="Aqua" 08 Name="MyButton" Content="Click Me" Click="ChangeMe" > 09 </Button> 10 </Grid> 11 </Page> |
01 void ChangeMe(object sender, RoutedEventArgs eventArgs) 02 { 03 count++; 04 MyButton.Content = "You have clicked " + count + " times!"; 05 } |
01 using System; 02 using System.Collections.Generic; 03 using System.Text; 04 using System.Windows; 05 using System.Windows.Controls; 06 using System.Windows.Data; 07 using System.Windows.Documents; 08 using System.Windows.Input; 09 using System.Windows.Media; 10 using System.Windows.Media.Imaging; 11 using System.Windows.Navigation; 12 using System.Windows.Shapes; 13 14 namespace BrowserDemo 15 { 16 /// <summary> 17 /// Interaction logic for Page1.xaml 18 /// </summary> 19 20 public partial class Page1 : Page 21 { 22 int count = 0; 23 public Page1() 24 { 25 InitializeComponent(); 26 } 27 28 void ChangeMe(object sender, RoutedEventArgs e) 29 { 30 count++; 31 MyButton.Content = "You have clicked " + count + " times!"; 32 } 33 } 34 } |