1. 程式人生 > >XamlReader 動態載入XAML

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的名稱空間有點區別.