WPF Caliburn Micro Bootstrapper在另一个项目中找不到实例。

huangapple go评论90阅读模式
英文:

WPF Caliburn Micro Bootstrapper not found instance from another project

问题

Caliburn.Micro 版本 4.0.212。
项目版本 .NET6

使用 Caliburn.Micro 3.0 和 .Net Framework 4.7.2 时可以工作。

当我尝试打开位于另一个项目中的简单 viewModel 时,我遇到了一个错误:"该组件没有通过 URI 标识的资源"。
我使用了 Caliburn Micro 的示例:https://github.com/Caliburn-Micro/Caliburn.Micro/tree/master/samples/tutorals/WPF/Wpf.Tutorial

为了重现这个错误,我在解决方案中创建了另一个项目:"CommonView"。
我将 AboutViewModel 从主项目移动到这个项目中,如下图所示:

解决方案资源管理器视图

在启动引导程序中,我需要修改 SelectAssemblies 方法,以添加来自这个新项目的文件。

protected override IEnumerable<Assembly> SelectAssemblies()
{
    var assemblies = new List<Assembly>();
    assemblies.AddRange(base.SelectAssemblies());

    string[] fileEntries = Directory.GetFiles(Directory.GetCurrentDirectory());

    foreach (string fileName in fileEntries)
    {
        if (!fileName.Contains(".conf") && fileName.Contains("CommonView.dll"))
        {
            assemblies.Add(Assembly.LoadFile(fileName));
        }
    }

    return assemblies;
}

现在,当我从按钮调用 About 类时,Caliburn 尝试获取实例,但找不到,所以我会得到错误。

protected override object GetInstance(Type service, string key)
{
    var instance = _container.GetInstance(service, key);    
    //instance is null
    return instance;
}

然而,如果我在调用 CategoryViewModel 时进行调试,它会正确返回实例。

我看到了这篇帖子,Juan Carlos 在其中重写了 InitializeComponent(),但我认为应该有一个更简单的方法:https://stackoverflow.com/questions/7646331/the-component-does-not-have-a-resource-identified-by-the-uri

提前感谢您的帮助。
祝好!

英文:

Caliburn.Micro version 4.0.212.
Projects version .NET6

With Caliburn.Micro 3.0 and .Net Framework 4.7.2 it works

I am getting an error when I try to open a simple viewModel located in another project: "The component does not have a resource identified by the URI"
I am using the Caliburn Micro example: https://github.com/Caliburn-Micro/Caliburn.Micro/tree/master/samples/tutorals/WPF/Wpf.Tutorial

To reproduce the error I created another project to the solution: "CommonView"
I moved AboutViewModel from the main project to this one as I show in the image below:

Solution Explorer View

In the bootstrapper I need to modify the SelectAssemblies method to add the files from this new project

protected override IEnumerable&lt;Assembly&gt; SelectAssemblies()
         {
             var assemblies = new List&lt;Assembly&gt;();
             assemblies.AddRange(base.SelectAssemblies());

             string[] fileEntries = Directory.GetFiles(Directory.GetCurrentDirectory());

             foreach (string fileName in fileEntries)
             {
                 if (!fileName.Contains(&quot;.conf&quot;) &amp;&amp; fileName.Contains(&quot;CommonView.dll&quot;))
                 {
                     assemblies.Add(Assembly.LoadFile(fileName));
                 }
             }

             return assemblies;
         }

Now, when I call the About class from the button, Caliburn tries to get the instance, but it is not found so I get the error.

protected override object GetInstance(Type service, string key)
          {
                var instance = _container.GetInstance(service, key);    
                //instance is null
                return instance;
          }

However if I debug when CategoryViewModel is called, it returns the instance correctly

I saw this post where Juan Carlos overrides InitializeComponent(), but I think It should have a easier aproachment: https://stackoverflow.com/questions/7646331/the-component-does-not-have-a-resource-identified-by-the-uri

Thank you in advance.
Regards

答案1

得分: 0

问题出在程序集的加载上,应该像这样加载:
assemblies.Add(Assembly.LoadFrom(fileName));

https://learn.microsoft.com/en-us/archive/blogs/suzcook/loadfile-vs-loadfrom

长命百岁,繁荣昌盛。

英文:

The problem is in the loading of the assembly, which should be like this
assemblies.Add(Assembly.LoadFrom(fileName));

https://learn.microsoft.com/en-us/archive/blogs/suzcook/loadfile-vs-loadfrom

Live long and prosper

huangapple
  • 本文由 发表于 2023年7月27日 17:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定