如何在silverlight中从viewmodel从一个视图导航到另一个视图?

如何在silverlight中从viewmodel从一个视图导航到另一个视图?,silverlight,mvvm,view,viewmodel,Silverlight,Mvvm,View,Viewmodel,我有一个视图模型和两个视图。如何从ViewModel导航到View2。我在某个地方读到,我们需要使用PRISM,以便在Silverlight中从ViewModel打开多个视图。PRISM有其他选择吗?您不需要使用PRISM,但它可能是最好的 我这样做的一个方法(而且很草率)是有一个主视图页面,其中有一个导航框架,在启动时加载第一个视图。MainView必须是页面,而不是用户控件。您需要在xaml中有一个带有uri映射的导航框架,并在MainView页面的代码隐藏中声明一个框架为shared/st

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

您不需要使用PRISM,但它可能是最好的

我这样做的一个方法(而且很草率)是有一个主视图页面,其中有一个导航框架,在启动时加载第一个视图。MainView必须是页面,而不是用户控件。您需要在xaml中有一个带有uri映射的导航框架,并在MainView页面的代码隐藏中声明一个框架为shared/static,然后设置框架的加载事件(在xaml中),如下所示:

Public Shared MainContentFrame As Frame
Private Sub MainContentFrameXaml_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
    MainContentFrame = TryCast(sender, Frame)
End Sub
然后在viewmodel中,您可以调用:

MainView.MainContentFrame.Navigate(New Uri("/SecondView", UriKind.Relative))

这可能在某种程度上违反了MVVM模式,可能不是一个好的方法,但它是有效的。我以前是这样做的,现在我用棱镜

理想情况下,您不希望在viewmodel中使用视图逻辑。您的viewmodel不应该知道有关视图的任何信息。您的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>
查看

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
    属性设置为viewmodel中的
    DoneDoingStuff
    属性,并将
    属性设置为“True”。当viewmodel中的
    DoneDoingStuff
    设置为true时,将触发
    DataTrigger

  • 现在你需要一个触发动作来导航。使用
    HyperlinkAction
    并将
    NavigateUri
    属性设置为要导航到的页面

  • 请确保在引用中包含System.Windows.InteractivitySystem.Windows.Controls.NavigationMicrosoft.Expression.interactivions程序集


起初,这似乎太多了,但您的视图逻辑现在正处于需要的位置。

您所说的“打开视图”是什么意思?你想在那里导航还是怎么的?是的!从一个视图导航到另一个视图。@user672094如果有帮助,请将我的答案标记为答案。。。我喜欢这个代表!