如何在新的 AppDomain 中运行 WPF 应用程序?执行程序集失败

发布于 2024-09-13 17:07:56 字数 1844 浏览 7 评论 0原文

我正在尝试使用应用程序域从控制台应用程序启动 WPF 应用程序, 但当我这样做时,我会收到意想不到的错误。

独立运行 WPF 应用程序是可行的。

此代码也有效:

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var path = string.Format("{0}AddressbookDesktop.exe", baseDirectory);
var processInfo = new ProcessStartInfo(path, "");
Process.Start(processInfo);    

但此代码失败并出现以下错误。错误似乎出现在构造函数中,它是空的:

var addressbookDomain = AppDomain.CreateDomain("addressbookDomain");
addressbookDomain.ExecuteAssembly("AddressbookDesktop.exe");

堆栈跟踪:

System.Windows.Markup.XamlParseException: Cannot create instance of 
'AddressbookMainWindow' defined in assembly 'AddressbookDesktop, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null'. Exception has been thrown
by the target of an invocation. Error in markup file 'AddressbookMainWindow.xaml' Line     1 Position 9.
---> System.Reflection.TargetInvocationException: Exception has been thrown by the
target of an invocation. ---> System.InvalidOperationException: The calling thread must 
be STA, because many UI components require this.

at System.Windows.Input.InputManager..ctor()
at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
at System.Windows.Input.InputManager.get_Current()
at System.Windows.Input.KeyboardNavigation..ctor()
at System.Windows.FrameworkElement.FrameworkServices..ctor()
at System.Windows.FrameworkElement.EnsureFrameworkServices()
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Control..ctor()
at System.Windows.Controls.ContentControl..ctor()
at System.Windows.Window..ctor()
at XX.YY.AddressbookDesktop.AddressbookMainWindow..ctor() in      C:\.....\AddressBookDesktop\AddressbookMainWindow.xaml.cs:line 15
--- End of inner exception stack trace ---

我想我做错了什么,但无法理解它是什么。 感谢您的任何帮助。

I'm trying to launch a WPF application from a Console application, using Application Domains,
but when I do, I receive unexpected errors.

Running the WPF application standalone, works.

This code works, too:

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var path = string.Format("{0}AddressbookDesktop.exe", baseDirectory);
var processInfo = new ProcessStartInfo(path, "");
Process.Start(processInfo);    

But this code fails with the error below. The error appears to be in the constructor, which is empty:

var addressbookDomain = AppDomain.CreateDomain("addressbookDomain");
addressbookDomain.ExecuteAssembly("AddressbookDesktop.exe");

Stack trace:

System.Windows.Markup.XamlParseException: Cannot create instance of 
'AddressbookMainWindow' defined in assembly 'AddressbookDesktop, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null'. Exception has been thrown
by the target of an invocation. Error in markup file 'AddressbookMainWindow.xaml' Line     1 Position 9.
---> System.Reflection.TargetInvocationException: Exception has been thrown by the
target of an invocation. ---> System.InvalidOperationException: The calling thread must 
be STA, because many UI components require this.

at System.Windows.Input.InputManager..ctor()
at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
at System.Windows.Input.InputManager.get_Current()
at System.Windows.Input.KeyboardNavigation..ctor()
at System.Windows.FrameworkElement.FrameworkServices..ctor()
at System.Windows.FrameworkElement.EnsureFrameworkServices()
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Control..ctor()
at System.Windows.Controls.ContentControl..ctor()
at System.Windows.Window..ctor()
at XX.YY.AddressbookDesktop.AddressbookMainWindow..ctor() in      C:\.....\AddressBookDesktop\AddressbookMainWindow.xaml.cs:line 15
--- End of inner exception stack trace ---

I guess I'm doing something wrong, but can't understand what it is.
Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-09-20 17:07:56

问题是 WPF 必须从 STA 线程运行(上面的内部异常之一说明了这一点)。我通过将 STAThreadAttribute 添加到我的 Main() 方法:

using System;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Console.WriteLine("Starting WpfApplication1.exe...");

        var domain = AppDomain.CreateDomain("WpfApplication1Domain");
        try
        {
            domain.ExecuteAssembly("WpfApplication1.exe");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            AppDomain.Unload(domain);
        }

        Console.WriteLine("WpfApplication1.exe exited, exiting now.");
    }
}

The problem is that WPF must be run from a STA thread (one of the inner exceptions above states this). I got this to work by adding the STAThreadAttribute to my Main() method:

using System;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Console.WriteLine("Starting WpfApplication1.exe...");

        var domain = AppDomain.CreateDomain("WpfApplication1Domain");
        try
        {
            domain.ExecuteAssembly("WpfApplication1.exe");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            AppDomain.Unload(domain);
        }

        Console.WriteLine("WpfApplication1.exe exited, exiting now.");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文