天天看點

1.WPF技術調查1. WPF技術調查

1. WPF技術調查

1.1 WPF簡介

WPF的全稱是Windows Presentation Foundation,是微軟新釋出的Vista作業系統的三大核心開發庫之一,其主要負責的是圖形顯示,是以叫Presentation(呈現)。作為新的圖形引擎,WPF是基于DirectX的,當然增加了很多新的功能。其2D和3D引擎的強大看看Vista的界面就明白了,再加上其對Aero圖形引擎的支援,更加讓你剛到神奇。順便提一下,Aero是專門為3D桌面開發的引擎,可以讓桌面實作神奇的3D翻轉,這絕對是作業系統有史以來的一次神奇嘗試,雖然對硬體配置的要求也是驚人的,此前已有相關報道稱,Vista對顯示卡十分挑剔就是出于運作Aero的考慮。

1.1.1 WPF的主要特性

圖形服務

  所有的圖形(包括桌面的視窗等對象)都将通過Direct3D渲染,旨在提供統一的圖形顯示通道,以實作進階的圖形表現效果。

憑借Direct3D渲染圖形的好處是,作業系統可以将圖形處理任務委托給計算機顯示卡上的圖形處理晶片GPU,以減輕CPU的負擔。

WPF支援任意比例無損縮放的矢量化圖形,支援在2D應用程式中進行3D模型的渲染和互動操作。

互操作性

  WPF提供了同Win32的互操作性。通過嵌入模式,可以在Win32 代碼中使用WPF,也可在WPF中使用Win32 代碼。同Windows 窗體的互操作可通過ElementHost類和WindowsFormsHost類來實作。

媒體服務

WPF為二維圖形提供了基本幾何元素,以及一整套畫刷、畫筆、幾何體和變形等工具。WPF的三維特性局限于Direct3D現有的功能。然而,WPF提供了與使用者界面、文檔和媒體媒體等的緊密內建。這使得三維界面、三維文檔、三維媒體成為可能。   

WPF支援大多數通用圖像格式,以及WMV, MPEG and AVI等視訊格式。WPF支援時基動畫而不是桢動畫,進而使動畫的播放速度不受系統性能的影響。WPF使用ClearType技術實作文本渲染。ClearType提供了次像素級定位、自然步幅、縱向反鋸齒等特性。WPF同時支援OpenType字型特性。

資料綁定

  WPF 内置了一系列資料服務,使得開發人員可以在應用程式之間自如地綁定和操縱資料。WPF提供三種資料綁定方式:

  一次性:用戶端忽略伺服器端的資料更新的情況

  單向性:用戶端對伺服器端的資料有隻讀權限的情況

  雙向性:用戶端對伺服器端的資料擁有讀寫權限的情況

使用者界面

  WPF内置一系列控件:按鈕、菜單、清單框等。

WPF提出了一個功能強大的概念:将控件的邏輯層和表現層分離。通過重載控件的模闆就可徹底改變其外觀。控件可包容任何其它控件,進而通過組合實作無限的控制功能。

1.1.2 WPF架構

1.1.3 WPF程式

WPF除了建構傳統的獨立運作桌面應用程式,也支援基于XAML技術的浏覽器應用(XBAP)。兩種類型應用的程式設計模型很相似。

1)      桌面應用程式是可信度最高,采用ClickOnce或MSI等軟體安裝在本地計算機的應用程式,對計算機資源擁有完全的通路權限。

2)      浏覽器應用程式應用雖然看起來像是在浏覽器内運作,實質上是建立了另一個程序。Internet Explorer 使用者可以将 XBAP 直接下載下傳到浏覽器,而不必部署安裝版 WPF 應用程式。此應用程式在 Internet Explorer 内運作,可以提供基于 WPF 的使用者界面。但是從 Internet 網站下載下傳和運作代碼是一項危險的操作。為保護使用者免遭惡意開發者的攻擊,所有從 Internet 下載下傳的 XBAP 都在部分信任沙箱中運作。基于 .NET Framework 提供的代碼通路安全性,該沙箱會限制 XBAP 的操作。例如,從 Internet 下載下傳的 XBAP 不能建立獨立視窗或啟動新視窗,不能顯示由 XBAP 本身啟動的儲存對話框或通路隔離存儲區以外的檔案系統。盡管沙箱規定了種種限制,XBAP 仍然可以使用大部分的 WPF 功能,包括二維和三維圖形、動畫、螢幕文檔、圖像、視訊等。

