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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,224
Location
Sydney, Australia
Programming Experience
10+
You have to pass a DependencyObject when calling GetNavigationService. Are you?

By the way, please don't say things like "it doesn't seem to be working". Explain EXACTLY what happens. If there's a compilation error then say so and provide the error message. If there's a run time error then say so and provide the error message. If the code runs but exhibits unexpected behaviour then describe the behaviour. We only know what you tell us so you have to give us all the relevant information or else we're left to make assumptions that can mean wasting our time and yours if they're wrong.
 

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
Compilation Error 1: The best overloaded method match for 'System.Windows.Navigation.NavigationService.GetNavigationService(System.Windows.DependencyObject)' has some invalid arguments D:\Docs\Networld\CDM Project\Interfacing USB in C#\NWCDM\NWCDM2\NWCDMLibNew.cs 842 37 NWCDM
Error 2: Argument 1: cannot convert from 'NWCDM.NWCDMLibNew' to 'System.Windows.DependencyObject' D:\Docs\Networld\CDM Project\Interfacing USB in C#\NWCDM\NWCDM2\NWCDMLibNew.cs 842 76 NWCDM

Since the function is not called from a click event what should I pass? Please note that the function is written in NWCDMNew.cs and called from Start.xaml.cs that checks to redirect to ErrorMsg.xaml.
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,224
Location
Sydney, Australia
Programming Experience
10+
I have no idea what NWCDMNew.cs is. Does it have access to an appropriate DependencyObject? If not then you should write your ShowErrorScreen method with a parameter of that type and then you can pass it on to the GetNavigationService call. Your WPF Window or whatever can then pass in an appropriate DependencyObject when it calls ShowErrorScreen.
 

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
Sorry, it was acrually NWCDMLibNew - that is just a class file where I wrote all the functions required throughout the application. The Function ShowError() is supposed to load the ErrorMsg.xaml page showing the customized error string on a label.

As I said earlier when I use,

NavigationService nav = NavigationService.GetNavigationService(this));
nav.Navigate(new Uri("ErrorMsg.xaml", UriKind.RelativeOrAbsolute));


from a click event of a button it works because then the dependency object is the button. What should I use pass in GetNavigationService if not called from any click event?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,224
Location
Sydney, Australia
Programming Experience
10+
As I said earlier when I use,

NavigationService nav = NavigationService.GetNavigationService(this));
nav.Navigate(new Uri("ErrorMsg.xaml", UriKind.RelativeOrAbsolute));


from a click event of a button it works because then the dependency object is the button.
Um, no. `this` does not refer to the Button unless the code is inside the Button class, which it's not. It is presumably inside a Window, so `this` refers to that Window.
What should I use pass in GetNavigationService if not called from any click event?
I've already told you what to do.
... you should write your ShowErrorScreen method with a parameter of that type and then you can pass it on to the GetNavigationService call. Your WPF Window or whatever can then pass in an appropriate DependencyObject when it calls ShowErrorScreen.
If you would have used `this` as the DependencyObject when calling GetNavigationService in the Window then obviously you should use `this` when calling the rewritten ShowErrorScreen method too, so it will then pass on that same object to GetNavigationService.
 

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
I didn't get it. What would be the DependencyObject to pass to GetNavigationService when called from a page class just after the InitializeComponent();
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
868
Location
Norway
Programming Experience
10+
ShowErrorScreen(this) when called from page class will pass the Page instance, which is the DependencyObject (Page inherits DependencyObject), to that method, and you will in turn pass that argument to GetNavigationService. That is the same as calling GetNavigationService(this) directly in the page class. So you will need to add a DependencyObject type parameter to the ShowErrorScreen method.
GetNavigationService when called from a page class just after the InitializeComponent();
That is not possible, NavigationService is not available until Loaded event.

By the way, you don't need GetNavigationService, page class has a NavigationService property that you can use, you can pass this to method instead of the DependencyObject.
 

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
I tried modifying the function as follows,

public void ShowError(Page s)
{
try
{

NavigationService nav = NavigationService.GetNavigationService(s);

nav.Navigate(new Uri("ErrorMsg.xaml", UriKind.RelativeOrAbsolute));
}
catch (Exception e)
{
MessageBox.Show(e.Message);


}


}

and called from a page as

ErrorMsg x = new ErrorMsg();
ShowError(x);

There is no compilation error but shows the following runtime error message

Object reference not set to an instance of an object.

Please note that ErrorMsg is a page where the error message is displayed.
 

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
I tried with the following now

Start x = new Start();
ShowError(x);

Where Start.xaml is the page ShowError function is called from. This time it works. However it loops around as if the Start.xaml page is being called for loading

 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,224
Location
Sydney, Australia
Programming Experience
10+
I used ShowError(this) and got the error message as before

Object reference not set to an instance of an object.

Show us the method and the calling code.
 

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
ShowError Function declared in NWCDMLibNew.cs:

public void ShowError(Page s)
{
try
{
s.NavigationService.Navigate(new Uri("ErrorMsg.xaml", UriKind.RelativeOrAbsolute));

}
catch (Exception e)
{
MessageBox.Show(e.Message);


}


}

Calling codes from Start.xaml:

InitializeComponent();
if (!IsDeviceReady()){
ShowError(this);
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
868
Location
Norway
Programming Experience
10+

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
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?
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
868
Location
Norway
Programming Experience
10+
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.
 

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
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.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
868
Location
Norway
Programming Experience
10+
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);
 

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
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:
Top Bottom