user1428798
user1428798

Reputation: 1546

redirect page in windows phone

In my mainpage.xaml, I check the localstorage, if the method GetAccept return false, I need to redirect to Mentions.xaml but the problem is that the NavigationService is Null in this step so i catch nullreferenceexecption

        public MainPage()
        {
            CacheManager cache = new CacheManager();
            if (!cache.GetAccept())
            {
                NavigationService.Navigate(new Uri("/Views/AppBar/Mentions.xaml", UriKind.RelativeOrAbsolute));
            }
            InitializeComponent();
            }

How can I do this redirection?

Upvotes: 0

Views: 2231

Answers (3)

user1836122
user1836122

Reputation: 146

You need to get the syntax correct. Try copying what I put below.

this.Loaded += 
(
    (sender, event) =>
    {
        NavigationService.Navigate(new Uri("/Views/AppBar/Mentions.xaml",
            UriKind.RelativeOrAbsolute))
    }
);

You are missing extra parenthesis and you have an extra semi-colon thrown in there.

Upvotes: 0

user1836122
user1836122

Reputation: 146

It appears you put in a += = (sender, events) You need to remove the extra = as I did below.

public MainPage() {

    InitializeComponent();
    CacheManager cache = new CacheManager();
    if (!cache.GetAccept())
    {            this.Loaded += (sender, event) =>{
        NavigationService.Navigate(new Uri("/Views/AppBar/Mentions.xaml", UriKind.RelativeOrAbsolute)); };
    } 

Upvotes: 1

FosterZ
FosterZ

Reputation: 3911

very first follow this Link

The NavigationService is instantiated when the current Page raises its Load event, in your solution your using an Un initialized NavigationService that's the cause for your NullReference Exception. So if you manually do the load event of the page then you'll get your NavigationService instance, following code is snippet for manually loading the page loaded event, write this right after your pages InitializeComponent() method

 this.Loaded += (sender, event) =>
  {
      var navigationService = NavigationService;
      navigationService.Navigate(new Uri("/Views/AppBar/Mentions.xaml", UriKind.RelativeOrAbsolute));
  };

Upvotes: 2

Related Questions