問題

在WPF中,WinForm究竟是否能完全能取代WebForm?如果說原有的Winform屬于胖客戶,那最終可以取代WebForm的Winform到底屬于胖客戶還是瘦客戶?

WPF之中,本身就有分為Windows及Browser兩種應用程式,另外再加上真正在Web上運作的WPF/E,故它其實是同時具備三種型态能力的. 然而雖然微軟對WPF期望很大,但畢竟不能不面對現實,ASP.NET的人口更大,傳統HTML仍主宰着現在及未來的網絡世界,您可以反問自己一個問題,Flash出來後,為什麼HTML沒有消失?故同理可證,WPF出來後,ASP.NET不會消失,且下一版本的ASP.NET已在設計中,會随着Visual Studio 2008一并推出,最終會形成WPF與ASP.NET各占一片天的兩大勢力。

1.2應用程式生命周期

App.xaml

<Application x:Class="WpfApplication_LifeCyclew.App"

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

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

    Startup="Application_Startup"

    Activated="Application_Activated"

    Deactivated="Application_Deactivated"

    Exit="Application_Exit"

    DispatcherUnhandledException="Application_DispatcherUnhandledException"

    SessionEnding="Application_SessionEnding"

    >

    <Application.Resources>

    </Application.Resources>

</Application>

App.xaml.cs

public partial class App : Application

    {

        bool isApplicationActive;

        /// <summary>

        /// StartupUri="Window1.xaml"

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Application_Startup(object sender, StartupEventArgs e)

        {

            Window2 win = new Window2();

            win.Show();

        }

        private void Application_Activated(object sender, EventArgs e)

        {

            this.isApplicationActive = true;

        }

        private void Application_Deactivated(object sender, EventArgs e)

        {

            this.isApplicationActive = false;

        }

        private void Application_Exit(object sender, ExitEventArgs e)

        {

            Application.Current.Shutdown(-1);

        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)

        {

            MessageBox.Show(e.Exception.Message);

        }

        private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)

        {

            string msg = string.Format("{0}. End session?", e.ReasonSessionEnding);

            e.Cancel = true;

    }

        private void Application_Exit(object sender, ExitEventArgs e)

        {

            Application.Current.Shutdown(-1);

       }

    }

1.3兩種WPF程式

1.3.1 WPF應用程式

<Window x:Class="WpfApplication1.Window1"

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

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

    Title="Window1" Height="300" Width="300">

    <Grid>

        <Button Height="23" Margin="96,74,108,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">ClickButton</Button>

    </Grid>

</Window>

/// <summary>

    /// Window1.xaml 的互動邏輯

    /// </summary>

    public partial class Window1 : Window

    {

        public Window1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            MessageBox.Show("成功點選按鈕!");

        }

    }

1.3.2 WPF浏覽器應用程式

<Page x:Class="WpfBrowserApplication1.Page1"

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

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

    Title="Page1">

    <Grid>

        <Button Height="23" Margin="96,74,108,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">ClickButton</Button>

    </Grid>

</Page>

/// <summary>

    /// Page1.xaml 的互動邏輯

    /// </summary>

    public partial class Page1 : Page

    {

        public Page1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

             MessageBox.Show("成功點選按鈕!");

        }

    }

1.3.3 程式差別

1. Window / Page

2. 項目部署差別

3. 安全限制

在網際網路區域,wpf的不安全特性:

1) 通路本地檔案系統;

2) 通路系統資料庫;

3) 與非托管代碼進行互動;

4) 啟動新的視窗。

下圖示範了區域、權限集、權限和資源之間的關系。

在浏覽器中打開新視窗的錯誤界面:

解決方案:

數字簽名,部署到GAC

1.3.4 通用開發方式

建立同一個庫,既可使用于WinApp程式,又可作為Web程式加載于浏覽器中。注意,要在運作時檢測狀态,IsBrowserHosted屬性判斷。

1.4小心ClickOnce緩存

ClickOnce部署,基于應用程式版本号。

Mage –cc  : 清除所有僅聯機應用程式的已下載下傳應用程式緩存。

1.5 按需下載下傳檔案

