2015-06-10 96 views
0

我将一个WPF应用程序移植到WinRT中。旧的应用程序有一部分需要像Image,MediaElement,Xaml Page等等;将其作为UIElement;然后接收类将使用VisualBrush将其渲染到按钮上。WinRT控件渲染XAML UIElement

不幸的是WinRT没有VisualBrush。我已经尝试设置内容到UIElement等。我也读了RenderTargetBitmap,但我不认为它会工作,因为我也有视频内容。

有什么办法可以让一个控件接受一个UIElement并正确呈现它吗?

+0

[VisualBrush可能重复不再适用于Windows 8 Metro Apps?](http://stackoverflow.com/questions/9044066/visualbrush-no-longer-works-for-windows-8-metro-apps) – WiredPrairie

回答

1

根据您想要达到的目标,您可以在Button.Content属性中设置您的UIElement。

Button.Content属性可以接受任何UIElement。

例如,你可以做到以下几点:

MainPage.xaml中

<Page ...> 
<StackPanel ...> 
    <Button x:Name="myButton" Width="200" Height="200" 
     HorizontalContentAlignment="Stretch" 
     VerticalContentAlignment="Stretch" > 

     <Button.Content> 
      <local:Page2 /> 
     </Button.Content> 
    </Button> 
</StackPanel> 
</Page> 

Page2.xaml

<Page...> 
    <Grid ...> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <Rectangle Fill="Red" /> 
     <Rectangle Fill="Yellow" Grid.Column="1"/> 
     <Rectangle Fill="Blue" Grid.Row="1"/> 
     <Button Content="Click Me" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center"/> 
    </Grid> 
</Page> 

或者从后面的代码:

MainPage.xaml中.cs

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     myButton.Content = new Page2(); 
    } 
} 
+0

是的,我想到了..但是从上流阶层传来的元素已经把这个类作为父类了。 VisualBrush复制UIElement,所以它工作,但我不能直接添加元素。总的来说这是非常混乱的代码。重新开始。 – diAblo