gpt4 book ai didi

silverlight - 如何在Silverlight中从ViewModel从一个 View 导航到另一个 View ?

转载 作者:行者123 更新时间:2023-12-03 10:13:36 24 4
gpt4 key购买 nike

我有一个ViewModel和两个 View 。如何从ViewModel导航到View2。我读到某个地方需要使用PRISM,以便在Silverlight中从ViewModel打开多个 View 。 PRISM是否有其他选择?

最佳答案

理想情况下,您不想在 View 模型中使用 View 逻辑。您的 View 模型应该对 View 一无所知。对于您的 View 模型来说,最好设置一个属性,让该 View 知道该导航的时间了。
这是一个例子:

ViewModel :

using System.ComponentModel;

namespace ViewModels
{
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged == null) return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

private bool _DoneDoingStuff;
public bool DoneDoingStuff
{
get
{
return _DoneDoingStuff;
}
set
{
if (_DoneDoingStuff != value)
{
_DoneDoingStuff = value;
NotifyPropertyChanged("DoneDoingStuff");
}
}
}
}
}

查看:
<navigation:Page
x:Class="Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:vm="clr-namespace:ViewModels">
<navigation:Page.Resources>
<vm:MyViewModel
x:Key="MyViewModelInstance" />
</navigation:Page.Resources>
<Grid
x:Name="LayoutRoot"
DataContext="{Binding Source={StaticResource MyViewModelInstance}}">
<i:Interaction.Triggers>
<ei:DataTrigger
Binding="{Binding DoneDoingStuff}"
Value="True">
<ei:HyperlinkAction
NavigateUri="AnotherPage.xaml" />
</ei:DataTrigger>
</i:Interaction.Triggers>
</Grid>
</navigation:Page>
  • 使用DataTrigger,将Binding属性设置为 View 模型中的DoneDoingStuff属性,并将Value属性设置为“True”。当您的 View 模型中的DataTrigger设置为true时,就会触发DoneDoingStuff
  • 现在,您需要触发操作来进行导航。使用HyperlinkAction,并将NavigateUri属性设置为要导航到的页面。
  • 请确保引用中具有 System.Windows.Interactivity System.Windows.Controls.Navigation Microsoft.Expression.Interactions 程序集。

  • 最初,这似乎太多了,但是现在您的 View 逻辑已经到了需要的地方。

    关于silverlight - 如何在Silverlight中从ViewModel从一个 View 导航到另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9283441/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com