creare un file XML Tempwin.xml utilizzare questo XAML
<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>
Creare un sa mple WPF Applicazione con il codice XAML sotto
<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>
Incollare il sottostante codice C# in codebehind del Button_Click
StreamReader mysr = new StreamReader(@"D:\Tempwin.xml");
FrameworkElement rootObject = XamlReader.Load(mysr.BaseStream) as FrameworkElement;
content.Children.Add(rootObject);
se si desidera caricare XAML in fase di esecuzione, non si può dare alcun codice dietro il vostro XAML file. Così ho rimosso i x: attributo di classe prima di creare l'xml
Eventi Aggancio ....
<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");
}
fonte
2010-11-02 12:50:53
Si tratta di un file xml di cui si sta parlando effettivamente una X valida File AML? –
@Steve, sì è un file xaml valido. Ma vogliamo che l'implementazione del codice sia allegata in fase di runtime. –
Hai una soluzione di lavoro di applicare la maggior parte dei concetti MVVM: [La mia soluzione] [1] [1]: http://stackoverflow.com/questions/9021677/loading-xaml-at-runtime-using- the-MVVM-modello-in-WPF/9033544 # 9033544 –