2015-06-10 97 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