天天看點

Windows Phone 7 開發探索筆記8——加載XAML檔案中的對象

    上篇文章介紹了如何在Silverlight for Windows Phone中讀取XML檔案,本文來看一下如何從XAML檔案中讀取資訊。

一.準備XAML檔案

    有時候我們需要加載一些來自檔案,資源中的UI元素,例如儲存在XAML檔案中的UI元素。首先來準備一個待讀取的XAML檔案,我們可以通過建立一個文本檔案并将其擴充名改為.xaml的方式來建立,但更好的方式是在Visual Studio中建立,方法如下:右擊解決方案資料總管中的項目選擇添加新項,選擇任意一個擴充名為.xaml的項均可,然後将自動生成的内容清空,添加如下的代碼:

Ellipse.xaml檔案中的XAML代碼:

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

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

         VerticalAlignment="Center" 

         HorizontalAlignment="Center" 

         Height="350" Width="350" >

    <Ellipse.Fill>

        <RadialGradientBrush  GradientOrigin="0.4,0.4" Center="0.4 0.4">

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

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

        </RadialGradientBrush>

    </Ellipse.Fill>

</Ellipse>

Rectangle.xaml檔案中的XAML代碼:

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

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

         VerticalAlignment="Center" 

         HorizontalAlignment="Center"

           Height="300" Width="350">

    <Rectangle.Fill>

        <LinearGradientBrush StartPoint="0 0.5" EndPoint="1 0.5">

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

            <GradientStop Color="Red" Offset="0.5"/>

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

        </LinearGradientBrush  >

    </Rectangle.Fill>

</Rectangle>

我分别建立了兩個XAML檔案,其中一個是圓形,一個是矩形,設定好它們相應的屬性即可,注意要添加相應的xml名稱空間(xmlns)。在Visual Studio中建立的好處是可以得到設計器的幫助,看到實時的效果,當然也可以使用Expression Blend來做。最後,要注意把Visual Studio自動建立的Code-Behind檔案删掉。

二.解析XAML檔案

    要解析XAML檔案中的資訊,需要借助于XamlReader類,它為分析XAML和建立相應的Silverlight對象樹提供XAML處理器引擎。在Silverlight for Windows Phone中它隻包含一個靜态的Load方法,此方法用來分析一段格式良好的XAML片段并建立相應的Silverlight對象樹,然後傳回這個對象樹的根。Load方法接受一個string類型的參數,我們可以将XAML檔案中的代碼直接傳給它,不過在實際項目中,通常不會寫死,是以采用與上篇文章中相同的方法,Application類的GetResourceStream方法,代碼如下:

T LoadXAMLAndGetShape<T>(string url)

        {

            string xaml = string.Empty;

            StreamResourceInfo sri = Application.GetResourceStream(new Uri(url, UriKind.Relative));

            using (StreamReader sr = new StreamReader(sri.Stream))

            {

                xaml = sr.ReadToEnd();

            }

            T shape = (T)XamlReader.Load(xaml);

            return shape;

        }

這裡我建立了一個泛型方法,以便傳回不同類型的對象。在方法中,通過Application.GetResourceStream方法加載XAML檔案,然後用StreamReader将它讀成字元串,最後将字元串傳入XamlReader的Load方法來構造相應的類型。

三.使用讀取到的Silverlight對象

讀取到相應的對象後就可以進行自由的操作了,我在程式中添加了2個按鈕用來在頁面中呈現兩個不同的對象,完整代碼如下:

using System;

using System.IO;

using System.Windows;

using System.Windows.Markup;

using System.Windows.Resources;

using System.Windows.Shapes;

using Microsoft.Phone.Controls;

namespace WindowsPhoneLoadXAML

{

    public partial class MainPage : PhoneApplicationPage

    {

        Ellipse ellipse;

        Rectangle rectangle;

        // Constructor

        public MainPage()

        {

            InitializeComponent();

            ellipse = LoadXAMLAndGetShape<Ellipse>("/WindowsPhoneLoadXAML;component/Ellipse.xaml");

            rectangle = LoadXAMLAndGetShape<Rectangle>("/WindowsPhoneLoadXAML;component/Rectangle.xaml");

        }

        T LoadXAMLAndGetShape<T>(string url)

        {

            string xaml = string.Empty;

            StreamResourceInfo sri = Application.GetResourceStream(new Uri(url, UriKind.Relative));

            using (StreamReader sr = new StreamReader(sri.Stream))

            {

                xaml = sr.ReadToEnd();

            }

            T shape = (T)XamlReader.Load(xaml);

            return shape;

        }

        private void btnEllipse_Click(object sender, RoutedEventArgs e)

        {

            if (ContentPanel.Children.Contains(rectangle))

            {

                this.ContentPanel.Children.Remove(rectangle);

            }

            if (ellipse != null && !ContentPanel.Children.Contains(ellipse))

            {

                this.ContentPanel.Children.Add(ellipse);

            }

        }

        private void btnRectangle_Click(object sender, RoutedEventArgs e)

        {

            if (ContentPanel.Children.Contains(ellipse))

            {

                this.ContentPanel.Children.Remove(ellipse);

            }

            if (rectangle != null && !ContentPanel.Children.Contains(rectangle))

            {

                this.ContentPanel.Children.Add(rectangle);

            }

        }

    }

}

下面是程式截圖:

Windows Phone 7 開發探索筆記8——加載XAML檔案中的對象
Windows Phone 7 開發探索筆記8——加載XAML檔案中的對象

四.下載下傳示例代碼:

WindowsPhoneLoadXAML.zip

如果大家喜歡我的文章,請點選“推薦”,謝謝!

繼續閱讀