3

我正在开发一个 silverlight 3 beta 导航应用程序,所以我对 MVVM 模式进行了一些细微的改动:)(多合一视图模型),使用棱镜和其他东西。

问题:如何在视图模型中导航到不同的“NavigationPage”

现在长话短说,视图模型被声明为页面资源。

<navigation:Page.Resources>
    <mvvm:LoginModel x:Key="DataSource" d:IsDataSource="True"></mvvm:LoginModel>
</navigation:Page.Resources>

然后使用命令将所有内容与视图模型连接起来

<Button x:Name="LoginButton" Width="100"  Margin="8" Content="Login"
        prism:Click.Command="{Binding LoginCommand}"/>

现在,如果我尝试像这样在视图模型中的任何位置导航

this.NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.Relative));

Navigationservice 是空的,我环顾四周发现了这篇文章,它描述了使用 helix 0.3 进行导航,这是在 sl2 天构建的,当时导航控件不存在,现在 helix 的模型运行良好,并通过在中实现 INavigationAware视图模型,您可以访问 NavigationContext,然后执行您需要的任何操作,我尝试过 helix,它可以工作。

SL3 带有内置的导航支持,可以这么说,这正是 helix 所做的。所以我不想使用 3rd 方框架,而是更喜欢使用内置的 sl3 功能。

SL3 中是否有任何东西可以模拟 helix 的 INavigationAware 接口?

4

5 回答 5

4

我个人认为 NavigationService 是一个与 UI Frame 或 Page 相关联的 UI 概念。

另一种无需将 NavigationService 传递到视图模型的方法是让 ViewModel 在应该发生导航时引发事件……让视图处理视图模型事件并调用 Navigate 作为响应。

于 2009-09-13T10:30:26.717 回答
1

一个狡猾的修复,但我唯一能用来让它工作的东西。在视图的 OnNavigatedTo 事件中,访问 ViewModel 并将 NavigationService 设置为视图模型中的一个属性,以便稍后在视图模型中使用它

    protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ViewModels.LoginViewModel viewmodel = (ViewModels.LoginViewModel)this.Resources["DataSource"];
//DataSource being the x:Name given to the viewmodel that is loaded as a page resource
            viewmodel .service = NavigationService;
        }
于 2009-06-30T07:55:48.633 回答
1

如果您使用 MVVM light,您可能需要考虑使用消息系统。在您的页面上有一个侦听器,该侦听器托管执行导航并从您的视图模型发送导航消息的框架。

于 2010-10-13T19:49:53.680 回答
0

可以帮助我的问题,因为仍然没有任何答案,我将提供更多信息。

这是视图模型中的代码

public LoginModel()
    {
        LoginCommand = new DelegateCommand<object>(LoginCommandExecuted, a => { return _CanLoginCommandExecute; });
    }

    public ICommand LoginCommand { get; private set; }
    private bool _CanLoginCommandExecute = true;
    private void LoginCommandExecuted(object parameter)

    {
        _CanLoginCommandExecute = false;

        AdminClient client = new AdminClient();
        client.AuthorizeAsync();
        client.AuthorizeCompleted += 
        new EventHandler<AsyncCompletedEventArgs>(
                (s, e) =>
                {
                    if (e.Error != null)
                    {
                        MessageBox.Show("Login Failed");
                    }
                    else
                    {
                        this.NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.Relative));
                    }
                    _CanLoginCommandExecute = true;
                }
                );

    }

NavigationService 为空,因此我无法移动到下一个视图,求助!!!

于 2009-06-29T10:23:49.833 回答
0
NavigationService.Navigate(new Uri("/About", UriKind.Relative));

以上应该工作。

于 2009-12-08T09:55:55.037 回答