欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpf Assembly.LoadFile dll GetType 反射 抛异常 不具有由 URI 识别的资源。

程序员文章站 2022-03-03 16:14:12
...
public static void LoadViewFromUri(this Window window, string baseUri)
        {
            try
            {
                var resourceLocater = new Uri(baseUri, UriKind.Relative);
                var exprCa = (PackagePart)typeof(Application).GetMethod("GetResourceOrContentPart", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { resourceLocater });
                var stream = exprCa.GetStream();
                var uri = new Uri((Uri)typeof(BaseUriHelper).GetProperty("PackAppBaseUri", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null, null), resourceLocater);
                var parserContext = new ParserContext
                {
                    BaseUri = uri
                };
                typeof(XamlReader).GetMethod("LoadBaml", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { stream, parserContext, window, true });

            }
            catch (Exception)
            {
                //log
            }
        }

   public partial class WinEditPedestrianNormalDeploy : Window
    {

        public WinEditPedestrianNormalDeploy()
        {
            //InitializeComponent();//屏蔽
            this.LoadViewFromUri("/Harzone.VideoData.RetrieveObjects;component/view/windows/wineditpedestriannormaldeploy.xaml");
        }
    }