Why does my modal page have a back button? - c#

Project and problem explanation
In my case I have a page which instructs the user how to move a certain machine. This page is supposed to be a modal page since the page should not be navigated away from without performing or completely canceling the action.
I have a mainpage with a listview which opens the details page of any of the items contained in the listview on click through the onSelect method:
Navigation.PushAsync(new FooPage(string Name)); /* gets called in the
onSelect method if selection is not null*/
The model is retrieved within the details page's viewmodel after passing the name to it and then using a model manager injected into the viewmodel with Unity to retrieve it.
-- we now both have a mainpage and detailspage on the stack which of only the detailspage has a backbutton --
On the detailspage we have a button called "Teach" with a x,y,z field next to it after clicking it this method gets called:
private async Task Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushModalAsync(new ManualTeachPage());
}
which then, as it should, creates the page but then for some reason decides to add a back button to it:
Debugging and Research
This behavior is not visible on Android which does not have a visible back button or navigation bar on this page but does have a backbutton on the Details-page as well.
I have used modal pages before but I have never seen this kind of behavior, I have tried using the Navigation property of the Application.Mainpage itself which resulted in the exact same result except for one case.
I thought it might have something to do with me switching out the Application.Mainpage at one point(there is a stack of tutorial pages the user has to go through), seems like calling the pushModalAsync one line AFTER setting the new mainpage then it DOES push the page as a modal page and performs like one (without a back button) but does not do so after this point.
No bug reports on Bugzilla about this either as far as I have seen, neither have I found anything on the internet about this particular problem.
Note that when clicking the back button on the teachPage it returns to the detailspage. When the teachPage gets pushed it does actually get pushed onto the ModalStack.
update 1
Checked again if the modal page which I was talking about was the only modal page on the modal Stack, it was. NavigationPage.SetHasBackButton(this, false); does not seem to work either as suggested by Diego Rafael Souza.
update 2
I thought I would temporarily disable the backbutton by using the onBackButtonPressed within the teachpage until I had found a solution. Turns out this doesn't work for UWP anymore, this method does not get called anymore on this page or any page for that matter. It does work for the Android Hardware button though.
update 3
I tried using:
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
AppViewBackButtonVisibility.Collapsed;
This does hide the backbutton on UWP but for some reason #if WINDOWS_UWP doesn't work at all. If I do this without the #if the program won't build for Android. No solution yet, not only this but when using this fix the other pages still had the onBackButtonPressed method disabled.
update 4
after updating to the newest version of xamarin forms the OnBackButtonPressed started working again. The backbutton still appears on the page but I now have disabled it.
Recreating the problem
I recreated the problem in this small test project:
https://www.dropbox.com/sh/6btsud3uvw503ee/AAAiaGb3TwhMrZMJb2j-rd36a?dl=0

In your function Button_Clicked() in your example, in page DetailPage, where you call your Modal, right after calling PushModalAsync(new TeachPage());, use:
NavigationPage.SetHasNavigationBar(this, false);
NavigationPage.SetHasBackButton(this, false);
My guess is when you tried using the above SetHasBackButton, you were doing it on the Modal itself, but the back button you were actually seeing came from your DetailPage.
Once you add this, the modal pops up, and the back button disappears. If you set a "Back" button on your modal in order to close it, you can easily get your NavBar and BackButton back in your DetailPage by adding in an OnAppearing() Function in your DetailPage code-behind like so:
protected override void OnAppearing()
{
base.OnAppearing(); // do the usual stuff OnAppearing does
NavigationPage.SetHasNavigationBar(this, true); //get your navbar back
NavigationPage.SetHasBackButton(this, true); //get your back button back
}

Related

How to create a MainLayout for refreshing the current page using Blazor?

