Cannot find view for ViewModel

15,365

Solution 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.

Solution 2

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

Solution 3

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

Solution 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;
}

Solution 5

I argee,I think fix this from two way. 1.In solution you should have two folders , one name is "Views" another is "ViewModels".

2.In your bootstrapper you need to add the assembly where the viewmodel or view is located:

protected override IEnumerable<Assembly> SelectAssemblies()
{
    var assemblies = base.SelectAssemblies().ToList();
    assemblies.Add(typeof(Project.ViewsNamespace.SomeView).Assembly);
   return assemblies;
}
Share:
15,365
David Shochet
Author by

David Shochet

Updated on June 18, 2022

Comments

  • David Shochet
    David Shochet almost 2 years

    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.

  • David Shochet
    David Shochet almost 12 years
    The application I am maintaining already had various views and viewmodels. None of them existed in folders with names Views and ViewModels, and yet they worked. They all existed in different class library projects though, and I am adding a new one called ReferenceMaintenanceWorkspace. I wonder if the projects should have some particular settings that I am not aware of...
  • ShadeOfGrey
    ShadeOfGrey almost 12 years
    Maybe it's being done manually Caliburn.Micro.View.SetModel(NameView, NameViewModel);
  • ShadeOfGrey
    ShadeOfGrey almost 12 years
    or in the View <UserControl xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Mi‌​cro" cal:Bind.Model="SomeNamespace.NameViewModel" />
  • David Shochet
    David Shochet almost 12 years
    No, such things are not used in the application. I tried it anyway, and it didn't help :(
  • ShadeOfGrey
    ShadeOfGrey almost 12 years
    Can you share the contents of the bootstrapper Configure method? Or the contents of IEnumerable<Assembly> SelectAssemblies() method (if it's there).
  • David Shochet
    David Shochet almost 12 years
    protected override IEnumerable<System.Reflection.Assembly> SelectAssemblies() { var assyList = new List<Assembly>{System.Reflection.Assembly.GetExecutingAssemb‌​ly()}; var dir = new DirectoryInfo("."); _workspaceAssyList = new List<Assembly>(); _workspaceAssyList.AddRange(dir.GetFiles("*Workspace.dll").S‌​elect(file => Assembly.LoadFile(file.FullName))); assyList.AddRange(_workspaceAssyList.ToArray()); return assyList }
  • David Shochet
    David Shochet almost 12 years
    I debugged this method, and could see my new project in assyList.
  • ShadeOfGrey
    ShadeOfGrey almost 12 years
    I'm kinda out of ideas :). One final shot in the dark. Is ViewLocator or ViewModelLocator used somewhere (if yes how)?
  • David Shochet
    David Shochet almost 12 years
    ViewLocator is used in one place, but it seems to have nothing to do with my problem, as that method is not called for other views that do work.
  • David Shochet
    David Shochet almost 12 years
    Figured it out. Instead of copying and pasting View and ViewModel files from another place, I created a brand new UserControl for the View and a class for the ViewModel... and it worked! I still don't know what the difference is though...
  • ShadeOfGrey
    ShadeOfGrey almost 12 years
    Glad you managed to getting done. If you ever find the root cause leave a message or create an answer, since this one is not really helpful :).
  • Stephen Holt
    Stephen Holt over 10 years
    This "expecting certain file structure within your project" seems like a major limitation of Caliburn Micro to me. Surely the whole point of MVVM is to decouple the ViewModel and View, such that they can live in isolation from each other. With this model, not only are they tightly coupled, they are done so by "convention" rather than explicitly.
  • juFo
    juFo almost 7 years
    Thanks @ShadeOfGrey didn't know about the View.SetModel
  • Corey
    Corey over 4 years
    The blog is no longer present.
  • ShadeOfGrey
    ShadeOfGrey over 4 years
    Thanks @Corey Replaced with link from webarchive.
  • DevCaf
    DevCaf almost 4 years
    If you didn't add a window in views, that would not work. You should check the view in views folder. That should be the window or usercontrol.