每次我启动WPF应用程序时JumpList都会重置

3
我在使用WPF中的JumpList时遇到了一些问题。当我在app.xaml中添加标签后,可以在跳转列表中看到任务,但是当我尝试向最近文件列表中添加项目时,新添加的项目从未出现。如果我创建一个名为“最近”的CustomCategory并手动添加JumpTask,则该任务将显示出来。但是,如果我重新启动应用程序,则新添加的JumpTask不再存在,只有测试任务。 澄清 最初,我遇到了JumpList.AddToRecentCategory根本无法工作的问题。它永远不会添加到最近列表中。Gayot Fow帮助解决了这个问题。但是问题仍然存在,即如果我手动使用自定义类别添加JumpTask,则所有最近的文件都会清除,如果我打开一个文件并调用addToRecent,则不会显示该文件。如果我删除在xaml中声明的JumpTask,则最近的文件将显示出来。
XAML:
<JumpList.JumpList>
    <JumpList ShowRecentCategory="True">

        <JumpTask Title="Test" Description="Test"
                  Arguments="/test" CustomCategory="Tasks" />
    </JumpList>

</JumpList.JumpList>

添加最近项目的C#代码

var jumpList = JumpList.GetJumpList(Application.Current);
if (jumpList == null) return;

//create a jump task
var jt = new JumpTask();

jt.Title = System.IO.Path.GetFileNameWithoutExtension(FileName);
jt.Description = jt.Title;
jt.CustomCategory = jt.Title;
jt.ApplicationPath = FileName;

//JumpList.AddToRecentCategory(jt);

jt.CustomCategory = "Recent";
jumpList.JumpItems.Add(jt);

jumpList.Apply();

无论我是从Visual Studio 2013(更新2)运行应用程序,还是从调试目录运行exe,这种情况都会发生。有人有任何想法为什么这不起作用吗?
我在某个地方读到过ClickOnce部署的应用程序不起作用,但我甚至不能在部署之前使其工作。
任何帮助将不胜感激,谢谢。
更新
Gayot Fow的答案让我解决了静态方法的问题。
JumpList.AddToRecentCategory(jt);

没有做任何事情。

我已经将我的AddToRecent代码更改如下:

var jumpList = JumpList.GetJumpList(Application.Current);
if (jumpList == null) return;


string title = System.IO.Path.GetFileNameWithoutExtension(FileName);
string programLocation = Assembly.GetCallingAssembly().Location;

var jt = new JumpTask
{
    ApplicationPath = programLocation,
    Arguments = FileName,
    Description = FileName,
    IconResourcePath = programLocation,
    Title = title
};
JumpList.AddToRecentCategory(jt);


jumpList.Apply();

问题

虽然最近文件的问题已经解决,但我仍然无法让它与名为“任务”的自定义类别共存。

在我的应用程序启动时,我调用以下代码:

var jumpList = JumpList.GetJumpList(Application.Current);
if (jumpList != null)
{

    string title = "New Document";
    string programLocation = Assembly.GetCallingAssembly().Location;

    var jt = new JumpTask
    {
        ApplicationPath = programLocation,
        Arguments = "/new",
        Description = title,
        IconResourcePath = programLocation,
        Title = title
    };
    jumpList.JumpItems.Add(jt);

    jumpList.Apply();
}

一旦调用此函数,最近类别将消失,任何添加最近项的调用都不会起作用。然而,我仍然看到我的“新建文档”任务 :/ 我是不是完全错了呢? 谢谢

跳转列表在调试下不可靠,因为它不是相同的可执行文件。 - Gayot Fow
谢谢,我已经考虑过了,但是当我手动从文件夹运行exe时,出现了同样的情况。在这种情况下,它确实是相同的EXE。 - Eric
之后,请尝试以管理员身份运行。 - Gayot Fow
好的,我尝试以管理员身份运行,但仍然是同样的情况。在xaml中声明的任务会替换我手动添加的任何任务,例如最近使用的文件,而JumpList.AddToRecentCategory静态方法根本没有起作用。也许我只能自己编写MRU并跳过JumpList,尽管我真的很想要它。 - Eric
我没有其他的想法。我可以从我的跳转列表中添加一些代码,但我不知道它是否有用。 - Gayot Fow
1个回答

5
以下是跳转列表的工作代码片段...
在 App.xaml 中...
<JumpList.JumpList>
    <JumpList
    ShowFrequentCategory="False" 
    ShowRecentCategory="False"
    JumpItemsRejected="OnJumpItemsRejected" 
    JumpItemsRemovedByUser="OnJumpItemsRemoved">
    </JumpList>
</JumpList.JumpList>

在App.xaml.cs文件中

    private void OnJumpItemsRejected(object sender, JumpItemsRejectedEventArgs e){}
    private void OnJumpItemsRemoved(object sender, JumpItemsRemovedEventArgs e){}

在编码中...

    public object PopulateJumpList(string directoryName)
    {
        try
        {
            string programLocation = Assembly.GetCallingAssembly().Location;
            var di = new DirectoryInfo(directoryName);
            var jt = new JumpTask
            {
                ApplicationPath = programLocation,
                Arguments = directoryName,
                Description = "Run at " + directoryName,
                IconResourcePath = programLocation,
                Title = di.Name
            };
            JumpList.AddToRecentCategory(jt);
            return jt;
        }
        catch (Exception ex)
        {
            return ex;
        }
    }

这个方法创建了一个跳转任务,形式为...

full executable path of the program |=> name of the directory where it was invoked

...通过静态方法AddToRecentCategory将此项添加到最近类别。这与您的代码不同,您将任务添加到跳转列表的本地副本中。必须提供应用程序路径的完全限定名称。另外,如评论中所述,它似乎在自己的安装目录中运行时效果最佳,并且每次覆盖可执行文件时都会删除跳转列表。在调试模式下(针对vshost.exe),使用它将无法可靠地工作。


好的,你的回答让我解决了最近文件未显示的问题。按照你的逻辑,我将ApplicationPath设置为程序的位置。将描述和标题更改为文件名,现在AddToRecentCategory静态方法可以使用了。但是,如果我还想在名为“Tasks”的类别中添加静态任务项,则仍然无法正确工作。一旦我添加它,最近的文件就会消失,并且它们不会被添加。有什么想法吗? - Eric
@Eric,我认为这是一个新问题。您原来的问题已经得到了回答,如果您回来扩展它、改变范围等,那对每个人来说都很尴尬。请参见http://meta.stackexchange.com/questions/188625/etiquette-for-russian-doll-questions。 - Gayot Fow
1
我的原始问题是,如果我在JumpList xaml中声明了一个项目,它会显示在列表中,但对AddToRecentCategory的调用会失败。这仍在发生。尽管以前我根本无法使AddToRecentCategory起作用。我曾尝试创建一个“最近”类别,因为至少那样我可以添加它,但每次应用程序启动时它都会清除。我无法让这两个一起工作。我需要一个任务列表和一个最近列表。 - Eric
没事了,它正在工作。我意识到声明中缺少CustomCategory。我添加了CustomCategory="Tasks",现在它可以工作了。谢谢。 - Eric
@Eric,如果可能的话,我想看看你的代码。这里很少有关于跳转列表的问题,分享知识是好事。 - Gayot Fow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接