[英]Loading XAML XML through runtime?
我們正在遷移到Winforms到基於WPF的解決方案。 我們有自定義XML定義,用於在運行時構建Windows窗體。
由於XAML是基於XML的,我們可以定義一個帶有XAML定義的HelloWorldWindow.xml文件,是否可以將其加載到WPF應用程序中而不需要CSharp文件后面的任何代碼? 我們將在運行時附加鈎子后面的代碼。
如何在運行時附加代碼?
使用此XAML創建XML文件Tempwin.xml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Background="Transparent" >
<Border Background="Black" CornerRadius="10" BorderThickness="4" BorderBrush="RoyalBlue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="Sample Text" Foreground="White" Margin="2"></TextBlock>
<TextBox Grid.Row="1" Margin="5"> </TextBox>
<TextBlock Text="Sample Text 1" Grid.Row="2" Foreground="White" Margin="2"></TextBlock>
<TextBox Grid.Row="3" Margin="5"></TextBox>
<Ellipse Fill="Red" Height="100" Width="100" Grid.Row="4" Margin="0,10,0,0"></Ellipse>
</Grid>
</Border>
使用以下xaml創建示例WPF應用程序
<Window x:Class="WpfApplication12.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="600">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Height="25" Width="100" Margin="2" Click="Button_Click"> Show Content</Button>
<Grid x:Name="content" Grid.Row="1" Margin="2">
</Grid>
</Grid>
將下面的C#代碼粘貼到Button_Click的代碼后面
StreamReader mysr = new StreamReader(@"D:\Tempwin.xml");
FrameworkElement rootObject = XamlReader.Load(mysr.BaseStream) as FrameworkElement;
content.Children.Add(rootObject);
如果要在運行時加載xaml,則無法在XAML文件后面添加任何代碼。 所以我在創建xml之前刪除了x:Class屬性
活動掛鈎....
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Background="Transparent" >
<Border Background="Black" CornerRadius="10" BorderThickness="4" BorderBrush="RoyalBlue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="Sample Text" Foreground="White" Margin="2"></TextBlock>
<TextBox Grid.Row="1" Margin="5"> </TextBox>
<TextBlock Text="Sample Text 1" Grid.Row="2" Foreground="White" Margin="2"></TextBlock>
<TextBox Grid.Row="3" Margin="5"></TextBox>
<Ellipse Fill="Red" Height="100" Width="100" Grid.Row="4" Margin="0,10,0,0"></Ellipse>
<Button Grid.Row="5" Height="25" Content="Event added at Runtime" x:Name="btnTest"></Button>
</Grid>
</Border>
Button ButtoninXAML;
private void Button_Click(object sender, RoutedEventArgs e)
{
StreamReader mysr = new StreamReader(@"D:\Tempwin.xml");
FrameworkElement rootObject = XamlReader.Load(mysr.BaseStream) as FrameworkElement;
ButtoninXAML = LogicalTreeHelper.FindLogicalNode(rootObject, "btnTest") as Button;
ButtoninXAML.Click += new RoutedEventHandler(Button_Click1);
content.Children.Add(rootObject);
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
MessageBox.Show("Added At Runtime");
}
您可以像這樣動態顯示Xaml:
string text = @"<TextBlock Text='test' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' />";
// Convert to stream
// You can also just stream the xaml from a file, using a FileStream
MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(text));
// Convert to object
TextBlock block = (TextBlock)System.Windows.Markup.XamlReader.Load(stream);
//... now you can put that TextBlock somewhere, for example in your main Window
有關詳細信息,請參閱XamlReader類: http : //msdn.microsoft.com/en-us/library/ms613427%28v=VS.95%29.aspx
我已經在運行時加載了XAML,這是一個簡短的例子
Grid grd = new Grid();
var grdEncoding = new ASCIIEncoding();
var grdBytes = grdEncoding.GetBytes(myXAML);
grd = (Grid)XamlReader.Load(new MemoryStream(grdBytes));
Grid.SetColumn(grd, 0);
Grid.SetRow(grd, 0);
parentGrid.Children.Add(grd);
private String myXAML = @" <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Margin='30 10 30 65' VerticalAlignment='Bottom'>" +
"<Label Content='Date: 1-Feb-2013' FontFamily='Arial' FontSize='12' Foreground='#666666' HorizontalAlignment='Left'/>" +
"<Label Content='4' FontFamily='Arial' FontSize='12' Foreground='#666666' HorizontalAlignment='Center'/>" +
"<Label Content='Hello World' FontFamily='Arial' FontSize='12' Foreground='#666666' HorizontalAlignment='Right'/>" +
"</Grid>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.