Application.Current"零"在控制台应用程序 [英] Application.Current "null" in console application

查看:278
本文介绍了Application.Current"零"在控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图用一个WPF组件,它使用Application.Current从一个WPF应用程序,但由于多种原因,我从来没有叫Application.Run(也不是一个选项)。其结果是一个NullReferenceException

I'm currently trying to use a WPF component that makes use of Application.Current from a WPF application, however due to several reasons I never call Application.Run (nor is that an option). The result is a NullReferenceException.

基本上,我试图显示来自这将是一个控制台应用程序相同的WPF窗口的多个实例。 任何意见(在C#/ F#和code样品)会受到欢迎!

I'm basically trying to display multiple instances of the same WPF window from what would be a console application. Any advice (and code samples in C#/F#) would be welcome!

在此先感谢

推荐答案

只是提供一个替代解决方案。 它可以继续运行的应用程序没有任何窗户打开。对我来说,这种感觉少的hackish。 :) <一href="http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx">http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx

Just to offer an alternative solution. It is possible to keep an application running without any windows open. To me this feels less 'hackish'. :) http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx

public class AppCode : Application
{
   // Entry point method
   [STAThread]
   public static void Main()
   {
      AppCode app = new AppCode();
      app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
      app.Run();
      ...
      app.Shutdown();
   }
}

编辑: 确定这让有点麻烦。 Application.Run将阻止,因此它需要在它自己的线程中运行。 当它在自己的线程中运行,你的主线程和你的UI线程之间的任何互动都最好由Application.Current.Dispatcher.Invoke完成。下面是一些工作code,假定你有一个类继承自应用。我使用的是改良的App.xaml / App.xaml.cs一个WPF项目模板为您创建,获得ResourceDictionaries好看的处理是免费的。

Ok this got a bit cumbersome. Application.Run will block, so it needs to run in its own thread. When it does run in its own thread, any interaction between your main thread and your ui thread had best be done by Application.Current.Dispatcher.Invoke. Here is some working code, that assumes you have a class that inherits from Application. I'm using a modified App.xaml/App.xaml.cs that a WPF project template creates for you, to get nice handling of ResourceDictionaries for free.

public class Program
{
  // Entry point method
  [STAThread]
  public static void Main()
  {
     var thread = new System.Threading.Thread(CreateApp);
     thread.SetApartmentState(System.Threading.ApartmentState.STA);
     thread.Start();

     // This is kinda shoddy, but the thread needs some time 
     // before we can invoke anything on the dispatcher
     System.Threading.Thread.Sleep(100);

     // In order to get input from the user, display a
     // dialog and return the result on the dispatcher
     var result = (int)Application.Current.Dispatcher.Invoke(new Func<int>(() =>
        {
           var win = new MainWindow();
           win.ShowDialog();
           return 10;
        }), null);

     // Show something to the user without waiting for a result
     Application.Current.Dispatcher.Invoke(new Action(() =>
     {
        var win = new MainWindow();
        win.ShowDialog();
     }), null);

     System.Console.WriteLine("result" + result);
     System.Console.ReadLine();

     // This doesn't really seem necessary 
     Application.Current.Dispatcher.InvokeShutdown();
  }

  private static void CreateApp()
  {
     App app = new App();
     app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
     app.Run();
  }
}

这篇关于Application.Current&QUOT;零&QUOT;在控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