【问题标题】:How to make navigation to the desired PivotItem using Prism? UWP App如何使用 Prism 导航到所需的 PivotItem? UWP 应用
【发布时间】:2015-12-02 11:58:38
【问题描述】:

我有一个菜单,当我单击一个菜单项时,我想导航到一个特定 PivotItem 的页面。 我想一定有这样的事情

_navigationService.Navigate(path**item=2**, ObjectContainer);

【问题讨论】:

  • 太棒了!玩得开心!如果您遇到困难,请随时在此处提出问题,并附上您尝试/研究过的内容以及为什么没有解决您的问题。阅读How to Ask了解更多指南
  • 如果你不知道答案/不想帮忙,请不要写任何东西。请不要泛滥。我一直在寻找解决方案,但仅在代码隐藏中找到了它。如果我知道如何使用 mvvm 来做,我就不会在这里写了

标签: c# xaml prism uwp


【解决方案1】:

我写了一个演示来解决你的问题。我看到你的项目中可能会用到一个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));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    相关资源
    最近更新 更多