[译]Hour 7 Teach.Yourself.WPF.in.24.Hours

本小时您能学到:

l  应用类型(WindowsXBAP,独立的XAML

l  标准应用VS浏览器类型应用

l  用户控件

本小时将介绍您创建WPF应用程序是所面对的选择。WPF为我们提供了创建不同应用的途径,而且一旦开发开始就很难更改。我们将讨论定制您的应用中所需要的组建。

 

WPF应用的部署形式

WPF应用共有三种形式,下表将着重描述这三种部署形式。

可执行应用

这种格式的应用我们经常使用。您的WPF应用被编译到一个标准的可执行文件中。

XAML浏览器应用

有些时候这种应用被成为XBAP(或者X-Bap)。这是一种特殊的宿主与浏览器(IE6+以及Firefox 2)的WPF应用。由于宿主在浏览器中,这种类型的应用会有许多严格的安全限制,但只这种类型的应用很容易部署以.xbap为扩展名。

XAML文件

这是一个XAML编写的文本文件。.xaml文件使用浏览器来解析。这种应用的限制就是在应用中除了XAML以外不能包含任何代码。使用.xaml为扩展名

XAML文件创建一个Font Viewer应用

在你的应用中不包含代码听起来像是很严格的限制,然而xaml是相当的强大。第一部分的Font Viewer应用程序并未使用任何代码,我们可以将改用用作为一个XAML文件。

 

虽然Visual Studio并未提供创建单独XAML文件的方法,但它可以帮助我们编辑一个XAML文件。接下来我们将FontViewer转换为一个简单的XAML文件。

 

1.         在您的桌面创建一个文本文件,并将其命名为FontViewer.xaml

2.         当您双击FontViewer.xaml,该文件将被默认浏览器加载。它会出现错误提示信息,因为我们并未添加任何代码。

3.         运行Visual Studio并使用【文件】->【打开文件】打开FontViewer.xaml

4.         我们可以使用这些markup在传统的FontViewer应用中。打开Font Viewer 项目下方的MainWindow.xaml文件。

5.         Copy其中的内容并将其粘贴到FontViewer.xaml文件中。

6.         FontViewer.xaml文件中定位到首尾的Window标签,并将其替换为Page标签。此时该应用便可执行了。

7.         运行这个应用这一步并不是必须的,但却是对可用性的一个很好的练习。修改Page标签中的Title属性为WindowTitle。但应用运行时WindowTitle属性的内容将显示在浏览器的标题栏中。

8.         双击并运行FontViewer.xaml应用。完整的代码如下: 

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

WindowTitle="Teach Yourself WPF: Font Viewer"

Height="480"

Width="640">

    <DockPanel Margin="8">

        <Border DockPanel.Dock="Top"

            CornerRadius="6"

            BorderThickness="1"

            BorderBrush="Gray"

            Background="LightGray"

            Padding="8"

            Margin="0 0 0 8">

            <TextBlock FontSize="14"

                TextWrapping="Wrap">

                Select a font to view from the list below.

                You can change the text by typing in the region at the bottom.

            </TextBlock>

        </Border>

        <ListBox x:Name="FontList"

            DockPanel.Dock="Left"

            ItemsSource="{x:Static Fonts.SystemFontFamilies}"

            Width="160" />

                <TextBox x:Name="SampleText"

                    DockPanel.Dock="Bottom"

                    MinLines="4"

                    Margin="8 0"

                    TextWrapping="Wrap">

                    <TextBox.ToolTip>

                        <TextBlock>

                            <Italic Foreground="Red">Instructions: </Italic>

                            Type here to change the preview text.

                        </TextBlock>

                    </TextBox.ToolTip>

                         The quick brown fox jumps over the lazy dog.

                </TextBox>

        <StackPanel Margin="8 0 8 8">

            <TextBlock Text="{Binding ElementName=SampleText,

                Path=Text}"

                FontFamily="{Binding ElementName=FontList,

                Path=SelectedItem}"

                FontSize="10"

                TextWrapping="Wrap"

                Margin="0 0 0 4" />

                <TextBlock Text="{Binding ElementName=SampleText,

                Path=Text}"

                FontFamily="{Binding ElementName=FontList,

                Path=SelectedItem}"

                FontSize="16"

                           TextWrapping="Wrap"

                Margin="0 0 0 4" />

            <TextBlock Text="{Binding ElementName=SampleText,

                Path=Text}"

                FontFamily="{Binding ElementName=FontList,

                Path=SelectedItem}"

                FontSize="24"

                TextWrapping="Wrap"

            Margin="0 0 0 4" />

            <TextBlock Text="{Binding ElementName=SampleText,

                Path=Text}"

                FontFamily="{Binding ElementName=FontList,

                Path=SelectedItem}"

                FontSize="32"

                TextWrapping="Wrap" />

        </StackPanel>

    </DockPanel>

</Page>

 

Font Viewer转换成XBAP应用

Font Viewer部署为一个XBAP应用是很简单的。Visual Studio提供了一个很有用的机制来发布您的XBAP应用。

接下来我们将通过修改Font Viewer创建一个XBAP项目。

1.         Visual Studio中,选择【文件】->【新建项目】,选择WFP浏览器应用。并将其命名为FontViewerXBAP.

2.         Visual Studio创建新建项目之后,在【解决方案资源管理器】中。您将看到两点不同。首先,除了Window1.xaml之外还有一个Page1.xaml文件。其次,有一个以.pfx为扩展名的签名文件。

3.         在编辑其中打开Page1.xaml文件,并使用以上代码替换其中的内容。注意:我们使用与仅仅拥有XAML代码版本的FontViewer一样的代码。

     4.         在使用这个XBAP应用之前需要做一个小小的修改,添加一下代码到Page标签中:

x:Class=”FontViewerXBAP.Page1”  

未完待续.. 

原文地址:https://www.cnblogs.com/netwenchao/p/1588977.html