silverlight - MVVM Silverlight 和页面导航

标签 silverlight mvvm navigation

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

在执行页面导航时应该如何传递参数似乎是一个很大的困惑。至少我在各种网络资源上找到了几个关于此的主题,但似乎没有人就“最佳实践”方法达成一致。

最佳答案

注意:以下在 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 模型中,您可以像这样向 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;
        }
    }

关于silverlight - MVVM Silverlight 和页面导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020081/

相关文章:

wpf - 为什么应该为 WPF、SL 和 WP7 使用 UI 模式框架?

c# - 为什么wpf控件只有一个命令? (MVVM)

android - 导航回 Android 上的页面时,Xamarin.Forms 控件消失

html - 固定导航栏在滚动时被图像 slider 重叠

Silverlight clientaccesspolicy.xml 和 Azure 开发存储

silverlight - 如何在Windows Phone(7.5)上制作网络游戏?

.net - 是否有在twisted中实现silverlight的策略文件服务器?

c# - 如何在我的所有属性(property)中使用 “distinct”

c# - 来自 subview 的 MainWindow ViewModel 引用

html - 如何停止 CSS/HTML 导航菜单以不同的分辨率换行?