首页 文章

跳转列表 - UWP应用程序

提问于
浏览
0

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

1 回答

  • 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() 方法的事实,因此 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);
    

    其中 "ms-appx:///Icons/Page2Icon.png" 是图标的路径(在此示例中为项目的Icons文件夹中的Page2Icon.png文件) .

相关问题