2014-04-08 18 views
0

,我的AppBar遇到很多重複代碼。除了登錄頁面之外,我的所有頁面都支持相同的AppBar。有什麼辦法來抽象的XAML代碼,還有xaml.cs代碼(例如,在點擊UI元素的修改)在我的當前C#WinRT MvvmLight應用程序中,在城市應用程序中創建一個常見的AppBar

除了登錄的頁面的XAML頁面:

<Page.TopAppBar> 
    <AppBar> 
    <!-- content here --> 
    </AppBar> 
</Page.TopAppBar> 
<Page.BottomAppBar> 
    <AppBar> 
    <!-- content here --> 
    </AppBar> 
</Page.BottomAppBar> 

任何xaml.cs除了登錄頁面頁面:

private void UserLogout_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { 
    //First we need to find out how big our window is, so we can center to it. 
    CoreWindow currentWindow = Window.Current.CoreWindow; 

    //Set our background rectangle to fill the entire window 
    rectBackgroundHide.Height = currentWindow.Bounds.Height; 
    rectBackgroundHide.Width = currentWindow.Bounds.Width; 
    rectBackgroundHide.Margin = new Thickness(0, 0, 0, 0); 

    //Make sure the background is visible 
    rectBackgroundHide.Visibility = Windows.UI.Xaml.Visibility.Visible; 

    //Now we figure out where the center of the screen is, and we 
    //move the popup to that location. 
    popupLogout.HorizontalOffset = (currentWindow.Bounds.Width/2) - (400/2); 
    popupLogout.VerticalOffset = (currentWindow.Bounds.Height/2) - (150/2); 
    popupLogout.IsOpen = true; 
    } 

回答

0

試試你AppBar添加到資源與一些關鍵的,例如在Page.Resources

<Page.Resources> 
    <AppBar x:Key="MyFavoriteAppBar" x:Shared="False" ... /> 
</Page.Resources> 

而在其它來源的使用是這樣的:

<Page.TopAppBar> 
    <ContentControl Content="{StaticResource MyFavoriteAppBar}" /> 
</Page.TopAppBar> 

<Page.BottomAppBar> 
    <ContentControl Content="{StaticResource MyFavoriteAppBar}" /> 
</Page.BottomAppBar> 

對於WPF它完美地工作,當你爲你的資源設置x:Shared="False",但在WinRT中沒有此相同的功能,但不支持此屬性。

因此,需要尋找替代品,因爲其中一個選項可以找到here

例子:

<Page.Resources> 
    <Style x:Name="MyFavoriteAppBar" TargetType="ContentControl"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ContentControl"> 
        <AppBar ... /> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Page.Resources> 

<Grid> 
    <Page.TopAppBar> 
     <ContentControl Style="{StaticResource MyFavoriteAppBar}" /> 
    </Page.TopAppBar> 

    <Page.BottomAppBar> 
     <ContentControl Style="{StaticResource MyFavoriteAppBar}" /> 
    </Page.BottomAppBar> 
</Grid> 

爲了Click事件成功地工作你的情況,你需要確定EventSetter的風格,或者使用命令。

+0

@theStig:我的答案可以幫到你嗎? –

相關問題