代码之家  ›  专栏  ›  技术社区  ›  Neil

Silverlight 3测试版,ViewModel中的导航服务

  •  3
  • Neil  · 技术社区  · 16 年前

    我正在开发一个silverlight 3-beta导航应用程序,所以我使用了MVVM模式的一个细微变化:)(一体化视图模型),使用棱镜等。

    <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));
    

    this 这篇文章描述了使用helix 0.3进行导航,这是在sl2天内构建的,当时导航控件从未存在过,现在helix的模型工作良好,通过在视图模型中实现INavigationAware,您可以访问NavigationContext,然后做任何您需要的事情,我尝试过helix,它工作正常。

    SL3附带了内置的导航支持,可以这么说,这正是helix所做的。所以我不想使用第三方框架,相反,我更喜欢使用内置的sl3功能。

    SL3中是否有任何东西可以模拟螺旋的INavigationAware界面?

    5 回复  |  直到 16 年前
        1
  •  4
  •   Nikhil Kothari    16 年前

    我个人认为NavigationService是一个与UI框架或页面相关的UI概念。

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

        2
  •  1
  •   Neil    16 年前

        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;
            }
    
        3
  •  1
  •   Bryce    15 年前

        4
  •  0
  •   Neil    16 年前

    这是视图模型中的代码

    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;
                    }
                    );
    
        }
    

    导航服务为空,因此我无法移动到下一个视图,帮助!!!

        5
  •  0
  •   Charles Menguy jdw6415    12 年前
    NavigationService.Navigate(new Uri("/About", UriKind.Relative));