My MainLayout contains a horizontal menu as well as a button on the top of the screen. The button brings up a modal for the user to make a selection. After it closes I need the current page to refresh because their selection affects key state which affects all pages.
I am calling this.StateHasChanged(); in the MainLayout page. This does cause its content to refresh. However, it does not cause the content of the page contained in the #Body to refresh. I have looked quite a bit and don't see how to make this happen.
Either pass the values down via a CascadingValue, because that will cause consumers to render when the value changes - or if you are relying on state external to the components (such as in a service) then you'll need to put an event on there that is triggered when your state changes, and your consuming components will have to subscribe and call InvokeAsync(StateHasChanged)

Xamarin C# - Is it possible to check the current active Layout?

I have a problem/question?
I created a button in my custom navigation menu, so when I click the button it goes to for example Page1. But now I only want to make the button active if the current active page not is equal to Page1. Is this possible and how?
If I understand correctly, you want to disable the button when Page1 is being displayed.
Most, if not all, Xamarin Forms controls have an IsEnabled property, so you want to set this to false when Page1 appears, and to true when it disappears. A ContentPage has onAppearing and OnDisappearing virtual methods that you can override to run code when the page appears and disappears respectively.
However, context matters. Knowing absolutely nothing about how you have set up your custom navigation menu, I cannot say exactly how you can do this. Is a reference to the button available in the ContentPage's OnAppearing method? If not, you need some way to get a reference to the button, or if the button is using bindings, to the view model that the button is binding to (which assumes you are using a binding for the IsEnabled property of the button), to set the appropriate property to disable/enable the button.
EDIT: Oh and see your actual question was if you can see the current active page. You can check Application.Current.MainPage to see what page is currently active, but if it is a NavigationPage, then you would have to retrieve the last page in the navigation stack to see what page is actually being displayed. Likewise for any other "container" page, like a TabbedPage, you would have to see what page the container page is actually displaying. For a TabbedPage you would have to check the currently selected tab and get the page that is being displayed there, and then if this is a NavigationPage the you have to, again, check the stack for the last page.

UWP C#: Returning to ContentDialog from Page

I have navigated from a ContentDialog to a Page in my UWP app written in C#. Now I want to return back to the ContentDialog when I click a button on the Page.
I tried writing (Window.Current.Content as Frame)?.GoBack(); but that only returned to my MainPage (from where my ContentDialog was launched).
If it is possible to perform such an action, what is the correct syntax?
No, you can not navigate to or even from content dialog. What you do is that you are navigating between the pages. Action that you call in your dialog does invoke this navigation i.e. there would be no difference if it was done with button or button in content dialog - these are just controls on your page.
If you want to see the dialog on MainPage when you navigate back, implement the logic to show it again if certain conditions are met (for example pass NavigationEventArgs containing details whether Content Dialog should be re-opened).

Button onClick event nothing is happening

