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();
            }
        }

  • 相关阅读:
    vulcanjs 包类型
    vulcanjs schemas&& collections
    vulcanjs 核心架构概念
    vulcanjs 开源工具方便快速开发react graphql meteor 应用
    ory Oathkeeper Ecosystem
    ory Oathkeeper docker-compose 安装运行
    benthos stream nats 集成试用
    benthos 几个方便的帮助命令
    benthos 通过配置文件配置 stream 说明
    benthos 通过rest api 配置 stream 说明
  • 原文地址:https://www.cnblogs.com/salam/p/1831143.html
Copyright © 2011-2022 走看看