zoukankan      html  css  js  c++  java
  • 读取写入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();
            }
        }

  • 相关阅读:
    【BZOJ 4151 The Cave】
    【POJ 3080 Blue Jeans】
    【ZBH选讲·树变环】
    【ZBH选讲·拍照】
    【ZBH选讲·模数和】
    【CF Edu 28 C. Four Segments】
    【CF Edu 28 A. Curriculum Vitae】
    【CF Edu 28 B. Math Show】
    【CF Round 439 E. The Untended Antiquity】
    【CF Round 439 C. The Intriguing Obsession】
  • 原文地址:https://www.cnblogs.com/salam/p/1831143.html
Copyright © 2011-2022 走看看