Navigation caching only when navigating back - c#

I have a little bit of experience programming Android apps.
And now i am wondering if the same thing is possible:
(Android:)
When navigating in an android app between activities, the state of the previous activity is hold. By pressing the back button, you get the previous activity the way you left it. When navigating towards an activity, this opens the activity in the default state.
(UWP:)
Navigating between pages:
I have a page which should be cached for when i navigate back.
But when I navigate towards the page, it should open the page in the initial state.
How is this possible?
(I already am able to enable navigationcachemode. I wonder if i can disable it, or create a new instance of the page for example.)

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
NavigationCacheMode = NavigationCacheMode.Disabled;
base.OnNavigatedFrom(e);
}
Just disable the NavigationMode when you're navigating back from a page. So you force the creation of a new page when you'll navigate again to that page (and the NavigationMode will be set to the NavigationMode you set in the constructor or XAML).

Related

Is there a way to disable back or forward navigation in Webview2 winforms

In webview2, setting source property to Uri for navigating.
Let's say First URL is opened in webview2 then navigated to another URL.
With the back button on Right-click context menu, able to navigate to the first page.
From google search, found there is no direct way to disable back and forward as of now.
In the normal system forms browser, performed an approach like below which is working
added a bool variable(like IsMyNavigationCall), setting it to true whenever just before navigating to some URL
Added a check in NavigationStarted event and if it's false(when navigation triggered from actions like back) cancelling the request and resetting the bool variable.
In Webview2, it's not working. The problem is navigation is not cancelled even after setting CoreWebView2NavigationStartingEventArgs.cancel to true.
Is there any way or kind of hack to prevent navigation between the back and forward?
It seems that you're looking for CoreWebView2.HistoryChanged Event. In order to enable/disable a "Back" button and a "Forward" button when a new URL is navigated to in WebView2, try the following:
Given:
WebView2 control: webView21
Back button: btnBack
Forward button: btnForward
//subscribe to CoreWebView2 events (add event handlers)
webView21.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;
...
private void CoreWebView2_HistoryChanged(object sender, object e)
{
btnBack.Enabled = webView21.CoreWebView2.CanGoBack;
btnForward.Enabled = webView21.CoreWebView2.CanGoForward;
}
You can disable the context menu and the accelatorkeys via CoreWeView2.Settings
https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.1245.22#arebrowseracceleratorkeysenabled
https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.1245.22#aredefaultcontextmenusenabled
Example:
webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;

windows silverlight app stuck on one page

In my windows phone app I am doing a scanning process, wherever I am in the app, when the scan button is pressed on my bottom appbar, I navigate to my scan page, known as ScanView.xaml.
Problem:
I have overridden onnavigatedTo method of scanView to initiate scanning screen.
When I press back button while I am on the scanning screen It goes back to ScanView.xaml and hence onnavigatedTo method is again called and my scanning screen again appears.
What I want to do is, when I press back button while on scanning screen it should navigate directly back to my mainpage.xaml
P.S : I have tried overriding the backbutton handler, but it is still not working.
Here is my code.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//this method is invoked as soon as we are navited to the ScanView
Scanning();
}
Thanks in advance
Just remove ScanView.xaml from the navigation stack (it is the item at the top of the stack) when navigated to the scanning page.

How to notify the page when GoBack() happened?

Here is what my app does:
The first page can navigate to the second page, and the second page displays a list of data. The user can choose one of them then the app will bring the data back to the first page.
Sounds easy, but I'm confused with the Windows Mobile Navigation Model.
The first page navigates to the second page, using this code:
this.Frame.Navigate(typeof(SecondPage));
and the second page uses the code below to go back:
this.Frame.GoBack();
How could the first page know if the second page disappeared? I want to update the UI on the first page after the second page disappeared.
Now, I used a static class to keep the data that user picks, but I have no idea when should be the right time to update the first page.
Is there any way to get an event or notification?
This is quite simple, since UWP does this for you. I noticed you're not using MVVM, so you can simply override the OnNavigatedTo event in your page. This event is triggered when navigation to your page is completed (and thus the second screen dissapeared). Simply check for NavigationMode.Back to confirm you're returning and not navigating forward.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == NavigationMode.Back)
{
// coming from 2nd page, so refresh your data
}
}

Immediately redirect to another page in WPF

I have a page that several other pages navigate to. However in some circumstances the user should not see this page, so I want to send them to another page instead.
Rather than update the rest of the calling code, I just want to change this page to handle it.
public MyPage()
{
Loaded += MyPage_Loaded;
InitializeComponent();
// Other stuff
}
void MyPage_Loaded(object sender, RoutedEventArgs e)
{
if(condition)
NavigationService.Navigate(someUri);
}
Since the NavigationService isn't available in the constructor I have to hook up to the Loaded event and do the redirect there. The problem is that the page has already been loaded and displayed to the user. There is also a slight delay before redirecting the user.
Is there a better way to do this where the redirect is seamless?

NavigationService.GoBack(); question

When I call NavigationService.GoBack(); it doesn't reload the page.
For example I have Page 1 which is my Login page, then I Navigate to Page 2 to the Settings Page. When I have saved my Settings on Page 2 I wish it to Navigate back to Page 1 and show the new settings that are displayed.
Is there any call I can make where the Navigate Service Goes Back AND forces the page to re-initialise? (ie call the page loaded method).
Thanks
Solved it. Use
protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e) { //INSERT RELOAD METHOD HERE }
In the PhoneApplicationPage part of every page
When you navigate to next page the previous page is destroyed (if it does not run the background thread). You have a sevral ways to display settings on page nr 1.
When user log in and go to page 2 save your setting to the Isolated Storage and when he presses the back button use
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
var settings = LoadMySettingsFromIS();
if (settings =! null)
{
// update it here
}
base.OnNavigatedTo(e);
}

Categories

Resources