2010-11-02 41 views
11

Chúng tôi đang chuyển sang Winforms thành giải pháp dựa trên WPF. Chúng tôi có định nghĩa XML tùy chỉnh được sử dụng để tạo biểu mẫu cửa sổ khi chạy.Tải XAML XML qua thời gian chạy?

Vì XAML dựa trên XML, chúng ta có thể định nghĩa tệp HelloWorldWindow.xml với định nghĩa XAML và nó có thể được tải vào ứng dụng WPF mà không có bất kỳ mã nào sau tệp CSharp không? Chúng tôi sẽ đính kèm mã sau móc tại thời gian chạy.

Cách đính kèm mã sau thời gian chạy?

+0

Đây có phải là tệp xml mà bạn đang nói đến thực sự là X hợp lệ không Tệp AML? –

+0

@Steve, vâng, đó là tệp xaml hợp lệ. Nhưng chúng tôi muốn thực thi mã được đính kèm khi chạy. –

+0

Got một giải pháp làm việc áp dụng hầu hết các khái niệm MVVM: [Giải pháp của tôi] [1] [1]: http://stackoverflow.com/questions/9021677/loading-xaml-at-runtime-using- the-mvvm-pattern-in-wpf/9033544 # 9033544 –

Trả lời

18

Tạo một tập tin XML Tempwin.xml sử dụng XAML này

<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> 

Tạo một sa Ứng dụng với XAML dưới đây mple 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> 

Dán dưới C# mã trong codebehind các Button_Click

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

nếu bạn muốn tải XAML khi chạy bạn không thể đưa ra bất cứ mã sau XAML của bạn tập tin. Vì vậy, tôi đã xóa dấu x: attribute lớp trước khi tạo xml

Sự kiện Hooking ....

<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"); 
    } 
4

Bạn có thể hiển thị XAML động như thế này:

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 

Xem lớp XamlReader để biết thêm thông tin: http://msdn.microsoft.com/en-us/library/ms613427%28v=VS.95%29.aspx

1

Tôi đã thực hiện tải XAML trong thời gian chạy, đây là một ví dụ ngắn

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>"; 
Các vấn đề liên quan