我刚刚开始使用Silverlight以及MVVM模型。 在执行页面导航并将参数从一个页面发送到另一个页面时,..是使用querystring最常用的方法吗?
在执行页面导航时如何传递参数似乎是一个很大的混乱。至少我在各种网络资源上找到了关于这方面的几个主题,但似乎没有人同意“最佳实践”方法。
答案 0 :(得分: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;
}
}
}
}
然后将以下内容添加到ViewModel:
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;
}
}