Closing Modal Page in Xamarin.Forms - xamarin.forms

I am opening new ContentPage like this:
MediaPage galleryPage = new MediaPage();
await Application.Current.MainPage.Navigation.PushModalAsync(galleryPage);
Now, when done with the task in that page, I would like to close it and come back to last page where I was before opening ModalPage.
I tried like this:
await Application.Current.MainPage.Navigation.PopModalAsync();
But nothing happens.

Are you sure you are executing it from the main thread? Try this:
Device.BeginInvokeOnMainThread(() =>
{
Application.Current.MainPage.Navigation.PopModalAsync();
});

Related

Confirmation on master detail page before exiting

I want to ask for confirmation before exiting page but i tried every way i can find but nothing worked so far.
I have a master detail page as Application Main page.
App.Current.MainPage = new NavigationPage(new FaceApp.MainPageActive());
In that page Detail page is set like following.
Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(RecoveryForm)));
In Recovery form page i have following code
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (await DisplayAlert("Alert", "Are you sure you want to go back ?", "Yes", "No"))
{
// Nothing works here. i tried many things including following
await Navigation.PopAsync();
}
});
return true;
}
Code runs successfully but nothing happens, if i remove code of running in Main thread application gets halted.

Xamarin Pillar navigation

What is the correct way to handle model navigation?
The first case below works as expected but I can then go back to the login page which I don't want. The second case I can see in the debugger that that page is loaded but never shown. Basically the model page stays on top. I am thinking I need to either close the model page before changing pages or I need to handle this differently. I don't won't to pop to root because the root is no longer home but main.
What I really won't to do is change the root, how?
[Edit] This seems to help but there is still a flicker when I unload the modal page.
await _navigator.PushAsync(vm => { vm.NoHistory = true; });
Case 1:
return viewFactory.Resolve<HomeVM>(); - from APP.cs
await _navigator.PushAsync<LoginVM>();
await _navigator.PushAsync<MainVM>();
Case 2:
return viewFactory.Resolve<HomeVM>(); - from APP.cs
await _navigator.PushModalAsync<LoginVM>();
await _navigator.PushAsync<MainVM>(); - Never works.
One way is to reset MainPage completely with the new page when login in successfully.
if (string.IsNullOrEmpty(authLoginToken))
MainPage = new LoginPage();
else
MainPage = new RootPage();
More detailed info, you can take a look the following thread:
https://github.com/asthanarht/CPXamarin/blob/master/CPMobile/CPMobile/App.cs
https://forums.xamarin.com/discussion/48634/from-login-page-to-main-page

Xamarin Forms add new controls dinamically in the content via code

I have a form in my Xamarin project that its result is something like that:
Basically there is a header (1) and a body of this form (2). The header is quite simple build with AbsoluteLayout.
For creating the body (2) I've created my component to show a tab control and then for each tab a specific grid with images and text. For each section, I'm checking in the database how many records there are for it and change the text. This activity is very long and I'm trying to understand why and how I can improve speed.
Then I should cut the corner to add later in my page the tab control so the user can see immediately the header and after few second all page. The code is like the following:
public class MyPage : WaitingPage
{
public MyPage(Card card)
{
LoadingMessage = "Loading...";
ShowLoadingFrame = true;
ShowLoadingMessage = true;
ShadeBackground = true;
WaitingOrientation = StackOrientation.Vertical;
IsWaiting = true;
StackLayout stackPage = new StackLayout() {
BackgroundColor = Color.FromHex("#fff"),
Children = {
ShowHeader(card),
}
};
Content = stackPage;
Task.Yield();
Task.Run(async () => {
Content = await ShowDetails(card);
});
IsWaiting = false;
}
}
I tried different ways to add the content from await ShowDetails(card); but nothing happens. Basically the content doesn't change unless await ShowDetails(card); is executed. I discovered Task.Yield(); (you can wait the page is rendered and showed and then continue) but in this case doesn't work. And also WaitingPage doesn't show the message.
Any suggestions or help? Thank you in advance

Is there a way to disable the background on the DisplayActionSheet using Xamarin Forms?

Issue:
I'm dealing with an issue on how to disable the background on a DisplayActionSheet or DisplayAlert in Xamarin Forms, so the only way the user can "get out" of the popup windows is via the options that the window has prompted.
Here is the screenshot of the problem:
http://postimg.org/image/s16cp66wf/
Do you know if there's a simple way to do this? I've been looking but I couldn't found....
I tried:
Using the methods that came with the control but nothing else came up. What I'm doing right now is to call it till I have an answer.
Code:
From my code behind I call the event
async void OnNextClicked(object sender, EventArgs args)
{
await userValidations.RMLocationServiceWindow();
}
And then I call the popup window
public async Task RMLocationServiceWindow ()
{
string rta = null;
while (rta == null)
{
rta = await App.Current.MainPage.DisplayActionSheet("Do you accept to use the service?", "Cancel", "Continue");
}
}
I couldn't find any that keep the focus on the windows.
Unfortunately this could not be done in displayactionsheet, for you to be able to achieve this, you must create a Modal Page with list where you could be able to disable back button until there is a selection on the list. hope it helps
I think I got your problem. If your problem is about when user not click 'Cancel' or 'Continue' and he click empty space, you wont get value for rta. Think about if user click white space it mean is canceling. This way may help you.
while (rta == null)
{
rta = await App.Current.MainPage.DisplayActionSheet("Do you accept to use the service?", "Cancel", "Continue");
if (rta==null)
rta=="Cancel";
}

catch close browser with onunload function using master page in ASP.NET

I have a website with master page. I want to catch a user trying to close the browser/tab. When I try using onunload in the body tag, it fires not only when I try closing the browser, but also when I navigate to another page.
Any idea how to only catch the event of closing the browser?
You can't distinguish closing the browser from navigating to another page. In both cases the current page is unloaded.
update: maybe you can handle some cases with some jquery, i.e. whenever a link is clicked, set some flag to be able to distinguish it from closing the window or entering a new URL:
<body onunload="checkForClose()">
...
<script>
var _isNavigation = false;
$(document).ready(function () {
// whenever a link is clicked set _isNavigation to true
$('a').click(function () {
_isNavigation = true;
});
});
function checkForClose() {
// show an alert if _isNavigation is not set
if (!_isNavigation) alert("closing the browser (maybe)");
}
</script>

Resources