如何从silverlight中的viewmodel从一个视图导航到另一个视图? [英] How to navigate from one view to another view from viewmodel in silverlight?
问题描述
我有一个视图模型和两个视图.如何从 ViewModel 导航到 View2.我在某处读到我们需要使用 PRISM,以便在 Silverlight 中从 ViewModel 打开多个视图.PRISM 有什么替代品吗?
I have one ViewModel and two Views. How can I navigate to View2 from ViewModel. I read somewhere that we need to use PRISM, for opening multiple Views from ViewModel in Silverlight. Is there any alternative for PRISM?
推荐答案
理想情况下,您不希望在视图模型中使用视图逻辑.您的视图模型不应该了解有关视图的任何信息.您的视图模型最好设置一个属性,让视图知道是时候导航了.举个例子:
Ideally you do not want to use view logic in your viewmodel. Your viewmodel should not know anything about the view. It would be a better idea for your viewmodel to set a property letting the view know it's time to navigate. Here's an example:
视图模型:
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
属性设置为视图模型中的DoneDoingStuff
属性,以及Value代码> 属性设置为True".
DataTrigger
将在视图模型中的DoneDoingStuff
设置为 true 时触发.Use a
DataTrigger
withBinding
property set to theDoneDoingStuff
property from your viewmodel, and theValue
property set to "True". TheDataTrigger
will trigger whenDoneDoingStuff
from your viewmodel is set to true.现在您需要一个触发操作来导航.使用
HyperlinkAction
并将NavigateUri
属性设置为您要导航到的页面.Now you need a trigger action to navigate. Use
HyperlinkAction
with theNavigateUri
property set to the page you're navigating to.确保有 System.Windows.Interactivity、System.Windows.Controls.Navigation 和 Microsoft.Expression.Interactions> 参考文献中的程序集.
Be sure to have System.Windows.Interactivity, System.Windows.Controls.Navigation, and Microsoft.Expression.Interactions assemblies in your references.
起初,这可能看起来太多了,但您的视图逻辑现在是需要的地方.
At first, this might seem to be too much, but your view logic is now where it needs to be.
这篇关于如何从silverlight中的viewmodel从一个视图导航到另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!