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.
 
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.
 
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:
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.
 
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?
 
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.
 
I didn't get it. What would be the DependencyObject to pass to GetNavigationService when called from a page class just after the InitializeComponent();
 
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.
 
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.
 
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

 
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.
 
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);
}
 

Latest posts

Back
Top Bottom