您的位置:首页 > 其它

介绍 Windows Presentation Foundation

2014-05-04 21:58 459 查看
介绍 Windows Presentation Foundation

WPF 是一个库,为开发用户界面提供了全新的模型,用它创建桌面的应用程序比使用 Windows 窗体更有优势;WPF 还提供了新的基于 XML 的语言 XAML,用于处理大量的窗体布局,而让 F# 专注于开发应用程序的感兴趣部分。

注意

现在有几种 XAML 设计器,这些设计器使用图形化的所见即所得(WYSWIG)工具设计界面,然后,用 F# 为其添加交互性。例如,Mobiform 提供的设计器 Aurora (http://www.mobiform.com/eng/aurora.html), Microsoft 提供的设计器Expression Blend (http://www.microsoft.com/products/expression/en/expression-blend/default.mspx)。

WPF 是 .NET 3.0 的一部分,如果使用Vista,默认情况下已经安装;[ 对于 Windows 7 及以上版本,.NET 3.0 需要手动安装;但是,提供了更高版本的 .NET 中同样包含 WPF ];其他的 Windows 用户需要安装 .NET 3.0 才能访问 WPF,最好的方法是下载 Windows SDK for Windows Server 2008 and .NET Framework 3.5 (http://is.gd/521hd)
。这一节中的示例需要引用以下的 dll:PresentationCore.dll、PresentationFramework.dll 和 WindowsBase.dll。

我们看到的第一个例子演示了如何用 XAML 创建一个简单窗体,然后用 F# 显示出来。示例显示了窗体 XAML 定义有四个控件:两个标签,一个文本框,一个按钮。

清单 8-5 用 XAML 创建的简单窗体

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64"/>
<ColumnDefinition Width="128"/>
<ColumnDefinition Width="128"/>
<ColumnDefinition Width="128"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="24"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" >Input:</Label>
<TextBox Name="input" Grid.Column="1"
Text="hello"/>
<Label Name="output" Grid.Row="0"
Grid.Column="2"></Label>
<Button Name="press" Grid.Column="3" >Press
Me</Button>
</Grid>
</Window>

使用窗体的 XAML 定义,需要做两件事情:第一,加载窗的定义并显示。但是,如果仅做到这个,不能提供与用户的交互,因此,需要做另一件事情才能使窗体具有交互性;第二,用 F# 为控件添加事件处理程序。在这里,我们为按钮添加事件处理程序,把文本框的内容显示到第二个标签中。函数 createWindow 是一个通用的函数,用于加载 XAML 窗体。用这个函数创建值 window;然后,把这个值传递给窗体的 FindName 方法,找出窗体中的控件,这样,就可以和控件进行交互了;最后,在主(main)函数中创建应用程序(Application)类的实例,并用它显示窗体(见清单
8-6)。

清单 8-6 显示XAML 窗体并为它添加事件处理程序

open System
openSystem.Collections.Generic
open System.Windows
openSystem.Windows.Controls
openSystem.Windows.Markup
open System.Xml

//creates the window and loads the given XAML file into it
let createWindow (file:
string) =
using (XmlReader.Create(file)) (fun stream->
(XamlReader.Load(stream) :?>Window))

//create the window object and add event handler
//to the button control
let window =
let temp = createWindow
" ..\..\Window1.xaml"
let press = temp.FindName("press") :?>Button
let textbox = temp.FindName("input") :?>TextBox
let label = temp.FindName("output") :?>Label
press.Click.Add (fun _
-> label.Content <-textbox.Text )
temp

//run the application
let main() =
let app =
new Application()
app.Run(window) |> ignore

[<STAThread>]
do main()

[ 前面说过,]为了编译这个程序,必须添加引用: PresentationCore.dll、PresentationFramework.dll 和 WindowsBase.dll,它们通常在C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 目录。这一章的其他例子,都不需要添加引用,因为编译器会自动引用库。图 8-7 是前面示例创建的窗体。



图 8-7 用 XAML 和 F# 创建的窗体

[

原文没有叙述创建项目的过程。

第一步,创建一个 Fsharp 应用程序,把清单 8-6 的代码复制过去;

第二步,在项目中添加新项,选择文本文件,命名很重要,一定要是 Window1.xaml,把清单 8-5 的代码复制过去;

第三步,添加引用 PresentationCore.dll、PresentationFramework.dll 和 WindowsBase.dll,还要加两个System.Xml.dll、System.Xaml.dll;

第四步,如果现在运行,会出错,提示找不到 Window1.xaml,改成" ..\..\Window1.xaml",具体原因未去深究。

现在再运行,应该没有问题了。

]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: