天天看點

XamlReader 動态加載XAML

原文: 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的命名空間有點差別.