Open owner-less window using .Show() -- 'ViewModel is not present as a data context for any registered views'
See original GitHub issueSo firstly, this is my first time delving into MVVM and WPF, and I’m migrating a winforms app. I’ve already refactored the program’s business logic into a dll, and have been building ViewModels into that dll.
For the time being here is my general setup:
Business Logic Assembly
namespace RSTCore
{
// Contains the core logic to intialize the static dictionaries used by the rest of the dll, as well as housing the IDialogService
public class Program { }
}
namespace RSTCore.ViewModels
{
//all my view models that point to business logic within this dll.
// This assembly will be used by old winforms forms until they have properly converted, hence being a separate project.
}
UI Assembly
namespace RST
{
// All WPF Views located within this namespace. This is a separate project in my solution that will house the updated UI.
// On application startup, assigns RSTCore.Program.DialogueService to a new object with an appropriate IDialogTypeLocator
}
namespace RST.WinformsUI
{
// This is the namespace all the old windows that are eventually going to be deprecated currently reside in.
// Once again, different project
}
The View I am trying to open:
<Window x:Class="RST.RoboQueueWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ViewModels="clr-namespace:RSTCore.ViewModels;assembly=RoboSoftwareTool_Core"
xmlns:local="clr-namespace:RST"
mc:Ignorable="d"
xmlns:md="https://github.com/fantasticfiasco/mvvm-dialogs"
md:DialogServiceViews.IsRegistered="True"
d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=ViewModels:RoboQueueViewModel}"
Title="RoboQueueWindow" Height="450" Width="800">
How I’m trying to open it: Static Method within the RoboQueueViewModel class that calls the DialogueService to open a new standalone window
/// <summary>
/// Create a new ViewModel and start the copy operation
/// </summary>
public static void CreateNonModalWindow(IRoboQueue roboQueue)
{
var vm = new RoboQueueViewModel() { ObjectModel = roboQueue };
Program.DialogueService.Show(vm, vm);
}
Error Message:
Message = "View model of type 'RSTCore.ViewModels.RoboQueueViewModel' is not present as data context on any registered view. Please register the view by setting DialogServiceViews.IsRegistered=\"True\" in your XAML."
The goal: Open a new window that doesn’t really need to care about any of the other windows currently open. Since this is a non-modal window, it doesn’t really need an ‘owner’ window.
What am I doing wrong here? Can this library open up a non-modal (standalone) window? My goal (Don’t know if this is proper or not) is to have the DialogueService built into the dll where the viewmodels are housed, so that the ICommands can open views as needed without caring about the actual type of the view being opened. I figured I would try this instead of writing my own WindowFactory class that utilize a bunch of interfaces.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
Yea, that is actually what I’m ending up doing. I’ve reworked my initializing method to accept the viewmodel as a parameter to update the splash, then once its complete I swap the actual main form in place of the splash
You can always play around with having the splash screen initially being the main window, and then open up the real main window when initializing is complete. You can read more about main windows here.