Silverlight 3 Beta,ViewModel 中的 NavigationService

发布于 2024-07-26 03:04:35 字数 1102 浏览 1 评论 0原文

我正在开发一个 silverlight 3 beta 导航应用程序,因此我对 MVVM 模式进行了轻微的修改:)(一体化视图模型),使用了 prism 等。

问题:如何导航到视图模型中的不同“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));

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

SL3 带有内置的导航支持,可以说,它的功能与 helix 的功能完全相同。 所以我不想使用第三方框架,而是更喜欢使用内置的 sl3 功能。

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

im developing a silverlight 3 beta navigation application, so i've gone with a slight variation of the MVVM pattern :) (all-in-one viewmodel), using prism, and stuff.

Question: How do i navigate to a different "NavigationPage" in the viewmodel

Now to cut a long story short, the viewmodel is declared as a page resource.

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

And then a command is used to wireup everything with the viewmodel

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

Now if i try to navigate anywhere in the viewmodel like so

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

the Navigationservice is null, i've looked around and found this article, which describes using helix 0.3 for navigation, this was built back in the sl2 days, when the navigation controls never existed, now helix's model works well, and by implementing INavigationAware in the viewmodel, you are able to gain access to the NavigationContext, and then do whatever it is you require, i have tried helix, and it works.

SL3 comes with the builtin Navigation support, so to speak, which does exactly what helix does. So i dont wanna use a 3rd party framework, instead i prefer to use the built in sl3 features.

Is there anything in SL3 that emulates the helix's INavigationAware interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

混吃等死 2024-08-02 03:04:35

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

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

I personally think NavigationService is a UI-concept associated with the UI Frame or Page.

Another way to accomplish this without having to pass in a NavigationService into the view model is to have the ViewModel raise an event when navigation is supposed to occur... have the view handle the view model event and call Navigate in response.

溺孤伤于心 2024-08-02 03:04:35

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

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

A dodgy fix, but the only thing i've been able to use to get this working.
In the OnNavigatedTo event in the view, access the ViewModel and set the NavigationService to a property in the viewmodel so that it can be used later in the viewmodel

    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;
        }
时光礼记 2024-08-02 03:04:35

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

You may want to consider using the Messaging system if you are using MVVM light. Have a listener on your page hosting the frame that does the navigation and send the nav messages from your view models.

﹉夏雨初晴づ 2024-08-02 03:04:35

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

这是视图模型中的代码

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 为空,因此我无法移动到下一个视图,救命!!!

okay to help my question along, cos there still hasn't been any answer, i'm gonna throw more information at it.

This is the code in the viewmodel

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 is null, therefore i cant move to the next view, help!!!

痴梦一场 2024-08-02 03:04:35
NavigationService.Navigate(new Uri("/About", UriKind.Relative));

以上应该有效。

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

Above should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文