Opening next file with the same instance of my app

泄露秘密 提交于 2019-12-13 10:36:59

问题


I associated .pdf files with my C#.NET WPF app in Windows.

If I open the pdf file by clicking on it, array "param" is:

string[] param = Environment.GetCommandLineArgs();

Which contains two paths:

1) Path to my app (param[0])
2) Path to opened (param[1])

I have set in the C# code (app.xaml.cs) that only one instance of my app may be opened in Windows. If I try to open a second instance, the main window of the first instance is activated.

But now, if I open the next pdf file by click on it “param” contains the path to the first file logically, therefore I can not open the next file :0(.

How should I solve this problem? I don’t want to move to the next instance of the app!

Here is my app.xaml.code

public partial class App : Application
{
    App()
    {
        InitializeComponent();
    }

    [STAThread]
    static void Main()
    {
        SingleInstanceManager manager = new SingleInstanceManager();
        manager.Run(new[] { "test" });

    }
}

public class SingleInstanceManager : WindowsFormsApplicationBase
{
    SingleInstanceApplication app;

    public SingleInstanceManager()
    {
        this.IsSingleInstance = true;
    }

    protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
    {
        // First time app is launched
        app = new SingleInstanceApplication();
        app.Run();
        return false;
    }

    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
    {
        // Subsequent launches
        base.OnStartupNextInstance(eventArgs);            
        app.Activate();
    }
}

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

        // Create and show the application's main window
        MainWindow window = new MainWindow();
        window.Show();
    }

    public void Activate()
    {            
        // Reactivate application's main window  
        this.MainWindow.WindowState = WindowState.Normal;            
        this.MainWindow.Activate();
        ((MainWindow)this.MainWindow).SpracujStartovacieParametre();
    }
}

回答1:


Here is my solution:

public partial class App : Application
    {
        App()
        {
            InitializeComponent();
        }

        [STAThread]
        static void Main()
        {
            SingleInstanceManager manager = new SingleInstanceManager();
            //manager.Run(new[] { "test" });
            manager.Run(Environment.GetCommandLineArgs());

        }
    }

public class SingleInstanceManager : WindowsFormsApplicationBase
{
    SingleInstanceApplication app;

    public SingleInstanceManager()
    {
        this.IsSingleInstance = true;
   }

   protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
    {
        // First time app is launched
        app = new SingleInstanceApplication();
        app.Run();
        return false;
    }

    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
    {
        // Subsequent launches
        base.OnStartupNextInstance(eventArgs);
        //MessageBox.Show("Event arguments:"+ eventArgs.ToString());            
        app.Activate(eventArgs.CommandLine.ToArray<string>());
    }
}

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

        // Create and show the application's main window
        MainWindow window = new MainWindow();
        window.Show();
    }

    public void Activate(string[] eventArgs)
    {

        ((MainWindow)this.MainWindow).SpracujCommandLineArgs(eventArgs);
        this.MainWindow.WindowState = WindowState.Maximized;            
        this.MainWindow.Activate();             

    }
}


来源:https://stackoverflow.com/questions/57572454/opening-next-file-with-the-same-instance-of-my-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!