无法在新的AppDomain中创建UserControl

本文关键字:创建 UserControl AppDomain | 更新日期: 2023-09-27 18:29:44

当我将程序集(xxx.dll)加载到新的AppDomain并尝试在那里创建UserControl时,会发生异常:

Could not load file or assembly 'xxx.resources' or one of its dependencies.

当我将程序集加载到Main AppDomain时,它运行良好
为什么会出现异常?

public void InitializeComponent() {
if (_contentLoaded) {
      return;
}
_contentLoaded = true;

//此处发生异常

System.Uri resourceLocater = new System.Uri("/Company.AddInApp;component/controls/usercontrol.xaml", System.UriKind.Relative);
#line 1 "..'..'..'Controls'UserControl1.xaml" stem.Windows.Application.LoadComponent(this, resourceLocater);
#line default
#line hidden
}

无法在新的AppDomain中创建UserControl

我发现了错误:

var setup = new AppDomainSetup
{
        ApplicationBase = rootAddInsPath,
         ........
};
var appDomain = AppDomain.CreateDomain(...)
//I don't must do this here!!!
appDomain.AssemblyResolve += (sender, args) =>
{
     ....
}
var managerType = typeof(AddInLoadManager);
var manager =(AddInLoadManager)appDomain.CreateInstanceAndUnwrap(managerType.Assembly.FullName, managerType.FullName);

"AppDomain.AssemblyResolve"事件的正确位置位于"AddInLoadManager"类内部。

感谢您的帮助@YK1