1.6 DeepZoom技術

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

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

    Title="Loose XAML PhotoGallery">

  <Page.Resources>

    <ScaleTransform x:Key="transform" ScaleX="3" ScaleY="{Binding Path=ScaleX, RelativeSource={RelativeSource Self}}"/>

    <LinearGradientBrush x:Key="shinyBrush" StartPoint="0,0" EndPoint="0,1">

      <GradientStop Offset="0" Color="Gray"/>

      <GradientStop Offset="0.3" Color="#FF222222"/>

      <GradientStop Offset="0.3" Color="Black"/>

      <GradientStop Offset="0.9" Color="Black"/>

      <GradientStop Offset="0.9" Color="#FF222222"/>

      <GradientStop Offset="1" Color="Gray"/>

    </LinearGradientBrush>

  </Page.Resources>

  <Grid>

    <Grid.RowDefinitions>

      <RowDefinition Height="50"/>

      <RowDefinition/>

      <RowDefinition Height="55"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width="70"/>

      <ColumnDefinition/>

    </Grid.ColumnDefinitions>

    <Rectangle Grid.ColumnSpan="3" Fill="{StaticResource shinyBrush}"/>

    <Rectangle Grid.Row="2" Grid.ColumnSpan="3" Fill="{StaticResource shinyBrush}"/>

    <Grid Grid.Row="1" Background="White">

      <Slider Margin="20" Orientation="Vertical" Value="{Binding Path=ScaleX, Source={StaticResource transform}, Mode=TwoWay}" Minimum="1" Maximum="10" Height="100"/>

    </Grid>

    <ListBox x:Name="pictureBox" Background="AliceBlue" Grid.Row="1" Grid.Column="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled">

      <ListBox.ItemsPanel>

        <ItemsPanelTemplate>

          <WrapPanel/>

        </ItemsPanelTemplate>

      </ListBox.ItemsPanel>

     <Image Source="Autumn Leaves.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Creek.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Desert Landscape.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Dock.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Forest.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Forest Flowers.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Frangipani Flowers.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Garden.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Green Sea Turtle.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Humpback Whale.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Oryx Antelope.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Toco Toucan.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Tree.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Waterfall.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

     <Image Source="Winter Leaves.jpg" Margin="3,8" Height="35" LayoutTransform="{StaticResource transform}"/>

    </ListBox>

  </Grid>

</Page>

ScaleTransform按元素的水準和垂直尺寸進行縮放。其中,ScaleX控制文字的水準縮放比率,ScaleY控制文字的垂直方向的比率 。

1.7 多媒體流執行個體

1.8與浏覽器互動

XBAP程式在浏覽器中加載的方式:

1)    直接加載

2)    Iframe嵌入

<iframe src="Wpf/InteractiveWithWpfBrowserApplication.xbap"></iframe>

方法1: URL

http://eking/InteractiveWithWpfBrowserApplication/InteractiveWithWpfBrowserApplication.xbap?a=1&b=2

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections.Specialized;

using System.Windows.Interop;

using System.Web;

using System.Deployment.Application;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

namespace EkingInteractiveWithWpfBrowserApplication

{

    /// <summary>

    /// Page1.xaml 的互動邏輯

    /// </summary>

    public partial class Page1 : Page

    {

        public Page1()

        {

            InitializeComponent();

            StringBuilder sb = new StringBuilder();

            sb.Append("URL參數:");

            NameValueCollection paramsQuery = GetQueryStringParameters(ApplicationDeployment.CurrentDeployment.ActivationUri);

            foreach (string key in paramsQuery.Keys)

            {

                sb.Append( "key: " + key + ", value: " + paramsQuery[key] );

                sb.Append("; ");

            }

            this.label1.Content = sb.ToString();

        }

        private NameValueCollection GetQueryStringParameters(Uri launchUri)

        {

            NameValueCollection nameValueTable = HttpUtility.ParseQueryString(launchUri.Query);

            return nameValueTable;

        }

    }

}

方法2: Cookie (未試通)

Application.GetCookie(ApplicationDeployment.CurrentDeployment.ActivationUri);

注:獲得正在浏覽的XBAP網頁的URI

1)      BrowserInteropHelper.Source

2)      System.Deployment.Application.ApplicationDeployment.CurrentDeployment.ActivationUri

1.9目前開發存在的問題

1. 工具欄停靠不好實作,沒有父視窗。

2. 屬性視窗的處理。

3. WPF中嵌入的Win32圖形控件,控件使用GDI+技術。控件在WPF中事件無法響應。

4. WPF浏覽器應用程式如何調試?

5. WPF浏覽器應用程式與外部網頁互動能力太弱。

繼續閱讀