I want to navigate from my cs class to my xaml main page.
i tried it with:
NavigationService.Navigate(new Uri("/Contacts.xaml", UriKind.Relative));
but i get a error: system nullreferenceexception ;
How can i navigate to my mainpage
I think that you are trying to call NavigationService.Navigate too early, for example, in Page constructor.
See NavigationService.navigate null reference exception.
Related
I am working on windows phone 8.1 runtime universal app and using webview to go to page. I want to be able to download the HTML on the page into a string so I can use t in my app. I have used these links for help but to no avail:
http://codesnack.com/blog/2012/01/05/metro-webview-source-workarounds/
Retrieve Inner Text from WebView HTML in Windows/Windows Phone 8.1
https://social.msdn.microsoft.com/Forums/windowsapps/en-US/331fa2db-3efa-40e2-af08-ddebe9424869/how-to-get-html-source-information-of-webview-in-windows-phone-81?forum=winappswithcsharp
so I added this line in my NavigationCompleted event handler
string pageContent = webBrowser1.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" }).ToString();
But when I step through the code all I get is System.__ComObject as the value for pageContent .
Any ideas what is going on here?
Thanks!
InvokeScriptAsync returns a IAsyncOperation not a string. If you call ToString() on it you'll just get the type of the object (System.__ComObject).
You'll almost always want to "await" Async functions like this. That will process the returned operation, wait for it to finish, and then return its type.
If you change your code to the following then pageContent will get the HTML from your evaled function:
string pageContent = await webBrowser1.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" });
I have a page in a WP8 application, that every time I navigate to it, the constructor is called.
From what I know, the constructor of a page called only once at the first time the page loaded. my page is very heavy, and every construction takes wasted time..
this my navigation code, usual one:
NavigationService.Navigate(new Uri("/Views/Pages/ContentControlNew.xaml", UriKind.Relative));
and this is the constructor of the page:
public ContentControlNew()
{
InitializeComponent();
}
Not special.. is it normal that the constructor is called every time? Please tell me if you need more details because I don't know what else to say about this subject.
Yes this is normal because whenever you use NavigationService.Navigate it always creates a new page object and adds that (pushes it) to the navigation stack. For example when you use GoBack() it pops it out of the stack and destroys it, but when it gets back to the previous page it doesn't call the constructor since that one was already in the stack and does not have to be recreated.
If you don't want to create a page every time you navigate to it, you should look into Navigation Models for Windows Phone for some ideas on how you can tackle this.
I've added the Share Target declaration to my app for the data format WebLink and everything works as expected. However, when I add a JumpListItemBackgroundConverter or JumpListItemForegroundConverter anywhere in the app, the app hangs on the splash screen when you enter the app using the Share from IE. No exception, no crash, the debugger doesn't even stop. All I get is a cryptic error in the output window, "The program '...' has exited with code -1073741819 (0xc0000005) 'Access violation'." The documentation for those converters say they're fine with universal apps, just that they've been moved to a different namespace. Has anyone been able to get these two things to work in the same app? If so, where did I go wrong? Or is there something better than those two converters to get the LongListSelector look and feel?
Steps to reproduce:
Create a new universal app. I chose hub.
Add a share target of format WebLink to the appxmanifest declarations.
Add a new page to point the share contract to.
Add the OnShareTargetActivated code to app.xaml.cs to open the new page. See code below
Add a JumpListItemBackgroundConverter to the resources of the main page of the app. You don't need to apply it to anything, just declaring it is enough to break the sharing.
Go to IE and share a link. It should hang on the splash screen.
Code for app.xaml.cs:
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
// Replace SharePage with the name of the share target page
var rootFrame = new Frame();
rootFrame.Navigate(typeof(SharePage), args.ShareOperation);
Window.Current.Content = rootFrame;
Window.Current.Activate();
}
It turns out this is a bug in the emulator. It works if you test on a physical device.
MSDN Forum - JumpListItemBackgroundConverter and Share Target in Windows Phone 8.1
How can we navigate to other page on click on button in user control in windows store app? I tried by making a new frame object and calling navigate method, but no luck till yet.
thanks.
The Frame is a ContentControl that hosts the pages. If you want to navigate back and forth between pages you need to use a single Frame control. The default one is created in the App class in the default Visual Studio templates. You can save the instance reference of that Frame like by having a static property on the App class like: public static Frame RootFrame { get; private set; } and then set it where it is constructed - App.RootFrame = new Frame(). Then you can navigate simply by calling App.RootFrame.Navigate().
I have something like this :
SplashPage -> MainPage -> Settings -> About
SplashPage is only page with my logo and animation for about 1 second, and then I redirect my user to MainPage. First problem was that when I press back button on MainPage, I'm back to splashpage and that wasn't good. I solved that by this piece of code :
private bool navigateBack;
public SplashPage()
{
InitializeComponent();
navigateBack = false;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (navigateBack)
{
this.NavigationService.GoBack();
}
else
{
navigateBack = true;
base.OnNavigatedTo(e);
}
}
This way on back button press, user never see splashpage again, but I have weird problem. Application is throwing Navigation Exception that CanGoBack property is false and application exit. It's true, that I want to exit from app, but not using exception, because I will fail certification in marketplace.
My question is how to navigate back from MainPage and exit application, but with no exception thrown. Thank you in advance!
Introducing a separate page for your splash screen introduces more problems than it solves (as you're finding out), so I'd reccomend using an overlay on your main page instead. I [posted about using splash screens and this very situation a while back.
Read the following for background on this scenario and how to approach it:
http://blogs.msdn.com/b/ptorr/archive/2010/08/28/introducing-the-concept-of-places.aspx
and
http://blogs.msdn.com/b/ptorr/archive/2010/08/28/redirecting-an-initial-navigation.aspx
in Mango you can use
NavigationService.RemoveBackEntry()
to remove a page from your page stack, i.e your splashpage as soon as you hit your MainPage
so if you have your splashpage to mainpage you would call the function twice
the first time to remove MainPage from the page stack and the second time to remove SplashPage from the page stack, you will notice that when you press the back key now you will close the app