//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