2013-06-29 28 views
1

在其代表LayoutAwarePage我xy.xaml文件,我有这样的两个元素重用元素(赢商店应用)

<StackPanel x:Name="LeftCommands" Orientation="Horizontal" Grid.Column="2" Margin="0,0,100,0" HorizontalAlignment="Right"> 
    <Button x:Name="BragButton" HorizontalAlignment="Left"/> 
</StackPanel> 

<Page.TopAppBar> 
    <AppBar x:Name="PageAppBar" Padding="10,0,10,0" Height="120" Opacity="0.98"> 
     <ItemsControl x:Name="groupTopMenuBar" Margin="116,0,40,0"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <StackPanel Orientation="Horizontal"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Grid Margin="0,0,0,0"> 
         <Button Click="UpperMenu_Click" 
           Style="{StaticResource TextPrimaryButtonStyle}" 
           Height="Auto" Margin="20,0,20,0" 
           CommandParameter="{Binding Group.MenuItemID}"> 
          <StackPanel> 
           <Image Source="{Binding Group.IconURL}" Width="40" 
             Height="40" VerticalAlignment="Top" Stretch="UniformToFill" /> 
           <TextBlock Text="{Binding Group.Name}" HorizontalAlignment="Left" 
              VerticalAlignment="Bottom" 
              Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" 
              Style="{StaticResource TitleTextStyle}" FontSize="16" /> 
          </StackPanel> 
         </Button> 
        </Grid> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </AppBar> 
</Page.TopAppBar> 

我需要使用这些元素我的应用程序的每一页,我不想在应用程序的每个.xaml文件中写这段代码,所以我想问问是否有任何方法如何做到这一点。

谢谢。

回答

1

你想在多个页面中分享您AppBar。不幸的是,在Windows Store应用程序中,使用StaticResource引用在App.xaml中定义的AppBar是不可能的,就像Windows Phone的情况一样。 有两种方式如何做到这一点:

  1. 在您的主页面内创建另一个框架,并在此框架中执行所有导航。 Check this MSDN article
  2. 创建AppBar与内容(按钮)用户控件,每个页面上添加TopAppBar并设置其内容发送到用户控件。在StackOverflow answer中建议采用这种方法。

我可能会看到页面导航小问题。如果你要离开的用户控件托管框架,其存储在App.xaml.cs OnActivated方法创建App类的一些静态属性的框架实例。例如public static Frame RootFrame { get; private set; }并由App.RootFrame = new Frame()设置。从您的用户控件后面的代码导航只需要调用是这样的:App.RootFrame.Navigate()。 这种方法是由菲利普Skakun here on StackOverflow建议。