Search code examples
c#wpfmvvmcross

MvvmCross Navigation to open a New Windows and let the original be open


I wish to open a new window and not a view inside another window. I have a notify icon with a menu from the menu I want to open a new windows from my MainViewModel. Can that be done? It comes up with this error:

System.InvalidOperationException
HResult=0x80131509
Message=Window must be the root of the tree. Cannot add Window as a child of Visual.
Source=PresentationFramework

The lines I doubt is this in my MainView Xaml <views:MvxWpfView>

<MenuItem Header="Settings"  mvx:Bi.nd="Command SettingsCommand">
       <MenuItem.Icon>
           <Image Width="16" Height="16" Source="/Images/settings.png"/>
        </MenuItem.Icon>
</MenuItem>

In my view model where I am calling the new window from is this code:

public class MainViewModel : MvxViewModel
{
    private readonly IMvxNavigationService navigationService;
    public IMvxCommand SettingsCommand { get; set; }

    public MainViewModel( IMvxNavigationService _navigationService) 
    {
        navigationService = _navigationService;
        GenerateMenu();
        ExitCommand = new MvxCommand(Exit);

        **SettingsCommand = new MvxAsyncCommand(async () => await  navigationService.Navigate<BolusViewModel>()); ** 
    }
}

The view model I am try to open as a new window looks like this:

public class BolusViewModel : MvxViewModel
{
    private readonly IMvxNavigationService navigationService;

    public BolusViewModel(IMvxNavigationService _navigationService)
    { 
        navigationService = _navigationService;
    }
}

And last the window that is trying to open looks like this - code behind:

[MvxContentPresentation]
[MvxWindowPresentation(Identifier =  nameof(BolusViewModel), Modal = false)]
public partial class BolusView : MvxWindow
{
    public BolusView()
    {
        InitializeComponent();
    }
}

Xaml:

<views:MvxWindow
                  xmlns:views="clr-          namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
                   xmlns:mvx="clr-      namespace:MvvmCross.Platforms.Wpf.Binding;assembly=MvvmCross.Platforms.Wpf"
             x:Class="TechnicalToolboxV3.Wpf.BolusView"
             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" 
             xmlns:Behaviors="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:local="clr-namespace:TechnicalToolboxV3.Wpf"
             xmlns:converters="clr-namespace:TechnicalToolboxV3.Wpf.Converters"
             mc:Ignorable="d" 
             d:DesignHeight="700" d:DesignWidth="1200" FontSize="14" Title="Bolus Controller" Icon="..\Images\Bolus.jpg" Background="#FFBEBEBE" Cursor="{Binding MouseIcon, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
    <views:MvxWindow.Resources>
        <converters:SystemColorToSolidBrushConverter x:Key="ColorConvert"/>
    </views:MvxWindow.Resources>
</views:MvxWindow>

Solution

  • This Seems to be working... I removed these lines from my Code behind on the window i wanted to open.

    [MvxContentPresentation]
              [MvxWindowPresentation(Identifier =  nameof(BolusViewModel), Modal = false)]
    

    XAMIMAX Thanks for make that point, it gave me a glue for what i should be looking for, Thanx