【问题标题】:JumpList in C# application (recent files)C# 应用程序中的 JumpList(最近的文件)
【发布时间】:2014-07-04 08:50:14
【问题描述】:

目前我正在开发一个应用程序并想添加一个 Windows (7) JumpList。我遵循了几个教程并研究了文档,但我不知道如何完成这项工作。简而言之:我想要最近选择的文件的最新列表。因此,在关闭应用程序后,用户可以轻松地使用我的应用程序打开最近的文件。我已经实现了一些文件关联机制。

是否可以分享一些代码/教程如何解决上述问题?

提前谢谢你!

*我已经尝试过接下来的几个项目/教程:

*Coding 4 Fun的代码可以,但是不知道怎么开发最近的文件列表。

【问题讨论】:

  • 你为什么不赞成我的问题?请解释一下。
  • 可能,反对者希望看到一些代码来详细说明,否则它似乎是免费的。可能,不确定。
  • 我认为各个网站的链接就足够了,因为我已经尝试了该代码。

标签: c# windows-7 taskbar


【解决方案1】:

您可以查看this 文章。您需要在跳转列表中显示结果,而不是在 WPF 中显示结果。

为什么不尝试将最近打开的文件名存储在数据库或 xml 文件中并读取它以设置跳转列表。

例如。

private void ReportUsage()

   {

       XmlDocument myXml = new XmlDocument();

       myXml.Load(historyXml);

       string list = historyXml;

       jumpList.ClearAllUserTasks();

       foreach (XmlElement el in myXml.DocumentElement.ChildNodes)

       {

           string s = el.GetAttribute("url");

           JumpListLink jll = new JumpListLink(Assembly.GetEntryAssembly().Location, s);

           jll.IconReference = new IconReference(Path.Combine("C:\\Program Files\\ACS Digital Media\\TOC WPF Browser\\Icon1.ico"), 0);

           jll.Arguments = el.GetAttribute("url");

           jumpList.AddUserTasks(jll);

       }

       jumpList.Refresh();

   }



或者初学者的解决方案是将所有文件路径保留到给定最大容量的队列中,并在运行时将它们添加到菜单项中。对不起,我没有时间写整个代码。

【讨论】:

  • 重新发明轮子是一种耻辱。 Microsoft 已经提供了一种用于存储最近文件的实现。
  • 是的,我同意。但我认为他希望他的跳转列表只包含他的应用程序可以打开的特定文件,而不是完整的列表,他必须在其中搜索他的关联文件,然后仅将这些文件显示为跳转列表项。因为创建自己的文件类型的最近列表将帮助他添加更多与他的应用程序相关的功能。
  • 感谢您提供这个出色的解决方案!这是比跳单更好的事件。
  • 太好了,你喜欢我的回答。希望对您有所帮助。并感谢赏金。
【解决方案2】:

正如your second article 中所述,您的应用程序必须注册为目标文件扩展名的处理程序,否则您的 Jumplist 的最新类别将不会显示。您可以找到更多关于文件关联注册的详细信息there

您可以手动注册您的应用程序,但您需要管理员权限,因此不建议这样做,或者为您的应用程序创建一个设置项目,如coding 4 fun文章中所述,或者您可以让用户关联文件扩展名。

这是一个无需注册即可在 Windows 7 下使用的示例,只需右键单击我要加载的文本文件并选择“打开方式”并浏览到我的应用程序。

该示例需要 Windows API 代码包

public partial class Form1 : Form
{
    [STAThread]
    static void Main(string[] args)
    {
        var file = args != null && args.Length > 0 ? args[0] : string.Empty;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1(file));
    }

    public Form1()
        : this(string.Empty)
    {
    }

    public Form1(string file)
    {
        InitializeComponent();

        Open(file);
    }

    [DllImport("user32.dll")]
    private static extern uint RegisterWindowMessage(string message);

    private uint wmTBC;

    /// <summary>
    /// Registers the window message for notification when the taskbar button is created.
    /// </summary>
    /// <param name="e">The event args.</param>
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        wmTBC = RegisterWindowMessage("TaskbarButtonCreated");
    }

    /// <summary>
    /// Handles the window message for notification of the taskbar button creation.
    /// </summary>
    /// <param name="m">The window message.</param>
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == wmTBC)
        {
            OnTaskbarButtonCreated();
        }
    }

    /// <summary>
    /// Override this method to recieve notification when the taskbar button is created on Windows 7 machines and above.
    /// </summary>
    protected void OnTaskbarButtonCreated()
    {
        if (TaskbarManager.IsPlatformSupported)
        {
            jumpList = JumpList.CreateJumpList();
            jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;
            jumpList.Refresh();
        }
    }

    JumpList jumpList;

    private void openToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Open(ofd.FileName);
            }
        }
    }

    private void Open(string file)
    {
        try
        {
            if (!string.IsNullOrEmpty(file) && File.Exists(file))
            {
                textBox1.Text = File.ReadAllText(file);

                if (TaskbarManager.IsPlatformSupported)
                {
                    jumpList.AddToRecent(file);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.openToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(12, 27);
        this.textBox1.Multiline = true;
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(796, 306);
        this.textBox1.TabIndex = 0;
        // 
        // menuStrip1
        // 
        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.openToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Name = "menuStrip1";
        this.menuStrip1.Size = new System.Drawing.Size(820, 24);
        this.menuStrip1.TabIndex = 1;
        this.menuStrip1.Text = "menuStrip1";
        // 
        // openToolStripMenuItem
        // 
        this.openToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.openToolStripMenuItem1});
        this.openToolStripMenuItem.Name = "openToolStripMenuItem";
        this.openToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
        this.openToolStripMenuItem.Text = "File";
        // 
        // openToolStripMenuItem1
        // 
        this.openToolStripMenuItem1.Name = "openToolStripMenuItem1";
        this.openToolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
        this.openToolStripMenuItem1.Text = "Open";
        this.openToolStripMenuItem1.Click += new System.EventHandler(this.openToolStripMenuItem1_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(820, 345);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Name = "Form1";
        this.Text = "Form1";
        this.menuStrip1.ResumeLayout(false);
        this.menuStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem1;
}

【讨论】:

  • 感谢您的回复,但我认为手动将文件与应用程序关联起来对用户不友好。 Jerin的解决方案更好。尽管有人对您的回答投了赞成票。
  • 没问题。只是为了说明清楚:手动注册建议更多用于测试目的。推荐的方法肯定是将此作为安装过程中的一个步骤,如其中一个链接中所建议的那样。
猜你喜欢
  • 2014-11-09
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-16
  • 2011-02-24
相关资源
最近更新 更多