MvvmCross导航以打开新窗口并保持原始窗口打开。

huangapple go评论58阅读模式
英文:

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:

希望打开一个新窗口而不是在另一个窗口内部显示视图。我有一个带有菜单的通知图标,我希望从菜单中从我的 MainViewModel 打开一个新窗口。这可以做到吗?出现以下错误:

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>

我怀疑的行是在我的 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>
<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>());
    }
}
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;
    }
}
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();
    }
}
[MvxContentPresentation]
[MvxWindowPresentation(Identifier = nameof(BolusViewModel), Modal = false)]
public partial class BolusView : MvxWindow
{
    public BolusView()
    {
        InitializeComponent();
    }
}

Xaml:

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 xKey="ColorConvert"/>
    </views:MvxWindow.Resources>
</views:MvxWindow>
<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/mark-up-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 xKey="ColorConvert"/>
    </views:MvxWindow.Resources>
</views:MvxWindow>
英文:

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 &lt;views:MvxWpfView&gt;

&lt;MenuItem Header=&quot;Settings&quot;  mvx:Bi.nd=&quot;Command SettingsCommand&quot;&gt;
       &lt;MenuItem.Icon&gt;
           &lt;Image Width=&quot;16&quot; Height=&quot;16&quot; Source=&quot;/Images/settings.png&quot;/&gt;
        &lt;/MenuItem.Icon&gt;
&lt;/MenuItem&gt;

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 () =&gt; await  navigationService.Navigate&lt;BolusViewModel&gt;()); ** 
    }
}

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:

&lt;views:MvxWindow
                  xmlns:views=&quot;clr-          namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf&quot;
                   xmlns:mvx=&quot;clr-      namespace:MvvmCross.Platforms.Wpf.Binding;assembly=MvvmCross.Platforms.Wpf&quot;
             x:Class=&quot;TechnicalToolboxV3.Wpf.BolusView&quot;
             xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
             xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
             xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; 
             xmlns:Behaviors=&quot;http://schemas.microsoft.com/xaml/behaviors&quot;
             xmlns:local=&quot;clr-namespace:TechnicalToolboxV3.Wpf&quot;
             xmlns:converters=&quot;clr-namespace:TechnicalToolboxV3.Wpf.Converters&quot;
             mc:Ignorable=&quot;d&quot; 
             d:DesignHeight=&quot;700&quot; d:DesignWidth=&quot;1200&quot; FontSize=&quot;14&quot; Title=&quot;Bolus Controller&quot; Icon=&quot;..\Images\Bolus.jpg&quot; Background=&quot;#FFBEBEBE&quot; Cursor=&quot;{Binding MouseIcon, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}&quot;&gt;
    &lt;views:MvxWindow.Resources&gt;
        &lt;converters:SystemColorToSolidBrushConverter x:Key=&quot;ColorConvert&quot;/&gt;
    &lt;/views:MvxWindow.Resources&gt;
&lt;/views:MvxWindow&gt;

答案1

得分: 0

这似乎在起作用...我从我想要打开的窗口的代码中删除了这些行。

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

XAMIMAX,谢谢你提出这一点,它给了我一个线索,让我知道应该寻找什么,谢谢。

英文:

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

huangapple
  • 本文由 发表于 2023年3月3日 23:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定