打开相关文件,例如记事本++标签功能'在WPF中

时间:2015-10-15 18:09:04

标签: c# wpf tabs associations notepad

这个问题是关于WPF应用程序的。 我想用我的自定义应用程序打开一些相关文件。 我找到了打开相关文件的方法。 但是,我仍然不知道如何处理重复的申请。

例如, 如果我点击' a.txt2',我的应用就会打开它。 我点击了' a2.txt2',它还会打开我应用的另一个流程。

我想只在第一个应用中获得第二次点击信息,而不是在点击多个相关文件时执行我的应用。 它类似于记事本++'打开文件到标签功能。

这是我的代码。

1 MainWindow.cs

Gdx.input.setInputProcessor(new GameInputListener() {

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {

                return false;
            }

            @Override
            public boolean touchDown(int x, int yy, int pointer, int button) {
                int y = Gdx.graphics.getHeight() - yy;
                // if sprite + 10 of px marge is touched
                if(mySpriteTouchable.isPressing(10, x, y)) {
                    // sprite is touched down
                }
                return false;
            }
}

2 app.xaml

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(MainContainer_Loaded);
    }
    void MainContainer_Loaded(object sender, RoutedEventArgs e)
    {
        string fn = "x";
        if (Application.Current.Properties["ArbitraryArgName"] != null)
        {
            fn = Application.Current.Properties["ArbitraryArgName"].ToString();

        }
        this.Title = fn;
    }
}

3 app.cs

<Application x:Class="sample.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         >
<Application.Resources>

</Application.Resources>
<!--
StartupUri="MainWindow.xaml"
Startup="App_Startup"--></Application>

感谢。

1 个答案:

答案 0 :(得分:1)

使用“Microsoft.VisualBasic”命名空间提供的底部单一单例实例技术:

using System;

namespace Sample
{
    public class Startup
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceApplicationWrapper wrapper = new SingleInstanceApplicationWrapper();
            wrapper.Run(args);
        }
    }

    public class SingleInstanceApplicationWrapper :
        Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        public SingleInstanceApplicationWrapper()
        {
            // Enable single-instance mode.
            this.IsSingleInstance = true;
        }

        // Create the WPF application class.
        private WpfApp app;
        protected override bool OnStartup(
            Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {


            app = new WpfApp();
            app.Run();

            return false;
        }

        // Direct multiple instances
        protected override void OnStartupNextInstance(
            Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs e)
        {
            if (e.CommandLine.Count > 0)
            {
                ((MainWindow)app.MainWindow).openFile(e.CommandLine[0]);
            }
        }
    }

    public class WpfApp : System.Windows.Application
    {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);

            // Load the main window.
            MainWindow main = new MainWindow();
            this.MainWindow = main;
            main.Show();

            // Load the document that was specified as an argument.
            if (e.Args.Length > 0) main.openFile(e.Args[0]);
        }



    }   
}