2011-05-16 106 views
3

我刚刚开始使用Silverlight和MVVM模型。 当执行页面导航并将参数从一个页面发送到另一个页面时,..是使用查询字符串最常用的方法吗?MVVM Silverlight和页面导航

这似乎是一个很大的混淆,如何执行页面导航时传递参数。至少我在各种网络资源上发现了几个关于这个问题的主题,但似乎没有人同意采用“最佳实践”方法。

回答

2

注意:以下解决方案在NavigationContext中使用查询字符串适用于浏览器内外。

你通常设置你的UriMapper是这样的:

  <navigation:Frame Source="/Home" > 
       <navigation:Frame.UriMapper> 
        <uriMapper:UriMapper> 
         <uriMapper:UriMapping Uri="" 
            MappedUri="/Views/Home.xaml"/> 
         <uriMapper:UriMapping Uri="/{pageName}/{key}" 
            MappedUri="/Views/{pageName}.xaml?entityGuid={key}"/> 
         <uriMapper:UriMapping Uri="/{pageName}" 
            MappedUri="/Views/{pageName}.xaml"/> 
        </uriMapper:UriMapper> 
       </navigation:Frame.UriMapper> 
      </navigation:Frame> 

然后拿到NavigationContext到视图模型,你添加一个助手到View像这样:

<navigation:Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:helpers="clr-namespace:MyApp.Helpers" 
       xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
       DataContext="{Binding Path=Entity, Source={StaticResource Locator}}" 
       helpers:Navigator.Source="{Binding}"> 

然后你有这样一个附加的财产帮助者(我从别人那里修改了这个,虽然我忘了是谁):

using System.Windows; 
using System.Windows.Controls; 

namespace MyApp.Helpers 
{ 

    public interface INavigable 
    { 
     System.Windows.Navigation.NavigationService NavigationService { get; set; } 
     System.Windows.Navigation.NavigationContext NavigationContext { get; set; } 
    } 


    public static class Navigator 
    { 
     public static INavigable GetSource(DependencyObject obj) 
     { 
      return (INavigable)obj.GetValue(SourceProperty); 
     } 

     public static void SetSource(DependencyObject obj, INavigable value) 
     { 
      obj.SetValue(SourceProperty, value); 
     } 

     public static readonly DependencyProperty SourceProperty = 
      DependencyProperty.RegisterAttached("Source", typeof(INavigable), typeof(Navigator), new PropertyMetadata(OnSourceChanged)); 

     private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      Page page = (Page)d; 

      page.Loaded += PageLoaded; 
     } 

     private static void PageLoaded(object sender, RoutedEventArgs e) 
     { 
      Page page = (Page)sender; 

      INavigable navSource = GetSource(page); 

      if (navSource != null) 
      { 
       navSource.NavigationService = page.NavigationService; 
       navSource.NavigationContext = page.NavigationContext; 
      } 
     } 
    } 
} 

然后添加以下到您的视图模型:

private NavigationContext _NavigationContext; 
    public NavigationContext NavigationContext { 
     get { return _NavigationContext; } 
     set { 
      if (_NavigationContext == value) 
       return; 
      _NavigationContext = value; 
      RaisePropertyChanged("NavigationContext"); 
     } 
    } 
    protected override void RaisePropertyChanged(string propertyName) { 
     base.RaisePropertyChanged(propertyName); 

     switch (propertyName) { 
      case "NavigationContext": 
       if (NavigationContext.QueryString.ContainsKey("entityGuid")) { 
        if (NavigationContext.QueryString["entityGuid"].Equals("new", StringComparison.InvariantCultureIgnoreCase)) { 
         LoadNewEntity(); // your 'new' logic 
        } else { 
         this.EntityGuid = new Guid(NavigationContext.QueryString["entityGuid"]); 
         LoadExistingEntity(EntityGuid); // your 'existing' logic 
        } 
       } 
       break; 
     } 
    } 
+0

我不太得到的XAML字符串:的DataContext =“{绑定路径=实体,源= {静态资源定位符}}”的定位是不在我的项目中找到 – Kman 2011-05-18 12:38:20

+0

对不起,这是许多人所做的MVVM灯光事物。看看这篇文章,它确实比在一个资源中设置ViewModel好多了: – JasonRShaver 2011-05-18 21:21:08

+0

http://johnpapa.net/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-ylylum – JasonRShaver 2011-05-18 21:21:24