I have a problem where my button click will not work whatsoever. I think all the code is right but why is nothing happening when it's clicked? Is the code right for displaying the message and is the code right for redirecting to a website/webpage? Any help much appreciated!
public delegate void myDelegate();
public event myDelegate FindInfo;
protected void btnOne_Click(object sender, EventArgs e)
{
FindInfo += new myDelegate(showFindInfoMessage);
FindInfo += new myDelegate(showWebsite);
FindInfo();
}
public void showFindInfoMessage()
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('You will now be redirected to the website!');</script>");
}
public void showWebsite()
{
string web = "https://facebook.com/";
Response.Redirect(web);
}
HTML
<asp:Button ID="btnOne" runat="server" Text="Find Info" OnClick="btnOne_Click" />
Your code looks a little strange, but it should work as you expect. I don't see anything that could immediatelly stop it from working.
Easiest things first - are there any exceptions thrown? Run the site from within VisualStudio in Debug mode, click the button on the site, and check out the "Output" window. If you see any "First Chance Exception" lines showing up after you clicked it, then try to chase and solve them, or write them down and show them to us. Make sure that those were really recorded only after you clicked the button. There might be some exceptions recorded when the site starts up. After starting up the site, be sure that the Output window stops chattering before you click the button.
Next, does any other buttons you create work as expected at all? On this page? How's that on other pages? Have you tried placing a breakpoint to see if this code runs at all? Place a breakpoint on the first line of btnOne_Click method (click there and press F9, assuming you have typical hotkeys), and also try placing some other breakpoints in other methods, like Page's constructor. Then try running your site in "debug" mode and click on the button. Check which breakpoints get triggered.
In case NO breakpoint gets hit at all, then either your VisualStudio and webserver are not configured for debugging (oops), or the page might not be compiled at all (for example, you may be running an old copy of it).
If some breakpoints are triggered but that one button handler is not, then try observing what your page does or tries to do when you click that button. If you are using any modern browser, it should have a "developer tools" panel somewhere. For example, in Chrome, press F12. In those dev-tools, find some tab or window that will show you the "Network operations". Display or refresh your page, check if some netwrok operations are captured. You should notice at least the page itself being downloaded with "200" status code. After observing that, when everything gets silent, "Clear" the log (there should be a button for that somewhere) so you get a fresh&clean view again, and press the button. Observe what happens.
If the button works on client-side, you will see a request being sent to the server. Observe the response code. Is it 200? 404? 500? All of those will mean different things. Report back with the code. Also, if there are any error (status code != 200) try to read/copy all of the details you can.
If the button and the server work ok or almost ok, then after clicking the button you would see a request sent back to the same page, followed by a status=200. However if IIS and ASPX are OK, this would run the xxx_Click handler. So probably you won't see this scenario.
If the button doesn't work at all and no requests are sent, then there's some problem with the HTML code that was rendered. Go back to the dev-tools in the browser, find a tab/window that allows you to see the raw HTML code of the site, and find that button. Check what click actions are set on it. If you don't see any, check which FORM tag is the parent of that button and check if its "method" and "action" attributes are set properly. If the method/action are generated wrong, the browser may try to send the 'click' to a wrong place, or even it may not try sending it at all.
If all of that does not show anything useful, then try ... temporarily replacing your code with something that should work in any case. Check out the example at MSDN site. Backup your page code somewhere and replace it with the example from this site. Paste all of that as the ASPX file, and remove everything from the code behind (ASPX.CS file), and run your site. This example shows a simple button and attaches one handler to that button. Check if it works. It should. Also you may observe all of the things I wrote above again. If the example does not work, then, again, I'd guess your IIS or ASP installation(s) are broken.
Your code works correctly. I put it in a test project and it redirects. If you are asking why you do not see the alert, that is because the page would need to post back to register the script, however you redirect to facebook.com before it has a chance to do so. You can register the script on page load, then call the JavaScript function to display the alert.
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
showFindInfoMessage();
}
public delegate void myDelegate();
public event myDelegate FindInfo;
protected void btnOne_Click(object sender, EventArgs e)
{
FindInfo += new myDelegate(showFindInfoMessage);
FindInfo += new myDelegate(showWebsite);
FindInfo();
}
public void showFindInfoMessage()
{
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "scriptkey", "alert('You will now be redirected to the website!')");
}
public void showWebsite()
{
string web = "https://facebook.com/";
Response.Redirect(web);
}
}

QR code control working all the time

I'm using JeffWilcox QR control (QR Scanner), but I have one issue with this control. When I launched a page with this control, it's working all the time, even I click back button and I am in main menu. Do you know how can I handle situation like: if user click back button the QR Scanner control will stop working ?
Working:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
nameOfControl.StopScanning();
base.OnNavigatingFrom(e);
}
When you're navigating through pages, there are some events triggered when you enter or exit a page, such as OnNavigatedFrom (when you left the page) or OnNavigatingFrom (just before leaving the page) or OnNavigatedTo (when you entered a page).
You can find more information about page navigation events in the documentation.
You can take advantage of these events to solve your issue. As it seems, create a handler for the OnNavigatingFrom event in the page where you have the QR control, to stop it.

Categories