跳转列表 - UWP应用程序

时间:2017-11-28 18:19:05

标签: c# uwp windows-10 uwp-xaml

我在Microsoft / Windows Store中发布了一个UWP应用程序,我正在进行新的更新。我想在我的应用中添加一个跳转列表。我已经阅读了微软的文档,但我不明白如何为我的应用程序制作跳转列表。我想把我的跳转列表,我的应用程序的XAML页面。用户单击跳转列表项并转到XAML页面。我是怎么做到的?

1 个答案:

答案 0 :(得分:1)

我们假设您有一个名为SecondPageThirdPage的网页,您希望直接从JumpList导航。

首先,您需要将相应的项目添加到JumpList本身:

JumpList jumpList = await JumpList.LoadCurrentAsync();

jumpList.Items.Clear();
jumpList.Items.Add(JumpListItem.CreateWithArguments("GoToSecondPage", "Go directly to the 2nd page"));
jumpList.Items.Add(JumpListItem.CreateWithArguments("GoToThirdPage", "Visit 3rd page"));

await jumpList.SaveAsync();

执行此操作的标准位置是应用的OnLaunched方法(App.xaml.cs文件)。有关用户点击的JumpListItem的信息也会作为参数传递给此方法,因此您可能还希望将页面导航逻辑放入OnLaunched方法,并使其如下所示:

        if(e.Arguments == "GoToSecondPage")
        {
            rootFrame.Navigate(typeof(SecondPage));
        }
        else if(e.Arguments == "GoToThirdPage")
        {
            rootFrame.Navigate(typeof(ThirdPage));
        }

因此,您的App.xaml.cs文件将如下所示:

sealed partial class App : Application
{
    ...
    protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
        await ConfigureJumpList();

        ...

        if(e.Arguments == "GoToSecondPage")
        {
            rootFrame.Navigate(typeof(SecondPage));
        }
        else if(e.Arguments == "GoToThirdPage")
        {
            rootFrame.Navigate(typeof(ThirdPage));
        }
    }
    ...
    private async Task ConfigureJumpList()
    {
        JumpList jumpList = await JumpList.LoadCurrentAsync();

        jumpList.Items.Clear();
        jumpList.Items.Add(JumpListItem.CreateWithArguments("GoToSecondPage", "Go directly to the 2nd page"));
        jumpList.Items.Add(JumpListItem.CreateWithArguments("GoToThirdPage", "Visit 3rd page"));

        await jumpList.SaveAsync();
    }
}

如果您使用模板10,那么一切都是相同的,除了一些次要的代码更改以及您编辑OnStartAsync()而不是OnLaunched()方法的事实,因此最终结果为{{1}文件应该如下所示:

App.xaml.cs

如果您想在sealed partial class App : Template10.Common.BootStrapper { ... public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args) { if ((args as LaunchActivatedEventArgs).Arguments == "GoToSecondPage") await NavigationService.NavigateAsync(typeof(Views.SecondPage)); else if ((args as LaunchActivatedEventArgs).Arguments == "GoToThirdPage") await NavigationService.NavigateAsync(typeof(Views.ThirdPage)); else await NavigationService.NavigateAsync(typeof(Views.MainPage)); await ConfigureJumpList(); } ... private async Task ConfigureJumpList() { JumpList jumpList = await JumpList.LoadCurrentAsync(); jumpList.Items.Clear(); jumpList.Items.Add(JumpListItem.CreateWithArguments("GoToSecondPage", "Go directly to the 2nd page")); jumpList.Items.Add(JumpListItem.CreateWithArguments("GoToThirdPage", "Visit 3rd page")); await jumpList.SaveAsync(); } } 中显示自定义图标,那么您需要做的就是替换这行代码

JumpListItem

以下行

jumpList.Items.Add(JumpListItem.CreateWithArguments("GoToSecondPage", "Go directly to the 2nd page"));

其中var secondPageItem = JumpListItem.CreateWithArguments("GoToSecondPage", "Go directly to the 2nd page"); secondPageItem.Logo = new Uri("ms-appx:///Icons/Page2Icon.png"); jumpList.Items.Add(secondPageItem); 是图标的路径(在此示例中为项目的图标文件夹中的 Page2Icon.png 文件)。