NullReferenceException when used INavigation in WP7?

Topics: Getting Started
Jun 15, 2011 at 12:58 PM
Edited Jun 15, 2011 at 12:59 PM

Hello all,

i am trying to use the CM in my WP7 project, but i have a problem when using INavigationService to navigate to a new wp page.

I add a new Page "Page1.xaml" to the Caliburn.Micro.HelloWP7 project, create a ViewModel "Page1ViewModel" for it.

And i also regist in HelloWP7Bootstrapper

 

container.PerRequest<Page1ViewModel>();

 

here is my viewmodel

namespace Caliburn.Micro.HelloWP7
{
    public class Page1ViewModel
    {
         readonly INavigationService navigationService;

         public Page1ViewModel(INavigationService navigationService)
         {
            this.navigationService = navigationService;
        }
    }
}

 

after, i made GotoPageTwo navigate to the new page

 

 public void GotoPageTwo() {
            /*navigationService.UriFor<PivotPageViewModel>()
                .WithParam(x => x.NumberOfTabs, 5)
                .Navigate();*/
            //navigationService.Navigate(new System.Uri("/Page1.xaml", System.UriKind.RelativeOrAbsolute));
            navigationService.UriFor<Page1ViewModel>().Navigate();
        }	


i got a nullreferenceexption when click GotoPageTwo. 
When i changed to :
navigationService.Navigate(new System.Uri("/Page1.xaml", System.UriKind.RelativeOrAbsolute));

it came up with a invalidOperationException:Sequence contains more than one element.
I really dont know how to use strongly typed navigation, can anybody give me some sample or tell me what's the problem of my code?

thanks in advance!
Jun 15, 2011 at 2:19 PM
Edited Jun 15, 2011 at 2:20 PM

in your bootstrapper do you have a line:

container.RegisterPhoneServices();

in your config override?

I am assuming you are version 1.1

Jun 15, 2011 at 2:45 PM
Edited Jun 15, 2011 at 2:46 PM

 

protected override void Configure() {
            container = new PhoneContainer(RootFrame);

            container.RegisterPhoneServices();
            container.PerRequest<MainPageViewModel>();
            container.PerRequest<PivotPageViewModel>();
            container.PerRequest<Page1ViewModel>();
            container.PerRequest<TabViewModel, TabViewModel>();
            
            AddCustomConventions();
        }

correct,i am using 1.1 and i do have RegisterPhoneServices, here is my Configure override
Coordinator
Jun 15, 2011 at 6:09 PM

I need you to email me a sample. We may have some bugs in our new stuff. Please create a simple soltution that reproduces the problem and email it to robertheisenberg at hotmail dot com

Jun 16, 2011 at 2:33 AM

Hello Reober,i have sent you the sample, please check it, thanks you very much

Jun 18, 2011 at 4:53 PM

 

I ran into the same issue. I discovered that my first mistake was following the mainpage.xaml/mainpageviewmodel.cs naming convention instead of newview.xaml/newviewmodel.cs. In addition I forgot to update the newview.xaml code behind class which was still public class newpage. in short make sure the code behind class name matches the .xaml file name...

Jun 20, 2011 at 3:10 AM
Edited Jun 20, 2011 at 3:11 AM

thank you very much, it fix my problem