XamlReader動態使用xaml
阿新 • • 發佈:2019-01-31
xamlload先在xaml做出一個grid,命名xgrid
<Page
x:Class="xamlload.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:xamlload"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="xgrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="button_Click"/>
</Grid>
</Page>
用和前面寫
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="button_Click"/>
private void button_Click(object sender , RoutedEventArgs e)
{
string usingXaml = " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" ";
string contentXaml = "<Button Content=\"Button\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\"" ;
string marginXaml= "Margin=\"10,50,0,0\"/>";
Button b = XamlReader.Load(contentXaml+usingXaml + marginXaml) as Button;
xgrid.Children.Add(b);
}