英文:
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 <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>
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论