XamlReader 动态加载XAML

XAML:

<Grid  xmlns:x="http://schemas.microsoft.com/client/2006" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

	<Button Content="子窗口按钮" HorizontalAlignment="Left" Margin="112,98,0,0" VerticalAlignment="Top" />

	<Label Content="Label" HorizontalAlignment="Left" Margin="112,169,0,0" VerticalAlignment="Top"/>

</Grid>

CS:

using (FileStream s = new FileStream(@"C:\Users\Lin.net\Desktop\WpfApplication1\Xaml\Test.xaml", FileMode.Open))

            {

                Grid grid = XamlReader.Load(s) as Grid;

                Window win = new Window1();

                win.Content = grid;

                win.ShowDialog();

            }

注意:
xmlns:x="http://schemas.microsoft.com/client/2006"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
以上两个命名空间必须加入到XAML中间(WPF).Silverlight的命名空间有点区别.

 

你可能感兴趣的:(reader)