Application=Code+Markup 讀書筆記 19

bjq_ren發表於2008-08-11

19XAML

 

鬆散XAML

       <Buttonxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

           Hello World!

       Button>

.xaml副檔名被關聯到PresentationHost.exe程式。只要執行XAML,就會執行此程式,由其建立Page型別的物件,而此程式又可以嵌入IE中。PresentationHost.exe程式還將載入的XAML轉成實際的Button物件,並將物件設定為PageContent屬性。

 

這裡,是XamlReader.Load這個靜態方法將XAML轉成物件。

 

需要在伺服器上為.xaml副檔名註冊MIME型別,在.hta檔案中加入下面這行:

AddType application/xaml+xml xaml

 

鬆散XAML的根元素可以是繼承FrameworkElement的任何類,但不可以是Window

 

XamlWriter.Save方法,從物件產生XAML——與XamlReader.Load功能正好相反。

 

 

使用XamlReader.Load手動載入XAML的方法:

           stringstrXaml =

               @"

                   Foreground='LightSeaGreen' FontSize='24pt'>

                   Click me!

               ";

有兩種載入方法:

      方法1,操作記憶體:

           MemoryStreammemory =newMemoryStream(strXaml.Length);

           StreamWriterwriter =newStreamWriter(memory);

           writer.Write(strXaml);

           writer.Flush();

           memory.Seek(0,SeekOrigin.Begin);

           Objectbj =XamlReader.Load(memory);

 

      方法2,作為XML讀取:

           StringReaderstrReader =newStringReader(strXaml);

           XmlTextReaderxmlReader =newXmlTextReader(strReader);

           Objectbj =XamlReader.Load(xmlReader);

 

將這個obj設定給Content,然後在Main中載入,完整程式碼如下:

   publicclassLoadEmbeddedXaml:Window

   {

       [STAThread]

       publicstaticvoidMain()

       {

           Applicationapp =newApplication();

           app.Run(newLoadEmbeddedXaml());

       }

       publicLoadEmbeddedXaml()

       {

           stringstrXaml =

               "

               "       Foreground='LightSeaGreen' FontSize='24pt'>"+

               "   Click me!"+

               "";

 

           StringReader strreader =newStringReader(strXaml);

           XmlTextReader xmlreader =newXmlTextReader(strreader);

           objectbj = XamlReader.Load(xmlreader);

 

           Content = obj;

       }

   }

 

注意:載入XAML的內部過程:

PresentationHost.exe程式判斷出返回物件為Button,於是進行相應轉換

           Buttonbtn = (Button)XamlReader.Load(xmlReader);

然後自動設定事件處理函式:

           btn.Click += ButtonClick;

 

 

以上處理的是內嵌XAML——將其寫入字串中。下面是如何載入外部XAML

這裡,即使是一個xml檔案,只要裡面的內容是XAML的,也可以解析。

對於外部的LoadXamlResource.xml檔案——位於與解決方案相同的目錄下,在類的建構函式中對其進行載入:

           Uriuri =newUri("pack://application:,,,/LoadXamlResource.xml");

           Streamstream =Application.GetResourceStream(uri).Stream;

           FrameworkElementel =XamlReader.Load(stream)asFrameworkElement;

           Content = el;

 

           Buttonbtn = el.FindName("MyButton")asButton;

 

           if(btn !=null)

               btn.Click += ButtonOnClick;

 

然後在Main函式中,執行這個例項:

       publicstaticvoidMain()

       {

           Applicationapp =newApplication();

           app.Run(newLoadXamlResource());

       }

 

如果XAML是一個Window根元素,此時載入要特殊處理——直接在Main函式中處理,將載入到的流轉型Window型別,並新增事件控制程式碼,然後直接執行:

       publicstaticvoidMain()

       {

           Applicationapp =newApplication();

 

           Uriuri =newUri("pack://application:,,,/LoadXamlWindow.xml");

           Stream stream =Application.GetResourceStream(uri).Stream;

           Windowwin = XamlReader.Load(stream)asWindow;

 

           win.AddHandler(Button.ClickEvent,

                          newRoutedEventHandler(ButtonOnClick));

 

           app.Run(win);

       }

 

 

對於使用Open File的方式開啟磁碟上一個XAML

           XmlTextReaderxmlreader =newXmlTextReader(dlg.FileName);

 

           // Convert XAML to object.

           objectbj =XamlReader.Load(xmlreader);

 

           // If it's a Window, call Show.

           if(objisWindow)

           {

               Windowwin = objasWindow;

               win.Owner =this;

               win.Show();

           }

 

           // Otherwise, set as Content of Frame.

           else

               frame.Content = obj;

 

額外需要注意的是,對於一個Window物件的XAML,要把Owner設定為this自己。

 

 

 

xmln宣告預設的XML預設名稱空間。這個名稱空間會被應用於宣告出現的element以及其下的每個孩子。它的名稱必須是唯一且一致的,所以常使用URL作為名稱空間,也就是

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

XAML不只用於WPF,還可以用於WF等框架,所以這裡使用presentation作為URL的字尾。

 

XAML規範定義了多個元素和屬性,可以在任何XAML中使用,包括WPF。這些元素和屬性被關聯到不同於WPF的名稱空間。由於我們肯定要使用到這些元素,所以要宣告

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

這裡標誌為xmlns:x,從而XAML中的專用元素和屬性都以x為字首,例如x:Codex:Class,它們都屬於XAML名稱空間,而非WPF名稱空間。

 

由於WPF名稱空間的元素和屬性多於XAML名稱空間,所以使用前者作為預設名稱空間。

 

x:Class屬性

只能出現在XAML檔案的根元素中?

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15123181/viewspace-423015/,如需轉載,請註明出處,否則將追究法律責任。

相關文章