Cannot find view for ViewModel

混江龙づ霸主 提交于 2019-12-10 02:48:01

问题


I have a wpf application using Caliburn.Micro. I have a view MyView:

<UserControl x:Class="ReferenceMaintenanceWorkspace.MyView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         >
  <UserControl.Resources>
 </UserControl.Resources>
 <TabControl x:Name="Items" > 
</TabControl>

I have also MyViewModel:

using System.ComponentModel.Composition;

namespace ReferenceMaintenanceWorkspace
{
[Export(typeof(MyViewModel))]
public class MyViewModel
{
  public MyViewModel()
  {
      base.DisplayName = "Reference Maintenance";
  }

Because of some reason, I get the following message on the tab control:

Cannot find view for ReferenceMaintenanceWorkspace.MyViewModel.

Could you please explain why this could happen? Thanks.


回答1:


Caliburn Micro is expecting certain file structure within your project. Your views and viewmodels should be in separate folders named Views and ViewModels.

Here is a nice Hello World example that describes this.




回答2:


Just for the future, it happens also after renaming classes/packages, but in view xaml file "x:Class" is not updated.




回答3:


You should override SelectAssemblies in bootstrapper and provide assembly name where your view lies in.




回答4:


make sure you have folders ViewModels and Views. Also make sure the names of your classes and usercontrols/windows also follow these namingconventions like:

  • = ViewModels
  • == YoloViewModel
  • = Views
  • == YoloView

If the views and viewmodels are located in a different assembly try this:

In your bootstrapper you need to add the assembly where the viewmodel/view is located:

protected override IEnumerable<Assembly> SelectAssemblies()
{
  var assemblies = base.SelectAssemblies().ToList();
  assemblies.Add(typeof(MyProject.Foo.ViewModels.YoloViewModel).Assembly);

  return assemblies;
}


来源:https://stackoverflow.com/questions/11083314/cannot-find-view-for-viewmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!