类之间的调用方法

时间:2013-08-28 01:03:27

标签: c# wpf

我需要从addDownload()

调用主应用程序类MainWindow.xaml.cs内的App.xaml.cs方法

有关应用程序的一些信息:

  • 在我的App.xaml.cs我有代码只能运行一个应用程序实例。(不是我的代码。我找到了它)

    using System;
    using System.Diagnostics;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows;
    using DownloadManager;
    namespace DownloadManager
    {
        public partial class App : Application
    {
        private static readonly Semaphore singleInstanceWatcher;
        private static readonly bool createdNew;
    
        static App()
        {
            // Ensure other instances of this application are not running.
            singleInstanceWatcher = new Semaphore(
                0, // Initial count.
                1, // Maximum count.
                Assembly.GetExecutingAssembly().GetName().Name,
                out createdNew);
    
    
    
            if (createdNew)
            {
                // This thread created the kernel object so no other instance
                // of this application must be running.
            }
            else
            {
            // This thread opened an existing kernel object with the same
            // string name; another instance of this app must be running now.
    
            // Gets a new System.Diagnostics.Process component and the
            // associates it with currently active process.
                Process current = Process.GetCurrentProcess();
    
                foreach (Process process in
                     Process.GetProcessesByName(current.ProcessName))
                {
                    if (process.Id != current.Id)
                    {
                        NativeMethods.SetForegroundWindow(
                            process.MainWindowHandle);
                        NativeMethods.ShowWindow(process.MainWindowHandle,
                            WindowShowStyle.Restore);
                        break;
                    }
                }
    
                // Terminate this process and gives the underlying operating 
                // system the specified exit code.
                Environment.Exit(-2);
            }
        }
    
        private static class NativeMethods
        {
            [DllImport("user32.dll")]
            internal static extern bool SetForegroundWindow(IntPtr hWnd);
            [DllImport("user32.dll")]
            internal static extern bool ShowWindow(IntPtr hWnd,
                WindowShowStyle nCmdShow);
        }
    
        /// <summary>
        /// Enumeration of the different ways of showing a window.</summary>
        internal enum WindowShowStyle : uint
        {
            Hide = 0,
            ShowNormal = 1,
            ShowMinimized = 2,
            ShowMaximized = 3,
            Maximize = 3,
            ShowNormalNoActivate = 4,
            Show = 5,
            Minimize = 6,
            ShowMinNoActivate = 7,
            ShowNoActivate = 8,
            Restore = 9,
            ShowDefault = 10,
            ForceMinimized = 11
        }
    }
    }
    
  • 我每次都需要将命令行参数传递给addDownload()方法,无论应用程序是否已经运行。

到目前为止我尝试了什么:

  • 首先,我删除了App.xaml中的Startup入口点,因此我可以在代码隐藏中进行管理

  • 然后,如果应用程序未运行,请创建一个MainWindow的新实例,并显示它:

    MainWindow MW = new MainWindow();
    MW.Show();
    
  • 获取命令行参数,并将其传递给addDownload()方法:

    string[] args = Environment.GetCommandLineArgs();
    if(args.Length > 1)
    {
        MW.addDownload(args[1]);
    }
    

好的,这部分完美无缺。

但正如我所说,如果应用程序已在运行,我还需要传递命令行参数。获取命令行参数与以前相同,但将其传递给当前MainWindow实例的addDownload()方法,不是,我找不到工作方式的麻烦;

我试过了:

try
{
    var main = App.Current.MainWindow as MainWindow;
    string[] args = Environment.GetCommandLineArgs();
    if (args.Length > 1)
    {
        main.addDownload(args[1]);
    }
}
catch(Exception ex)
{
    MessageBox.Show(String.Format("{0}\n{1}", ex.GetType().ToString(), ex.Message));
}

但我得到一个NullReferenceException ..我想在main声明..我不知道如何调试Visual Studio中的这种特殊情况。

有任何帮助吗?我做错了什么?

1 个答案:

答案 0 :(得分:0)

您得到NullReferenceException,因为您想要访问不同进程中存在的对象(MainWindow)的实例,而您无法执行此操作。您需要做的是某种InterProcess通信,有很多方法可以做到这一点,其中一种方法是使用.NET远程处理(可能不是最好的方式)。

以下是一些快速搜索的链接:

http://www.codeproject.com/Articles/17606/NET-Interprocess-Communication

What is the best choice for .NET inter-process communication?

http://msdn.microsoft.com/en-us/library/kwdt6w2k(v=vs.100).aspx