尝试使用Xamlreader在运行时加载Xaml元素

时间:2017-05-09 21:02:19

标签: wpf xaml dynamic loading xamlreader

这是我的C#,它似乎加载Xaml就好了,所有的孩子都在R1C3Viewbox以及它们包含的数据。但屏幕上没有显示任何内容。

ParserContext context = new ParserContext();
context.XmlnsDictionary.Add("", 
"http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.XmlnsDictionary.Add("x", 
"http://schemas.microsoft.com/winfx/2006/xaml");

using (FileStream fs = new FileStream("R1C3Viewbox.xaml", FileMode.Open))
    {
      R1C3Viewbox = (Viewbox)XamlReader.Load(fs, context);
    }

xaml文件包含:

<Viewbox Name="R1C3Viewbox" Grid.Row="1" Grid.Column="6" Grid.ColumnSpan="1" 
Grid.RowSpan="1">
  <Border Name="R1C3Border" BorderBrush="Black" CornerRadius="3" 
  BorderThickness="1" Background="#FFFFC602" Height="40" Width="40" 
  Margin="3,3,0,0">
    <Grid Name="R1C3Grid">
      <TextBlock Name="R1C3TextBlock1" FontFamily="Arial" FontSize="10" 
       FontWeight="Bold" Text="RAPID" Foreground="Black" 
       Margin="4,12,0,15"/>
      <TextBlock Name="R1C3TextBlock2" FontFamily="Arial" FontSize="10" 
      FontWeight="Bold" Text="OVER" Foreground="Black" Margin="5,23,5,3"/>
      <Polygon Name="R1C3LED" Points="0,0 15,0 0,15" Stroke="#FFED1C24" 
       StrokeThickness="1"> 
       <Polygon.Fill> <RadialGradientBrush> <GradientStop Color="White" 
       Offset="2.5"/> <GradientStop Color="Black"/> </RadialGradientBrush> 
       </Polygon.Fill>
       </Polygon>
       <Button Name="R1C3Button" Background="Transparent" 
        BorderBrush="Transparent"></Button>
      </Grid>
     </Border>
   </Viewbox>

任何想法都会非常感激。

0 个答案:

没有答案