首页 > 解决方案 > 使用 .Net 标准项目在 WPF 应用程序中执行方法

问题描述

我的.Net 标准项目正在使用 .NET 调用WPF 应用程序中的方法reflection

我的 .NET 标准项目中的方法调用代码...

            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Google.Cast.DesktopV2.exe");
            var asm = Assembly.LoadFile(path);
            Type t = asm.GetType("Google.Cast.DesktopV2.Utility.PlayMedia");
            MethodInfo methodInfo = t.GetMethod("FindMediaPlayer");

            var o = Activator.CreateInstance(t);
           methodInfo.Invoke(o);
            

这是我在WPF Application中的方法。

    public MediaElement FindMediaPlayer()
    {
        return ((MainWindow)System.Windows.Application.Current.MainWindow).VideoPlayer;
    }

VideoPlayer是我的 XAML 文件中的MediaElement 。我收到错误“调用线程无法访问此对象,因为另一个线程拥有它。” ",无论如何我可以使用反射来实现这一点吗?

标签: wpf

解决方案


推荐阅读