NavigationService抛出NullReferenceException [英] NavigationService throws NullReferenceException

查看:229
本文介绍了NavigationService抛出NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVVM Light,我正在尝试开发一个相当简单的WP7应用程序.我在使用导航服务时遇到了问题.我可以导航到页面,但是按返回按钮后,我将无法再次导航到同一页面. NavigationService抛出NullReferenceException.

Using MVVM Light, I'm trying to develop a rather simple WP7 application. I've run into a problem using the navigation service. I can navigate to a page, but after pressing the back button I can't navigate to the same page again. NavigationService throws a NullReferenceException.

我已经使用GalaSoft.MvvmLight.Messaging命名空间中的消息传递来实现导航.我的所有视图均来自自定义的PhoneApplicationPage基类,该基类在"NavigationRequest"上注册了侦听器:

I have implemented my navigation using Messaging from the GalaSoft.MvvmLight.Messaging namespace. All my views inherits from a customized PhoneApplicationPage base class that registrers a listener on "NavigationRequest":

public class PhoneApplicationPage : Microsoft.Phone.Controls.PhoneApplicationPage
{
    public PhoneApplicationPage() : base()
    {
        Messenger.Default.Register<Uri>(this, "NavigationRequest", (uri) => NavigationService.Navigate(uri));
    }
}

从我的视图模型中,我将Uri发布到该侦听器:

From my view models I post Uri's to this listener:

SendNavigationRequestMessage(new Uri("/View/AppSettingsView.xaml", UriKind.Relative));

就像我说的那样,除了按后退"按钮进行导航外,此方法均有效. 为什么会这样,我该如何解决?

Like i said, this works except when navigating after pressing the Back button. Why is this and how can I solve it?

是否有更好的方法使用MVVM Light来实现导航?

Is there a better way to implement navigation using MVVM Light?

推荐答案

我也在使用MVVM Light.我有一个称为PageConductor的类,该类基于Microsoft的John Papa(Silverlight MVP)的使用.这是我使用的PageConductor服务

I'm using MVVM Light as well. I have a class called PageConductor, which is based on what John Papa (Silverlight MVP) from Microsoft uses. Here's the PageConductor Service I use

public class PageConductor : IPageConductor
{
    protected Frame RootFrame { get; set; }

    public PageConductor()
    {
        Messenger.Default.Register<Messages.FrameMessage>(this, OnReceiveFrameMessage);
    }
    public void DisplayError(string origin, Exception e, string details)
    {
        string description = string.Format("Error occured in {0}. {1} {2}", origin, details, e.Message);
        var error = new Model.Error() { Description = description, Title = "Error Occurred" };
        Messenger.Default.Send(new Messages.ErrorMessage() { Error = error });
    }

    public void DisplayError(string origin, Exception e)
    {
        DisplayError(origin, e, string.Empty);
    }
    private void OnReceiveFrameMessage(Messages.FrameMessage msg)
    {
        RootFrame = msg.RootFrame;
    }
    private void Go(string path, string sender)
    {
        RootFrame.Navigate(new Uri(path, UriKind.Relative));
    }
    public void GoBack()
    {
        RootFrame.GoBack();
    }
}

在MainPage.xaml.cs构造函数中,它具有此函数,可在PageConductor服务中创建ContentFrame的实例.

In my MainPage.xaml.cs constructor, I have this, which creates an instance of my ContentFrame in my PageConductor service.:

Messenger.Default.Send(new Messages.FrameMessage() { RootFrame = ContentFrame });

然后,我使用依赖项注入将PageConductor服务的实例实例化到MainPage ViewModel中.这是我的MainViewModel类:

I then use dependency injection to instantiate an instance of my PageConductor Service into my MainPage ViewModel. Here is my MainViewModel class:

protected Services.IPageConductor PageConductor { get; set; }
    public RelayCommand<string> NavigateCommand { get; set; }

    public MainViewModel(Services.IPageConductor pageConductor)
    {

        PageConductor = pageConductor;
        RegisterCommands();
    }
    private void RegisterCommands()
    {
        NavigateCommand = new RelayCommand<string>(
            (source) => OnNavigate(source));

    }
    private void OnNavigate(string sender)
    {
        PageConductor.GoToView(sender, "main");
    }

在MainViewModel构造函数方法中,将我的PageConductorService实例作为参数通知.我通过ViewModelLocator传递了这个信息:

Notice the instance of my PageConductorService as a parameter in my MainViewModel constructor method. I pass this in via my ViewModelLocator:

private readonly TSMVVM.Services.ServiceProviderBase _sp;
public ViewModelLocator()
{
    _sp = Services.ServiceProviderBase.Instance;
CreateMain(_sp);
}
        #region MainPageViewModel
    public static MainViewModel MainStatic
    {
        get
        {
            Services.ServiceProviderBase SP = Services.ServiceProviderBase.Instance;
            if (_main == null)
            {
                CreateMain(SP);
            }

            return _main;
        }
    }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
         "CA1822:MarkMembersAsStatic",
         Justification = "This non-static member is needed for data binding purposes.")]
    public MainViewModel Main
    {
        get
        {
            return MainStatic;
        }
    }

    public static void ClearMain()
    {
        _main.Cleanup();
        _main = null;
    }

    public static void CreateMain(Services.ServiceProviderBase SP)
    {
        if (_main == null)
        {
            _main = new MainViewModel(SP.PageConductor);
        }
    }
    #endregion

作为进一步的参考,我的Messages.FrameMessage类很简单:

For further reference, my Messages.FrameMessage class is simply:

internal class FrameMessage
{
    public Frame RootFrame { get; set; }
}

前进/后退按钮没有问题.

I've had no issues with forward/back buttons.

这篇关于NavigationService抛出NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