2011-05-16 129 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