c#应用程序中的跳转列表(最近的文件)



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

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

提前感谢!

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

  • http://www.codeproject.com/Articles/103913/How-to-Create-a-Custom-Jumplist-with-Custom-Events
  • http://channel9.msdn.com/coding4fun/articles/Windows-7-Jump-Lists
  • http://csharp-tricks-en.blogspot.nl/2011/10/create-jumplist-using-c.html

* Coding 4 Fun的代码可以工作,但是我不知道如何开发一个最近的文件列表

您可以查看这篇文章。而不是在WPF中显示结果,您需要在jumplist中显示结果。

为什么不试着将最近打开的文件名存储在数据库或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();
   }



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

正如第二篇文章中所描述的,您的应用程序必须注册为目标文件扩展名的处理程序,否则将不会显示您的Jumplist最近的类别。你可以在这里找到更多关于文件关联注册的详细信息。

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

这是一个在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;
}

最新更新