Python.net - 远程控制 WPF 应用程序 - WPF 资源问题

问题描述

我们正在尝试通过 Python.net 使用 Python 脚本远程控制 WPF 应用程序。 使用 Windows 窗体应用程序运行良好,可以启动 WPF 测试应用程序,通信正常,但只要使用单个 WPF 资源字典,资源加载器就会引发异常(未找到资源)。只有在 (User)Controls/Windows 中直接声明的资源似乎有效。

有没有人遇到过这样的场景?是否存在有关资源使用的任何已知限制?

与此同时,我找到了解决方案。在显式调用 App.InitializeComponent() 之后,所有资源都被正确找到并加载。资源字典通过包 URI 引用。

public class RemoteStart
{
    public Thread AppThread { get; private set; }

    public RemoteController RemoteController { get; private set; }

    public App App { get; private set; }

    /// <summary>
    /// This is the entry point from python to start the application. Its non-blocking.
    ///
    ///  remoteStart = RemoteStart()
    ///  remoteStart.Start()
    /// 
    /// </summary>
    public void Start()
    {
        AppThread = new Thread(new ThreadStart(() =>
                                               {
                                                   try
                                                   {
                                                       App = new App();

                                                       RemoteController = RemoteController;

                                                       // Thats the important call!
                                                       App.InitializeComponent();

                                                       // Configured start object starts or the application bootstrapper 
                                                       // sets up the object tree and starts the UI
                                                       App.Run();
                                                   }
                                                   catch (Exception e)
                                                   {
                                                       Console.WriteLine(e);
                                                   }

                                               }));

        // UI components require single threaded appartment
        AppThread.SetApartmentState(ApartmentState.STA);

        // Run the app
        AppThread.Start();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)