如何使用 Prism 导航到所需的 PivotItem? UWP应用程序
How to make navigation to the desired PivotItem using Prism? UWP App
我有一个菜单,当我单击菜单项时,我想导航到特定 PivotItem 的页面。
我想一定有这样的东西
_navigationService.Navigate(path**item=2**, ObjectContainer);
我写了一个demo来解决你的问题。我看到你的项目中可能会用到navigationService,我没用过,这里也没有用到任何菜单,只是一个很简单的demo,好吗?
是的,您使用带有参数传递的导航是正确的,所以问题是您如何在 Pivot
页面中接收此参数并处理此参数。
为此,您需要覆盖 Pivot
页面中的 OnNavigatedTo()
方法:
MainPage.xaml:
<StackPanel>
<Button Content="PivotItem 1" Click="Button_Click1" VerticalAlignment="Top" Margin="30"/>
<Button Content="PivotItem 2" Click="Button_Click2" VerticalAlignment="Center" Margin="30"/>
<Button Content="PivotItem 3" Click="Button_Click3" VerticalAlignment="Bottom" Margin="30"/>
</StackPanel>
PivotPage.xaml:
<Pivot Title="Test" FontSize="60" x:Name="pivotcontrol">
<PivotItem Header="Item1">
<StackPanel Orientation="Vertical">
<TextBlock Text="PivotItem1" FontSize="30" Margin="60"/>
</StackPanel>
</PivotItem>
<PivotItem Header="Item2">
<StackPanel Orientation="Vertical">
<TextBlock Text="PivotItem2" FontSize="30" Margin="60"/>
</StackPanel>
</PivotItem>
<PivotItem Header="Item3">
<StackPanel Orientation="Vertical">
<TextBlock Text="PivotItem3" FontSize="30" Margin="60"/>
</StackPanel>
</PivotItem>
</Pivot>
<Button Content="Back to MainPage" Margin="60" Click="item_back"/>
MainPage.xaml.cs:
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PivotPage), "Item1");
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PivotPage), "Item2");
}
private void Button_Click3(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PivotPage), "Item3");
}
PivotPage.xaml.cs:
string selectitem = null;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
string getdata = e.Parameter.ToString();
selectitem = getdata;
}
if (selectitem.Equals("Item1"))
{
pivotcontrol.SelectedIndex = 0;
}
else if (selectitem.Equals("Item2"))
{
pivotcontrol.SelectedIndex = 1;
}
else
{
pivotcontrol.SelectedIndex = 2;
}
}
public PivotPage()
{
this.InitializeComponent();
}
private void item_back(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MainPage));
}
我有一个菜单,当我单击菜单项时,我想导航到特定 PivotItem 的页面。 我想一定有这样的东西
_navigationService.Navigate(path**item=2**, ObjectContainer);
我写了一个demo来解决你的问题。我看到你的项目中可能会用到navigationService,我没用过,这里也没有用到任何菜单,只是一个很简单的demo,好吗?
是的,您使用带有参数传递的导航是正确的,所以问题是您如何在 Pivot
页面中接收此参数并处理此参数。
为此,您需要覆盖 Pivot
页面中的 OnNavigatedTo()
方法:
MainPage.xaml:
<StackPanel>
<Button Content="PivotItem 1" Click="Button_Click1" VerticalAlignment="Top" Margin="30"/>
<Button Content="PivotItem 2" Click="Button_Click2" VerticalAlignment="Center" Margin="30"/>
<Button Content="PivotItem 3" Click="Button_Click3" VerticalAlignment="Bottom" Margin="30"/>
</StackPanel>
PivotPage.xaml:
<Pivot Title="Test" FontSize="60" x:Name="pivotcontrol">
<PivotItem Header="Item1">
<StackPanel Orientation="Vertical">
<TextBlock Text="PivotItem1" FontSize="30" Margin="60"/>
</StackPanel>
</PivotItem>
<PivotItem Header="Item2">
<StackPanel Orientation="Vertical">
<TextBlock Text="PivotItem2" FontSize="30" Margin="60"/>
</StackPanel>
</PivotItem>
<PivotItem Header="Item3">
<StackPanel Orientation="Vertical">
<TextBlock Text="PivotItem3" FontSize="30" Margin="60"/>
</StackPanel>
</PivotItem>
</Pivot>
<Button Content="Back to MainPage" Margin="60" Click="item_back"/>
MainPage.xaml.cs:
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PivotPage), "Item1");
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PivotPage), "Item2");
}
private void Button_Click3(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PivotPage), "Item3");
}
PivotPage.xaml.cs:
string selectitem = null;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
string getdata = e.Parameter.ToString();
selectitem = getdata;
}
if (selectitem.Equals("Item1"))
{
pivotcontrol.SelectedIndex = 0;
}
else if (selectitem.Equals("Item2"))
{
pivotcontrol.SelectedIndex = 1;
}
else
{
pivotcontrol.SelectedIndex = 2;
}
}
public PivotPage()
{
this.InitializeComponent();
}
private void item_back(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MainPage));
}