Introducing the Windows Presentation Foundation

Source: Internet
Author: User

Introducing the Windows Presentation Foundation

WPF is a library that provides a new model for developing the user interface, and the applications that use it to create desktops are more advantageous than Windows forms; WPF also provides new XML-based language XAML for handling a large number of form layouts, while allowing F # to focus on developing an interesting part of the application.

Attention

There are now several XAML designers that use the graphical WYSIWYG (Wyswig) tool to design the interface and then add interactivity to it with F #. For example, Mobiform provides a designer for Aurora (http://www.mobiform.com/eng/aurora.html), a Microsoft-provided designer for expression Blend (HTTP/ www.microsoft.com/products/expression/en/expression-blend/default.mspx).

WPF is part of. NET 3.0, and if you use Vista, it is installed by default, [for Windows 7 and later,. NET 3.0 requires manual installation, but a later version of. NET also includes WPF]; other Windows users need The best way to install. NET 3.0 to access WPF is to download the Windows SDK for Windows Server and. NET Framework 3.5 (HTTP://IS.GD/521HD). The examples in this section need to refer to the following dll:PresentationCore.dll, PresentationFramework.dll, and WindowsBase.dll.

The first example we saw demonstrates how to create a simple form in XAML and then display it in F #. The example shows the form XAML definition with four controls: two labels, a text box, and a button.

Listing 8-5 a simple form created in 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 = "/> "

< ColumnDefinition Width = "/>"

< ColumnDefinition Width = "/>"

< ColumnDefinition Width = "/>"

</ grid.columndefinitions >

< grid.rowdefinitions >

< RowDefinition Height = "/>"

</ 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 >

Using the form's XAML definition, you need to do two things: first, the definition of the load window and display. However, if you only do this, you cannot provide interaction with the user, so you need to do another thing to make the form interactive; second, add an event handler for the control in F #. Here, we add an event handler for the button to display the contents of the text box to the second label. The function CreateWindow is a generic function that is used to load a XAML form. Use this function to create the value window, and then pass this value to the form's FindName method to find the controls on the form, so that you can interact with the control, and finally, create an instance of the application (application) class in the main (main) function. and use it to display the form (see Listing 8-6).

Listing 8-6 shows the XAML form and adds an event handler for it

Open System

Open System.Collections.Generic

Open System.Windows

Open System.Windows.Controls

Open System.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 ()

[previously said,] in order to compile this program, you must add references: PresentationCore.dll, PresentationFramework.dll, and WindowsBase.dll, which are usually C:\Program files\ Reference assemblies\microsoft\framework\v3.0 directory. Other examples of this chapter do not need to add references because the compiler will automatically reference the library. Figure 8-7 is the form created by the previous example.

Figure 8-7 forms created with XAML and F #

[

The original text does not describe the process of creating the project.

The first step is to create a FSHARP application that copies the code from listing 8-6 to the past;

The second step, add new item in the project, select text file, name is very important, must be Window1.xaml, copy the code of listing 8-5 to the past;

The third step, add references PresentationCore.dll, PresentationFramework.dll and WindowsBase.dll, plus two System.Xml.dll, System.Xaml.dll;

The fourth step, if run now, will be wrong, the hint can not find Window1.xaml, changed to ". \.. \window1.xaml ", the specific reason did not go into the deep.

Now run again, there should be no problem.

]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.