读取写入Xaml的方法

一、读取Xaml文件的内容

我们首先使用StreamResourceInfo的GetResourceStream(Uri uri)方法读取xap包中的MainPage.xaml文件,然后StreamReader来读取信息。

 private void LoadingXaml()
        {
            StreamResourceInfo info = App.GetResourceStream(new Uri("XamlReaderDemo;component/MainPage.xaml", UriKind.RelativeOrAbsolute));
            StreamReader sr = new StreamReader(info.Stream);
            txtRead.Text = sr.ReadToEnd();
        }

 

二、写入Xaml文件的内容

我们首先要重载OnApplyTemplate(),来应用现有样式

然后使用XamlReader来操作.

 Image _myImage = null;
 
        private const string _contentTemplate
            = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
              " xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
              "<Image Grid.Column=\"1\"  Width=\"500\" Height=\"231\" x:Name=\"sonicImage\" Source=\"space.jpg\"></Image>" +
              "</ControlTemplate>";
        public override void OnApplyTemplate()
        {
            _myImage = (Image)GetTemplateChild("sonicImage");
        }

        public MyXaml()
        {
            Template = (ControlTemplate)XamlReader.Load(_contentTemplate);
            ApplyTemplate();
        }

 

最后我们加入模板

this.iamge.Children.Add(new MyXaml());

 

完整代码如下:

1.MyXaml.cs

public class MyXaml : Control
    {
        Image _myImage = null;
 
        private const string _contentTemplate
            = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
              " xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
              "<Image Grid.Column=\"1\"  Width=\"500\" Height=\"231\" x:Name=\"sonicImage\" Source=\"space.jpg\"></Image>" +
              "</ControlTemplate>";
        public override void OnApplyTemplate()
        {
            _myImage = (Image)GetTemplateChild("sonicImage");
        }

        public MyXaml()
        {
            Template = (ControlTemplate)XamlReader.Load(_contentTemplate);
            ApplyTemplate();
        }
    }

2.MainPage.xaml

<UserControl x:Class="XamlReaderDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
   
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBlock x:Name="txtRead"   TextWrapping="Wrap" Grid.Column="0" Text="这是xaml的内容" ></TextBlock>
        <Canvas x:Name="iamge" Grid.Column="1" Background="Transparent"></Canvas>
    </Grid>
</UserControl>

3.MainPage.cs

 public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.iamge.Children.Add(new MyXaml());
            //this.MouseLeftButtonDown += new MouseButtonEventHandler(UserControl_MouseLeftButtonDown);

            LoadingXaml();
        }

        private void LoadingXaml()
        {
            StreamResourceInfo info = App.GetResourceStream(new Uri("XamlReaderDemo;component/MainPage.xaml", UriKind.RelativeOrAbsolute));
            StreamReader sr = new StreamReader(info.Stream);
            txtRead.Text = sr.ReadToEnd();
        }

        private void UserControl_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
        {
            LoadingXaml();
        }
    }

posted @ 2010-09-19 16:22  ForrestWoo  阅读(1356)  评论(0编辑  收藏  举报