2013-07-31 22 views
0

我試圖創建一個透視控件,並且要補充一個不同的ApplicationBar的Pivot控件的項目。我試圖按照MSDN this演練,但它似乎是有這個代碼中的錯誤:在一個數據透視控制中使用許多應用程序吧

private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     switch (((Pivot)sender).SelectedIndex) 
     { 
      case 0: 
       ApplicationBar = ((ApplicationBar)Application.Current.Resources["CountingAppBar"]); 
       break; 

      case 1: 
       ApplicationBar = ((ApplicationBar)Application.Current.Resources["SavingAppBar"]); 
       break; 
     } 
    } 

的錯誤是應用程序任務欄是一個類,它被用來作爲變量,所以我試圖創建一個在switch語句之前的實例,這裏是我所做的:

private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     ApplicationBar appBar; 
     switch (((Pivot)sender).SelectedIndex) 
     { 
      case 0: 
       appBar = ((ApplicationBar)Application.Current.Resources["CountingAppBar"]); 
       break; 

      case 1: 
       appBar = ((ApplicationBar)Application.Current.Resources["SavingAppBar"]); 
       break; 
     } 
    } 

但它似乎沒有工作。 我的編程水平仍然是初學者,如果答案是詳細的,我們將不勝感激。 謝謝。

+0

代碼你確定你鏈接到正確的頁面?它適用於Windows Phone,雖然我不確定,但ApplicationBar可能只是Windows Phone應用程序的固有屬性。所以我不確定它在WPF或Silverlight中起作用。 – AkselK

+0

可能只是你沒有調用構造函數的事實,即'''ApplicationBar appBar = new ApplicationBar();''' – T045T

回答

1

http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394044%28v=vs.105%29.aspx

你爲什麼不只使用一個應用程序任務欄添加或刪除基於選定的樞軸控制ApplicationBarButtons?考慮到許多應用程序都這麼做,應該很簡單。 PS:從個人經驗來看,通過C#代碼添加應用程序欄......我在更改XAML應用程序欄時遇到了問題。

+0

完全同意這兩個,儘管當然如果你使用代碼和UI的分離是更清晰的XAML爲AppBar。 – T045T

+0

確實....我喜歡XAML並在其中創建控件,但由於某種原因,AppBar在XAML中變得太不可預測,並且在嘗試在代碼中訪問它時繼續拋出NullReferenceExceptions –

0

嘿似乎很晚,但我遵循相同的msdn教程。您可能無法獲得所需的結果,因爲您需要將選擇已更改的事件與數據透視表項相關聯。

<controls:Pivot SelectionChanged="Pivot_SelectionChanged_2" > 

</controls:Pivot> 
3

這是非常簡單的。你可以使用不同的appbar每個pivote項目。試試下面

<phone:Pivot> 

 <i:Interaction.Triggers> 
      <appBarUtils:SelectedPivotItemChangedTrigger> 
       <appBarUtils:SelectedPivotItemChangedTrigger.SelectionMappings> 
        <appBarUtils:SelectionMapping SourceIndex="0" TargetIndex="0"/> 
       </appBarUtils:SelectedPivotItemChangedTrigger.SelectionMappings> 

       <appBarUtils:SwitchAppBarAction> 
        <appBarUtils:AppBar Id="0" BackgroundColor="{StaticResource AppBarBg}" ForegroundColor="{StaticResource Foreground}"> 
         <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.home.png" Text="home" Command="{Binding HomeNavigationCommand}"/> 
        </appBarUtils:AppBar> 

        <appBarUtils:AppBar Id="1" BackgroundColor="{StaticResource AppBarBg}" ForegroundColor="{StaticResource Foreground}"> 
         <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.home.png" Text="home" Command="{Binding HomeNavigationCommand}"/> 
        </appBarUtils:AppBar> 

        <appBarUtils:AppBar Id="2" BackgroundColor="{StaticResource AppBarBg}" ForegroundColor="{StaticResource Foreground}"> 
         <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.home.png" Text="home" Command="{Binding HomeNavigationCommand}"/> 
         <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.money.png" Text="collection" Command="{Binding CollectionPageCommand}"/> 
         <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.check.rest.png" Text="ok" Command="{Binding OrderConfirmationButtonCommand}"/> 
        </appBarUtils:AppBar> 

        <appBarUtils:AppBar Id="3" BackgroundColor="{StaticResource AppBarBg}" ForegroundColor="{StaticResource Foreground}"> 
         <appBarUtils:AppBarButton x:Name="ConfirmationAppBarButton" IconUri="/Assets\Images\appbar.cancel.rest.png" Text="cancel" Command="{Binding OrderCancelButtonCommand}"/> 
         <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.check.rest.png" Text="ok" Command="{Binding OrderConfirmationButtonCommand}" IsEnabled="{Binding Model.EnableCheck,Mode=TwoWay}" /> 
        </appBarUtils:AppBar> 

       </appBarUtils:SwitchAppBarAction> 
      </appBarUtils:SelectedPivotItemChangedTrigger> 
     </i:Interaction.Triggers> 
    </phone:Pivot> 
相關問題