首页 > 其他分享 >WPF XamlReader Load xaml and parse

WPF XamlReader Load xaml and parse

时间:2024-08-23 20:37:47浏览次数:6  
标签:Load Windows win xaml System parse using WPF null

//The xaml material named MainWin.xaml

<Window
        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"
        xmlns:local="clr-namespace:WpfApp268" Topmost="True"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindowXamlReader.Load()" Height="450" Width="800">
    <StackPanel>
        <Button Content="Load" Width="200" Height="100" Background="Black"/>
        <Button Content="Load" Width="200" Height="100" Background="Red"/>
        <Button Content="Load" Width="200" Height="100" Background="Green"/>
        <Button Content="Load" Width="200" Height="100" Background="Blue"/>
        <Button Content="Load" Width="200" Height="100" Background="Cyan"/>
    </StackPanel>
</Window>

 

 

C# read the xaml file via FileStream and parse it

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp268
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Window win = null;
            using (FileStream fs = new FileStream("MainWin.xaml", FileMode.Open, FileAccess.Read))
            {
                win = (Window)XamlReader.Load(fs);
                if (win != null)
                {
                    StackPanel stkPanel=win.Content as StackPanel;
                    if (stkPanel != null)
                    {
                        Button cyanBtn = stkPanel.Children[4] as Button;
                        if(cyanBtn != null)
                        {
                            cyanBtn.Click += CyanBtn_Click;
                        }
                    }
                }
                win.ShowDialog();

            }
        }

        private void CyanBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("This is Cyan Button!");
        }
    }
}

 

 

 

标签:Load,Windows,win,xaml,System,parse,using,WPF,null
From: https://www.cnblogs.com/Fred1987/p/18377055

相关文章

  • WPF ystem.Windows.Markup.XamlParseException HResult=0x80131501 Message='Spec
    System.Windows.Markup.XamlParseExceptionHResult=0x80131501Message='Specifiedclassname'WpfApp268.MainWindow'doesn'tmatchactualrootinstancetype'System.Windows.Window'.RemovetheClassdirectiveorprovideanin......
  • el-upload重复上传文件失效(Element-Plus)
    当指定了参数limit=1,再次上传就会无效以下是官方文档给出的解决方法示例通过on-exceed来定义超出限制时的行为<template><el-uploadref="uploadRef":limit="1":on-exceed="handleExceed":auto-upload="false"></el-upload>......
  • WPF 模仿前端大佬写一个Hover效果
    先看一下效果吧: 原博主的地址:【动画进阶】神奇的卡片Hover效果与Blur的特性探究-ChokCoco-博客园(cnblogs.com)原效果是一个css效果,我们采用WPF的方式模仿一下因为技术有限,没有原博主的那么好看,毕竟盗版永远比不过原版...然后这里看一下盗版的怎么写吧先是拿到......
  • 使用 reloadNuxtApp 强制刷新 Nuxt 应用
    title:使用reloadNuxtApp强制刷新Nuxt应用date:2024/8/22updated:2024/8/22author:cmdragonexcerpt:reloadNuxtApp是一个强大的工具,用于在Nuxt3应用中强制刷新页面。通过不同的选项,你可以控制刷新行为、指定路径、保存状态等。categories:前端开发tags:......
  • WPF 模拟UWP原生窗口样式——亚克力|云母材质、自定义标题栏样式、原生DWM动画 (附我封
    先看一下最终效果,左图为使用亚克力材质并添加组合颜色的效果;右图为MicaAlt材质的效果。两者都自定义了标题栏并且最大限度地保留了DWM提供的原生窗口效果(最大化最小化、关闭出现的动画、窗口阴影、拖拽布局器等)。接下来把各部分的实现一个个拆开来讲讲。一、使用窗口材质特......
  • WPF 设备输入事件封装
    本文主要介绍WPF应用对鼠标输入、触摸屏触笔以及触摸事件的封装之前有简单说明设备输入类型 WPF屏幕点击的设备类型-唐宋元明清2188-博客园(cnblogs.com)1、鼠标- 通过Mouse相关的事件参数MouseButtonEventArgs中的数据,e.StylusDecice==null表示没有触摸设备,所以设备为......
  • Free Online 1080P Youtube Video Downloader All In One
    FreeOnline1080PYoutubeVideoDownloaderAllInOne免费在线1080PYoutube视频下载器https://yt5s.io/en93https://yt5s.biz/enxj100/demosIELTS雅思https://www.youtube.com/watch?v=GSnWFA3qnAg(......
  • CANoe_UDS-boorloader 自动化测试系列(六)基本功能:CAPL实现bin文件数据解析
    CANoe_UDS-booroader自动化测试系列(一)创建一个CANoe测试工程(测试节点的选选择)CANoe_UDS-booroader自动化测试系列(二)基本刷写流程CANoe_UDS-booroader自动化测试系列(三)基本功能:CAPL实现UDS协议下的CAN报文接收#解析#发送CANoe_UDS-booroader自动化测试系列(四)基本功能:CAPL实......
  • facefusion整合包cuda 环境报错解决: onnxruntime::ProviderLibrary::Get [ONNXRuntim
    在b站下载了一个up提供的facefusion整合包,运行go-web.bat报错报错信息如下:2024-08-1910:53:07.6316097[E:onnxruntime:Default,provider_bridge_ort.cc:1992onnxruntime::TryGetProviderInfo_CUDA]D:\a\_work\1\s\onnxruntime\core\session\provider_bridge_ort.cc:1637......
  • 【html+css 绚丽Loading】 - 000009 五行逆流珠
    前言:哈喽,大家好,今天给大家分享html+css绚丽Loading!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏+关注哦......