Question Is it possible to load a navigation page without clicking on any button or any event

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
I have this following code that acts when there's an error:

public void ShowErrorScreen()
{
NavigationService nav = NavigationService.GetNavigationService(this));
nav.Navigate(new Uri("ErrorMsg.xaml", UriKind.RelativeOrAbsolute));
}But it doesn't seem to be working for "this"

Before loading a page a few things are being checked. If eveything is ok it shows a page otherwise it shows another page with error information. I can navigate to a page when I initiate the navigation from a button click event. But I just want to redirect to a page according to my requirement without any click event as stated earlier. Its just loading page on a frame of the main window without any clicking event (after a sequence of error checking). Need an alternate for the method above.
 
Thanks!! IT'S WORKING!!!!

However, if I ever need to call the ShowError function from outside the page load or clcik event how would I do that?
 
NavigationService is not available in page until Loaded event has occurred, so it is about when and not about where. After Loaded you can use it where you want, it don't have to be from that event handler, but you need to know it has happened.

There could be other options, for example from window class you can use Frame.Navigate directly after InitializeComponent. So maybe you could check the conditions there and navigate the frame based on that.
 
My Main Window is named MainWindow. It has a frame named MainFrame. As per your advice I could use frame.content/frame.source. However, I cannot access this frame from any xaml page. How would I do it? If possible it would be the most simplest one to load a page from anywhere within the app.
 
My Main Window is named MainWindow. It has a frame named MainFrame. As per your advice I could use frame.content/frame.source. However, I cannot access this frame from any xaml page. How would I do it? If possible it would be the most simplest one to load a page from anywhere within the app.
I said Frame.Navigate in window code, which in your case would mean something like:
MainFrame.Navigate(theUri);
 
From window is alright. However when I try navigating to another page from a page with the following code after it checks 20 seconds timer has elapsed or not

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            NWCDMLibNew x = new NWCDMLibNew();
            if (i >= 0)
            {
                if (Global.VarLang == 0)
                {
                    TimeLeft.Text = i.ToString();
                }
                else
                {
                    TimeLeft.Text = x.Convert2Bangla(i.ToString());
                }


                i--;                
            }
            else
            {                
                NavigationService nav = NavigationService.GetNavigationService(this);
                if (nav == null)
                {
                    MessageBox.Show("nav is Null");
                }
                else
                {
                    MessageBox.Show("nav is not Null");
                }
                nav.Navigate(new Uri("Start.xaml", UriKind.RelativeOrAbsolute)); 
                
            }


It shows the following error message:
"Object reference not set to an instance of an object." just after loading the Start.xaml


I have checked "this" is not null. nav is not null before going to Start.xaml but gets null just after.
 
Last edited by a moderator:
Your nav.Navigate can only go into the else code block, where nav is not Null.

Does it make sense to use a timer event, though? That is usually meant for reoccuring events. Why not use the Loaded event to determine when to navigate?

nav is not null before going to Start.xaml but gets null just after.
Same as above, why would you try to navigate again from a page you have already navigated away from?
 
The timer is used to track if 20 seconds have elapsed or not and notify the user of how much seconds left. When there's no seconds left I would like redirect it to Start.xaml.

I didn't get "why would you try to navigate again from a page you have already navigated away from?" I am only navigating once from the source page to Start.xaml where the 20 seconds are over i.e. i=0.
 
The timer is used to track if 20 seconds have elapsed or not and notify the user of how much seconds left. When there's no seconds left I would like redirect it to Start.xaml.
That makes sense, and in that case did you stop timer after first navigation?
I have checked "this" is not null. nav is not null before going to Start.xaml but gets null just after.
If nav is not null when you navigate and you only navigate once, then how can you say nav is Null later? It must mean you are attempting to navigate more than one time.
me said:
Your nav.Navigate can only go into the else code block, where nav is not Null.
Also make sure you change this code, to prevent using navigation service should it for any reason any time be Null.
 
Sorry to bother you again. But I am getting similar exception during the following code:

Dispatcher.Invoke(new System.Threading.ThreadStart(delegate
{
NavigationService.Navigate(new Uri("TmpMsg.xaml", UriKind.RelativeOrAbsolute));
}));

Interestingly it shows the null exception only when it is called for the 2nd time. Exepecting the similar help as usual. Thanks in advance.
 
Back
Top Bottom