简体   繁体   中英

Caliburn.Micro WindowManager Cannot find view

I have done some searching and I can't find anyone with my specific problem.

I have a Caliburn.Micro project and I successfully have a main view with sub-views inside it which is not a problem. My View Models are in a different assembly to my views.

This meant I had to override SelectAssemblies to include my view models project:

protected override IEnumerable<Assembly> SelectAssemblies()
    {
        var assemblies = base.SelectAssemblies().ToList();
        assemblies.Add(typeof(OrderViewModel).Assembly);

        return assemblies;
    }

Now, this is where my confusion starts. I successfully have a OrderView showing the OrderViewModel. Inside that there is a KeyboardViewModel with a KeyboardView. This all works fine so caliburn is finding the right assemblies etc.

However when I come to use the window manager to display a new view/viewmodel which is passed into the order view. I am getting a screen with the text "Cannot find view model for XX.ViewModels.Model."

This is my OrderViewModel

[Export(typeof(OrderViewModel))]
public class OrderViewModel : Screen
{
        private readonly IWindowManager windowManager;
        private ISession session;

        [ImportingConstructor]
        public OrderViewModel(IWindowManager windowManager, KeyboardViewModel keyboardViewModel)
        {
            TillDatabase.CreateInstance(ApplicationConfiguration.Instance.DatabaseConnectionString);
            this.windowManager = windowManager;
            this.Keyboard = keyboardViewModel;
            this.Keyboard.Order = this;
            this.Keyboard.Home();
        }


        public void ChangePriceBand()
        {
            windowManager.ShowWindow(new PriceBandSelectionViewModel(this));
        }

}

The thing is, I even tried this in ChangePriceBand

 windowManager.ShowWindow(new OrderViewModel(this.windowManager, new KeyboardViewModel()));

And this gets the same error. Even though a view has already been associated with the OrderViewModel previously!!

This is the PriceBandSelectionViewModel just in case.

[Export(typeof(PriceBandSelectionViewModel))]
public class PriceBandSelectionViewModel : Screen
{
    private OrderViewModel order;

    [ImportingConstructor]
    public PriceBandSelectionViewModel(OrderViewModel order)
    {
        this.order = order;
    }

    public ObservableCollection<PriceBandButtonViewModel> Buttons
    {
        get
        {
            var list = new ObservableCollection<PriceBandButtonViewModel>();
            var priceBands = this.order.Session.QueryOver<Application_Model_PriceBand>().List();
            foreach (var priceBand in priceBands)
            {
                PriceBandButtonViewModel button = new PriceBandButtonViewModel(priceBand, this);
                list.Add(button);
            }
            return list;
        }
    }


    public void ProcessButtonClick(Application_Model_PriceBand button)
    {
        this.order.ChangeCurrentPriceBand(button);
        base.TryClose();
    }

}

I'm just really confused to how Caliburn is setting up my main view, but the window manager isn't even though its the same ViewModel?

have you tried to remove OrderViewModel or put a breakpoint there, cant find view error might happen if it encountered error when initialising the exported class

public PriceBandSelectionViewModel()
{
    // this.order = order;
}

or add

assemblies.Add(typeof(PriceBandSelectionViewModel).Assembly);

This may be the same problem as I am experiencing as described here: Caliburn.Micro HelloWindowManager Sample - View location not working

To see if it is the same problem, try changing the call from

windowManager.ShowWindow(new PriceBandSelectionViewModel(this)); 

to

windowManager.ShowDialog(new PriceBandSelectionViewModel(this));.  

In my case, ShowDialog was able to locate the view no problem, but ShowWindow and ShowPopup were not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM