How to stop reload page after navigation windows phone? - windows-phone-7

My mainpage call to Page1. I add a webbrowser control to the Page1. When the webbrowser load data complate, I go the background.Then, i back my Page1, webbrowser reload data. How to stop reload it?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_mlink = NavigationContext.QueryString["link"];
web.Navigate(new Uri(_mlink, UriKind.Absolute));
}

You can use the NavigationEventArgs.NavigationMode property to determine which direction the navigation is coming from.
The available options can be found here (MSDN), but listed out, they are:
New
Back
Forward
Refresh
So my tip would be to determine which value of this you want to navigate the web browser control on (I would guess New).

NavigationEventArgs.NavigationMode works only in windows phone 8. So edit the code like this.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_mlink = NavigationContext.QueryString["link"];
if(web.Source.AbsolutePath!=_mlink)
web.Navigate(new Uri(_mlink, UriKind.Absolute));
}

Related

Navigate within modal dialog using shell

I'm using shell to navigate in my Xamarin Forms app. I want my settings to be in a Modal dialog which is navigated to by calling PushModalAsync. In that settings page I want to navigate to other pages, eg password change. But since I'm not using NavigationPage I cannot get it to work.
How do one use Shell with Modal and navigate within that Modal page?
I can get it to work by setting my SettingsPage as a new MainPage. But then I don't get the modal navigation animation.
using NavigationPage will probably solve the problem but then I will
no longer use Shell which I want to use.
I think wrap your Settings page in a NavigationPage would solve your problem. You can still use Shell after using NavigationPage.
For example, you can do some push actions in the SettingPage:
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new BearDetailPage());
}
In the BearDetailPage, if you want to use Shell, you can dismiss the SettingPage by send a message usingMessagingCenter:
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PopToRootAsync(false);
MessagingCenter.Send<BearDetailPage>(this, "BackToShell");
}
And in the SettingPage, subscribe that message:
public SettingPage()
{
InitializeComponent();
MessagingCenter.Subscribe<BearDetailPage>(this, "BackToShell", (sender) =>
{
// Do something whenever the "BackToShell" message is received
this.Navigation.PopModalAsync();
});
}
If you don't want the navigationbar in the SettingPage, you can just hide it.

How to go to an xaml page by clicking on the button in WebBrowser control in Windows Phone 7 or 8

I have a XAML Page with a WebBrowser tag. WebBrowser has few text boxes and a button, here my question is after clicking on that button i have to go to a XAML page. How can i do this?
By clicking the button in WebBrowser, i want to go to a XAML Page which is in solution file.
Please help me on this.
I believe you can solve this by listening to the WebBrowser.Navigating event. First do:
myWebBrowser.Navigating += myWebBrowser_Navigating;
Then in the handler method check if the uri navigated to is the pointed to by that button:
private void myWebBrowser_Navigating(object sender, NavigatingEventArgs e)
{
if(e.Uri.AbsolutePath == URI_OF_BUTTON) {
NavigationService.Navigate(new Uri("/otherPage.xaml", UriKind.Relative));
}
}
It sound like Page Navigation.
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Pasta.xaml", UriKind.Relative));
}

How to load MainPage when goBack(); form SecondPage?

I use NavigationService.Navigate("..."). But I want to load MainPage again when I click the Back button by event goBack();
In your goBack method you can use the NavigationService.GoBack() call to move from SecondPage back to the MainPage. You can view all the Navigation methods available to you on MSDN.
You can override the method OnBackKeyPress, and inside the method write code shown below, it ll navigate to MainPage.xaml
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
If you are using binding do this:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
App.ViewModel.LoadData();
}
And in MainViewModel.cs
public void LoadData()
{
Items.Clear();
this.Items.Add(new ItemViewModel() { LineOne = "runtime one", LineTwo = "Data info 2" });
this.IsDataLoaded = true; }
Like Nigel said, you can use NavigationService.GoBack() however this will only work if you went from the MainPage to the second page.
The only other way to do this is how you have mentioned using NavigationService.Navigate(uri).
Override the navigated to event
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (PhoneApplicationService.Current.State["msg"].ToString() == "from page2")
{
// do something when its comming from page2
}
base.OnNavigatedTo(e);
}
Now that will trigger every time its navigated to but you could send some parameters so you know what to depending on what page it came from.
PhoneApplicationService.Current.State["msg"] = "from page2";
NavigationService.Navigate(new Uri("/MainPage.xaml?msg=", UriKind.Relative));
There are two ways to get back to your MainPage.
(First Method) Override the OnBackKeyPress() and write the following snippet inside it.
NavigationService.GoBack();
Note :
This will move to the MainPage only if the MainPage is the previous page in your Navigation history.
(Second Method) Override the OnBackKeyPress() and write the following snippet inside it.
NavigationService.Navigation(new Url("/MainPage.xaml",UriKind.Relative));
Note :
This will create a New MainPage and navigates to it. So if you navigated from MainPage to SecondPage and if you navigate again to MainPage using the above snippet, you will be having two MainPage in your Navigation history.
So, use any one of the snippet according to your scenario.
if WP7 or WP8 : NavigationService.GoBack()
if WP8.1 : Frame.GoBack();
and you can Override the navigated to event
protected override void
OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
// do nothing }
I don't recommend overriding the OnBackKeyPress method and calling NavigationService.Navigate method within it as it may mess up your page back stack.
I assume your app has more than 2 pages. Otherwise you should just call NavigateService.GoBack() as others have recommended.
Example
Start on MainPage (backstack: empty)
Navigate to SecondPage (backstack: MainPage)
Navigate to ThirdPage (backstack: SecondPage, MainPage)
Click the back button (overridden) and you are taken to MainPage (backstack: ThirdPage, SecondPage, MainPage)
Click the back button and you are taken to the ThirdPage (backstack: SecondPage, MainPage). This is not the expected behaviour as the application should have exited.
Now you're stuck in a back button loop.
Solution
Override OnNavigatingFrom and remove any of the backstack entries you don't want the user to go to. You can either allow the user to press the back button to get back to the MainPage from the ThirdPage or you can call NavigationService.GoBack().
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
// Remove all backstack entries except for the first one
while(NavigationService.BackStack.Count() > 1)
{
NavigationService.RemoveBackEntry();
}
}

WebBrowser control: show to user when it's navigating

I'm developing a Windows Phone application.
I'm using WebBrowser control and I want to show to users when is loading a page. I've used events:
private void Browser_Navigating(object sender, Microsoft.Phone.Controls.NavigatingEventArgs e)
{
LoadingText.Visibility = System.Windows.Visibility.Visible;
}
private void Browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
LoadingText.Visibility = System.Windows.Visibility.Collapsed;
}
But it doesn't work.
Any advice?
I think your problem is in the navigated event - this
From msdn
Occurs when the WebBrowser control has navigated to a new document and has begun loading it.
This obviously could be long before the document is actually rendered.
I'm not sure there's any event to use to determine when the page is fully loaded and is rendered.
In iron7, I detect when the editor is loaded by using a timer - that timer keeps trying to call javascript methods in the script - I know these are only available after the document javascript ready occurs.
Try using the LoadCompleted event:
private void Browser_LoadCompleted(object sender, NavigationEventArgs e)
{
LoadingText.Visibility = System.Windows.Visibility.Collapsed;
}
This ensures that once everything is rendered the loading bar will disappear.
See the msdn page: http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.webbrowser.loadcompleted(v=VS.92).aspx
(I think Stuart was looking at the Windows Forms implementation of WebBrowser rather than the Phone Control)

PhotoChooserTask + Navigation

I taken two Images & added event (MouseButtonDown) for them.
When first image handles event to open Gallery. Second image handles events for open camera.
When user has choosed his image from the gallery, I want to navigate to next page. Its navigates. But before completing navigation process, it displays MainPage & then moves toward next page. I didnt want to display the MainPage once user chooses the image from the gallery.
Plz help.
Thanks in advance.
public partial class MainPage : PhoneApplicationPage
{
PhotoChooserTask objPhotoChooser;
CameraCaptureTask cameraCaptureTask;
// Constructor
public MainPage()
{
InitializeComponent();
objPhotoChooser = new PhotoChooserTask();
objPhotoChooser.Completed += new EventHandler<PhotoResult>(objPhotoChooser_Completed);
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(objCameraCapture_Completed);
}
void objPhotoChooser_Completed(object sender, PhotoResult e)
{
if (e != null && e.TaskResult == TaskResult.OK)
{
//Take JPEG stream and decode into a WriteableBitmap object
App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
//Delay navigation until the first navigated event
NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
}
}
void navigateCompleted(object sender, EventArgs e)
{
//Do the delayed navigation from the main page
this.NavigationService.Navigate(new Uri("/ImageViewer.xaml", UriKind.RelativeOrAbsolute));
NavigationService.Navigated -= new NavigatedEventHandler(navigateCompleted);
}
void objCameraCapture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//Take JPEG stream and decode into a WriteableBitmap object
App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
//Delay navigation until the first navigated event
NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
}
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
objPhotoChooser.Show();
}
private void image2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cameraCaptureTask.Show();
}
To my knowledge when you use one of the choosers, like the Photo gallery or the camera, when your application is activating it will take it back to the page you left it. I don't think there is a way to get around this itself. What you would have to do is catch the Activating event in your main page code and Navigate to the desired page from there.
Now I am not completely sure how you would pass the image from the MainPage to the target page. It does not look like there is a property in the Navigation service to store this value. But you could either set it in an application wide variable, ModelView or even store it in the Isolated Storage area.
You could work around this by navigating to an intermediate blank page and have that intermediate page launch the tasks. When the tasks are completed you can then navigate as normal to your new page and only this blank page will show in transit.
Chris is correct that some of the tasks will navigate away from your app (effectively tombstoning it) and will the re-activate your application when the user returns from the task. For the camera this is particularly difficult, as to my knowledge there is no simple way to detect when you are returning from the camera. Also the camera doesn't work when attached to the debugger or Zune software (at least this is true on my HTC Surround), which makes troubleshooting quite difficult!
In my WP7 Barcode Scanning application I ended up using flags on the PhoneApplicationService class to help track where the navigation events are coming from. Something like:
PhoneApplicationService.Current.State["ReturnFromSampleChooser"] = true;
You can then check for these flags in the PhoneApplicationPage_Loaded or OnNavigatedTo method of your main page and redirect to the desired page as needed. Just make sure to clear the flag and be careful to not cause any loops in the navigation, as that might make your app fail certification (back button must ALWAYS work correctly).
For an example of how to use the camera and set/clear flags using PhoneApplicationService check out the source code for the Silverlight ZXing Barcode Library. You can download the full source here or browse the files online.

Resources