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

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

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>

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

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

在我的视图模型中,我从中调用新窗口的代码如下:

  1. public class MainViewModel : MvxViewModel
  2. {
  3. private readonly IMvxNavigationService navigationService;
  4. public IMvxCommand SettingsCommand { get; set; }
  5. public MainViewModel(IMvxNavigationService _navigationService)
  6. {
  7. navigationService = _navigationService;
  8. GenerateMenu();
  9. ExitCommand = new MvxCommand(Exit);
  10. SettingsCommand = new MvxAsyncCommand(async () => await navigationService.Navigate<BolusViewModel>());
  11. }
  12. }
  1. public class MainViewModel : MvxViewModel
  2. {
  3. private readonly IMvxNavigationService navigationService;
  4. public IMvxCommand SettingsCommand { get; set; }
  5. public MainViewModel(IMvxNavigationService _navigationService)
  6. {
  7. navigationService = _navigationService;
  8. GenerateMenu();
  9. ExitCommand = new MvxCommand(Exit);
  10. SettingsCommand = new MvxAsyncCommand(async () => await navigationService.Navigate<BolusViewModel>());
  11. }
  12. }

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

我要尝试作为新窗口打开的视图模型如下:

  1. public class BolusViewModel : MvxViewModel
  2. {
  3. private readonly IMvxNavigationService navigationService;
  4. public BolusViewModel(IMvxNavigationService _navigationService)
  5. {
  6. navigationService = _navigationService;
  7. }
  8. }
  1. public class BolusViewModel : MvxViewModel
  2. {
  3. private readonly IMvxNavigationService navigationService;
  4. public BolusViewModel(IMvxNavigationService _navigationService)
  5. {
  6. navigationService = _navigationService;
  7. }
  8. }

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

最后,试图打开的窗口如下 - 代码后台:

  1. [MvxContentPresentation]
  2. [MvxWindowPresentation(Identifier = nameof(BolusViewModel), Modal = false)]
  3. public partial class BolusView : MvxWindow
  4. {
  5. public BolusView()
  6. {
  7. InitializeComponent();
  8. }
  9. }
  1. [MvxContentPresentation]
  2. [MvxWindowPresentation(Identifier = nameof(BolusViewModel), Modal = false)]
  3. public partial class BolusView : MvxWindow
  4. {
  5. public BolusView()
  6. {
  7. InitializeComponent();
  8. }
  9. }

Xaml:

XAML:

  1. <views:MvxWindow
  2. xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
  3. xmlns:mvx="clr-namespace:MvvmCross.Platforms.Wpf.Binding;assembly=MvvmCross.Platforms.Wpf"
  4. x:Class="TechnicalToolboxV3.Wpf.BolusView"
  5. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  6. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  9. xmlns:Behaviors="http://schemas.microsoft.com/xaml/behaviors"
  10. xmlns:local="clr-namespace:TechnicalToolboxV3.Wpf"
  11. xmlns:converters="clr-namespace:TechnicalToolboxV3.Wpf.Converters"
  12. mc:Ignorable="d"
  13. d:DesignHeight="700" d:DesignWidth="1200" FontSize="14" Title="Bolus Controller" Icon="../Images/Bolus.jpg" Background="#FFBEBEBE" Cursor="{Binding MouseIcon, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
  14. <views:MvxWindow.Resources>
  15. <converters:SystemColorToSolidBrushConverter xKey="ColorConvert"/>
  16. </views:MvxWindow.Resources>
  17. </views:MvxWindow>
  1. <views:MvxWindow
  2. xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
  3. xmlns:mvx="clr-namespace:MvvmCross.Platforms.Wpf.Binding;assembly=MvvmCross.Platforms.Wpf"
  4. x:Class="TechnicalToolboxV3.Wpf.BolusView"
  5. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  6. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  7. xmlns:mc="http://schemas.openxmlformats.org/mark-up-compatibility/2006"
  8. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  9. xmlns:Behaviors="http://schemas.microsoft.com/xaml/behaviors"
  10. xmlns:local="clr-namespace:TechnicalToolboxV3.Wpf"
  11. xmlns:converters="clr-namespace:TechnicalToolboxV3.Wpf.Converters"
  12. mc:Ignorable="d"
  13. d:DesignHeight="700" d:DesignWidth="1200" FontSize="14" Title="Bolus Controller" Icon="../Images/Bolus.jpg" Background="#FFBEBEBE" Cursor="{Binding MouseIcon, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
  14. <views:MvxWindow.Resources>
  15. <converters:SystemColorToSolidBrushConverter xKey="ColorConvert"/>
  16. </views:MvxWindow.Resources>
  17. </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;

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

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

  1. public class MainViewModel : MvxViewModel
  2. {
  3. private readonly IMvxNavigationService navigationService;
  4. public IMvxCommand SettingsCommand { get; set; }
  5. public MainViewModel( IMvxNavigationService _navigationService)
  6. {
  7. navigationService = _navigationService;
  8. GenerateMenu();
  9. ExitCommand = new MvxCommand(Exit);
  10. **SettingsCommand = new MvxAsyncCommand(async () =&gt; await navigationService.Navigate&lt;BolusViewModel&gt;()); **
  11. }
  12. }

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

  1. public class BolusViewModel : MvxViewModel
  2. {
  3. private readonly IMvxNavigationService navigationService;
  4. public BolusViewModel(IMvxNavigationService _navigationService)
  5. {
  6. navigationService = _navigationService;
  7. }
  8. }

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

  1. [MvxContentPresentation]
  2. [MvxWindowPresentation(Identifier = nameof(BolusViewModel), Modal = false)]
  3. public partial class BolusView : MvxWindow
  4. {
  5. public BolusView()
  6. {
  7. InitializeComponent();
  8. }
  9. }

Xaml:

  1. &lt;views:MvxWindow
  2. xmlns:views=&quot;clr- namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf&quot;
  3. xmlns:mvx=&quot;clr- namespace:MvvmCross.Platforms.Wpf.Binding;assembly=MvvmCross.Platforms.Wpf&quot;
  4. x:Class=&quot;TechnicalToolboxV3.Wpf.BolusView&quot;
  5. xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  6. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
  7. xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  8. xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
  9. xmlns:Behaviors=&quot;http://schemas.microsoft.com/xaml/behaviors&quot;
  10. xmlns:local=&quot;clr-namespace:TechnicalToolboxV3.Wpf&quot;
  11. xmlns:converters=&quot;clr-namespace:TechnicalToolboxV3.Wpf.Converters&quot;
  12. mc:Ignorable=&quot;d&quot;
  13. 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;
  14. &lt;views:MvxWindow.Resources&gt;
  15. &lt;converters:SystemColorToSolidBrushConverter x:Key=&quot;ColorConvert&quot;/&gt;
  16. &lt;/views:MvxWindow.Resources&gt;
  17. &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.

  1. [MvxContentPresentation]
  2. [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:

确定