mvvmlight - 什么是“正确的方法”?为视图模型获取 url 参数

发布于 2024-11-29 23:53:08 字数 622 浏览 0 评论 0原文

我只是将一个项目切换到 mvvmlight 并尝试以“正确的方式”做事

我有一个带有列表框的简单应用程序

当在列表框中选择一个项目时,我就连接了一个 RelayCommand

这个 RelayCommand 导致对 INavigationService 的调用(http://geekswithblogs.net/lbugnion/archive/2011/01/06/navigation-in-a-wp7-application-with-mvvm-light.aspx)导航到像“/DetailPage.xaml?DetailId”这样的url =12"

然后加载 DetailPage.xaml ......这是我有点不确定的地方......

  • DetailPage 应该如何连接到 DetailView DetailId 为 12?
  • 我应该在 Xaml 中以某种方式使用 ViewLocator 上的属性来执行此操作吗?
  • 我应该在 NavigatedTo 方法中执行此操作吗?

请随意向我指出一个完整的示例 - 当然这之前已经做过(十)万次了,但是所有博客和教程似乎都跳过了这最后一个琐碎的细节(而是专注于消息传递和国际奥委会)导航服务)

谢谢!

I'm just switching a project across to mvvmlight and trying to do things "the right way"

I've got a simple app with a listbox

When an item is selected in the listbox, then I've hooked up a RelayCommand

This RelayCommand causes a call on an INavigationService (http://geekswithblogs.net/lbugnion/archive/2011/01/06/navigation-in-a-wp7-application-with-mvvm-light.aspx) which navigates to a url like "/DetailPage.xaml?DetailId=12"

The DetailPage.xaml is then loaded and ... this is where I'm a bit unsure...

  • how should the DetailPage get hooked up to a DetailView with DetailId of 12?
  • should I do this in Xaml somehow using a property on the ViewLocator?
  • should I do this in the NavigatedTo method?

Please feel free to point me to a full sample - sure this has been done a (hundred) thousand times before, but all the blogs and tutorials seem to be skipping this last trivial detail (focussing instead on the messaging and on the ioc on on the navigationservice)

Thanks!

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

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

发布评论

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

评论(2

空袭的梦i 2024-12-06 23:53:08

唯一可以检索 URL 参数的位置是视图中。因此,由于您的视图可能依赖于它,因此您应该在 OnNavieratedTo 方法中获取它。

然后,您应该将其传递给您的视图模型,或者使用消息传递(如果您问我的话,这会很昂贵),或者通过引用您的数据上下文(我认为这是视图模型),并执行一个方法。

private AddTilePageViewModel ViewModel
{
    get
    {
        return DataContext as AddTilePageViewModel;
    }
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var postalCode = NavigationContext.TryGetKey("PostalCode");
    var country = NavigationContext.TryGetStringKey("Country");

    if (postalCode.HasValue && string.IsNullOrEmpty(country) == false)
    {
        ViewModel.LoadCity(postalCode.Value, country);
    }

    base.OnNavigatedTo(e);
}

我对 NavigationContext 使用了一些特殊的扩展,以使其更容易。

namespace System.Windows.Navigation
{
    public static class NavigationExtensions
    {
        public static int? TryGetKey(this NavigationContext source, string key)
        {
            if (source.QueryString.ContainsKey(key))
            {
                string value = source.QueryString[key];

                int result = 0;
                if (int.TryParse(value, out result))
                {
                    return result;
                }
            }

            return null;
        }

        public static string TryGetStringKey(this NavigationContext source, string key)
        {
            if (source.QueryString.ContainsKey(key))
            {
                return source.QueryString[key];
            }

            return null;
        }
    }
}

The only place you can retrieve the URL parameter is in the view. So since your view is likely depending on it, you should fetch it in the OnNavigatedTo method.

Then, you should pass it along to your viewmodel, either using messaging (to expensive if you ask me), or by referring to your datacontext (which is the viewmodel I presume), and execeuting a method on that.

private AddTilePageViewModel ViewModel
{
    get
    {
        return DataContext as AddTilePageViewModel;
    }
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var postalCode = NavigationContext.TryGetKey("PostalCode");
    var country = NavigationContext.TryGetStringKey("Country");

    if (postalCode.HasValue && string.IsNullOrEmpty(country) == false)
    {
        ViewModel.LoadCity(postalCode.Value, country);
    }

    base.OnNavigatedTo(e);
}

I'm using some special extensions for the NavigationContext to make it easier.

namespace System.Windows.Navigation
{
    public static class NavigationExtensions
    {
        public static int? TryGetKey(this NavigationContext source, string key)
        {
            if (source.QueryString.ContainsKey(key))
            {
                string value = source.QueryString[key];

                int result = 0;
                if (int.TryParse(value, out result))
                {
                    return result;
                }
            }

            return null;
        }

        public static string TryGetStringKey(this NavigationContext source, string key)
        {
            if (source.QueryString.ContainsKey(key))
            {
                return source.QueryString[key];
            }

            return null;
        }
    }
}
绿光 2024-12-06 23:53:08

创建一个新的 WindowsPhoneDataBound 应用程序,它有一个如何处理视图之间导航的示例。基本上,您处理视图中的导航部分,然后根据查询字符串设置视图的 DataContext。我认为它与 MVVM 模式配合得很好,因为您的 ViewModel 不必了解有关导航的任何信息(IMO 应该在 UI 级别处理)。

Create a new WindowsPhoneDataBound application, it has an example of how to handle navigation between views. Basically you handle the navigation part in your view, then set the view's DataContext accord to the query string. I think it plays nicely with the MVVM pattern since your ViewModels don't have to know anything about navigation (which IMO should be handled at the UI level).

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