通过运行时加载XAML XML?

12 .net c# wpf xaml

我们正在迁移到Winforms到基于WPF的解决方案.我们有自定义XML定义,用于在运行时构建Windows窗体.

由于XAML是基于XML的,我们是否可以使用XAML定义定义HelloWorldWindow.xml文件,并且可以将其加载到WPF应用程序中而不需要CSharp文件后面的任何代码?我们将在运行时附加钩子后面的代码.

如何在运行时附加代码?

Kis*_*mar 19

使用此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>
Run Code Online (Sandbox Code Playgroud)

使用以下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>
Run Code Online (Sandbox Code Playgroud)

将下面的C#代码粘贴到Button_Click的代码后面

  StreamReader mysr = new StreamReader(@"D:\Tempwin.xml");
        FrameworkElement  rootObject = XamlReader.Load(mysr.BaseStream) as FrameworkElement;
        content.Children.Add(rootObject);
Run Code Online (Sandbox Code Playgroud)

如果要在运行时加载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>
Run Code Online (Sandbox Code Playgroud)

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");
    }
Run Code Online (Sandbox Code Playgroud)


Fal*_*con 5

您可以像这样动态显示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
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅XamlReader类:http: //msdn.microsoft.com/en-us/library/ms613427%28v=VS.95%29.aspx