question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Open owner-less window using .Show() -- 'ViewModel is not present as a data context for any registered views'

See original GitHub issue

So 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:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
RFBombcommented, Aug 15, 2022

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

0reactions
FantasticFiascocommented, Jul 28, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF MVVM - Views does not inherit the Data context from ...
Child controls always inherit the parent DataContext . What you must do is provide dot-prefixed paths to your relevant viewmodel properties.
Read more >
Wiring up View and Viewmodel in MVVM and Silverlight 4
As you can see each instance of the View has different data shown that proves that each has different instance of ViewModel set...
Read more >
DataContext And Autowire In WPF
DataContext is the head of everything. It makes sure that your View is hooked up with ViewModel. There are 3 ways to hook-up...
Read more >
XAML – Bruno Sonnino
The ViewModel maintains the open windows count. When you click on the button to show the details, it will increase the window count....
Read more >
Tasks and the back stack
A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found