我有一个3页的数据透视页面,有两个应用程序栏按钮。但我希望在更改枢轴时,应用程序栏的第一个按钮应在不同的枢轴上执行不同的任务,第二个按钮将在所有枢轴上执行相同的任务。我这样做:
private void PivotControl_SelectionChanged(object sender,SelectionChangedEventArgs e) { ApplicationBarIconButton firstButton =(ApplicationBarIconButton)ApplicationBar.Buttons [0];
if (PivotControl.SelectedIndex == 0)
{
firstButton.IsEnabled = true;
firstButton.Click += new EventHandler(FirstPivotButton_Click);
}
else if (PivotControl.SelectedIndex == 1)
{
firstButton.IsEnabled = true;
firstButton.Click += new EventHandler(SecondPivotButton_Click);
}
else if (PivotControl.SelectedIndex == 2)
{
firstButton.IsEnabled = false;
}
}
void FirstPivotButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/PageA.xaml", UriKind.Relative));
}
void SecondPivotButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/PageB.xaml", UriKind.Relative));
}
但问题是PageA正在导航,但是从secondpivotbutton点击事件转到PageB时出现问题。请帮帮我
答案 0 :(得分:1)
使用这个出色的库来简化Windows Phone上ApplicationBar的使用 - AppBarUtils。 您可以在NuGet as well上找到它们。
还有一个很好的教程,如何使用这个库显示每个全景/枢轴项目的不同按钮:
http://allenlooplee.wordpress.com/2012/09/17/how-to-show-different-app-bar-for-different-pivotpano-item/
答案 1 :(得分:0)
以下是我在其中一个应用中执行此操作的方法。对于枢轴页面,按钮1是不同的,按钮2总是相同的,就像你一样。
在SelectionChanged事件上,更新按钮图标uri和text:
Private Sub statPivot_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles statPivot.SelectionChanged
Dim saveBtn As ApplicationBarIconButton = ApplicationBar.Buttons(0)
If statPivot.SelectedIndex = 0 Then
'calculation pane active
saveBtn.Text = "save"
saveBtn.IconUri = New Uri("/Assets/AppBar/appbar.save.rest.png", UriKind.Relative)
Else
'history pane active
saveBtn.Text = "clear"
saveBtn.IconUri = New Uri("/Assets/AppBar/appbar.refresh.rest.png", UriKind.Relative)
End If
End Sub
然后在onclick事件中,我检测到正在查看的透视窗格,并使用基本的if ... then ... else ...在同一个处理程序中运行不同的代码。
答案 2 :(得分:0)
如果您想在运行时动态更改应用栏按钮,可以执行以下操作:
<phone:PhoneApplicationPage.Resources>
<shell:ApplicationBar x:Key="appbar1" IsVisible="True">
<shell:ApplicationBarIconButton x:Name="abMain1" IconUri="/icons/appbar.favs.addto.rest.png" Text="blabla1"/>
</shell:ApplicationBar>
<shell:ApplicationBar x:Key="appbar2" IsVisible="True">
<shell:ApplicationBarIconButton x:Name="abMain2" IconUri="/icons/appbar.favs.addto.rest.png" Text="blabla2"/>
<shell:ApplicationBarIconButton x:Name="abMain3" IconUri="/icons/appbar.cancel.rest.png" Text="blabla3"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.Resources>
然后从代码中更改它:
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar) Resources["appbar1"];
和
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar) Resources["appbar2"];
您可以使用大量不同的AppBars
。
希望能帮到你。