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

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.

Related

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).

Why does my modal page have a back button?

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
}

How to load a WPF window with values last entered?

I have 2 WPF windows. Both the windows have textboxes and combo boxes. Upon entering data into Window1, the user presses a "Next" button and Window2 is loaded. Window2 has a "Back" button which will reload Window1 incase the user wants to change some values. Since it is the same session, I want the last entered values in window1 to appear when the "Back" button in Window2 is pressed. How should I go about doing it in C# or XAML?
you have to take one property in window 1 like
public string ChangeValue { get; set; }
when you press back buttion at that time you can set the property value using instance of window 1
instanceofWindow1.ChangeValue="Value";
Each window should have its own ViewModel. These contains the values you type in the windows. If you click next (and destroy the first window) the you should implement some kind of save method (ICommand that is invoked when clicking on Next) that saves the current state of the ViewModel to a model class (or a database or a text file ...). When you click back you have to reload the model, connect it with the ViewModel and show the Window1.
It seams you are not familiar with WPF, maybe the links from another answer might help you to get started: https://stackoverflow.com/a/2034333/1015350
Furthermore you should get into the topics:
MVVM
ICommands
Databinding
Rule of thumb: Your code behind file should contain nothing. This is all done by databinding.

How to programmatically reload ASP.NET page as new session, as if first access to the page

I have an ASP.NET web app that uses a single page, but makes some controls invisible and others visible throughout the workflow. It's a fairly simple application, which is why it just uses one page.
I'm not sure though how to reload the page when a user clicks a cancel button, or when they complete the workflow. Here's some details about the application workflow...
The page loads and displays a LoginTable control with a dropdown list with employee names, and a pinpad where a pin number is entered for the selected employee, and a login button.
On clicking the login button, a postback occurs where the pin is validated, and if valid, the Table control that holds most of the page contents (except the header) gets made invisible, and a new Table control gets made visible depending on the current status (clocked in or out).
At this point, either the ClockInTable or ClockOutTable control are visible, and the LoginTable control is invisible. The user can either click a Cancel button, or a Clock In / Out button (with some other things to select if clocking in, like work site and job).
What I want is, when the user either clicks the Cancel, Clock In or Clock Out buttons (after any other processing on postback), for the page to load again with a new session and showing the main LoginTable again just as if the user had restarted the browser and opened the page for the first time.
I've tried using Session.Abandon(), and then in the Global.asax Session_End event, I do
Response.Redirect("Login.aspx");
This doesn't work though; it still just reloads the same view of the page.
How do you reload the page (not refresh the page) as if it were the first access (i.e. not a postback)?
Also, I'm new to ASP.NET, so if I'm misunderstanding how a session works, please let me know.
Have you tried Response.Redirect()-ing the user to the same page? That should load the page as if it were a new one. It won't lose the session data though.
I'm not exactly sure you can do that the way you describe. However, what you can do is have a flag as a session variable. Call it say IsFirstComer. I'm not sure how the workflow of your system goes, but you can have it reset to true every time a new user logs in (hasn't started a workflow yet), or has exited a workflow (needs to start all over again), but as soon as te user has started the workflow you set it to false. Your controls behaviour can then be controlled through the value of IsFirstComer.
Have you write: if(!this.IsPostback) in the Page_Load method, after that try "Response.Redirect(page.aspx)" again.

How do I make a custom modal popup window in WPF and have it return a value?

I'm taking a whack at WPF and trying to learn as I go. I'd appreciate any advice offered.
I've got a Window that has a Page attached to it (through a Frame on the Window). When you press a button on the Page, I want a custom window to pop up to present several custom options and be displayed in a manner of my choosing (I'm thinking right now I want it to be a grid but that may change as I go on). When selected, the modal window will disappear and return to the calling method (button press from the Page) the value of the selected choice.
I don't want the standard windows dialog box with the options of yes, no, okay, cancel, or anything like that. This is truly just a custom popup that returns a value to the caller when the user makes their selection on the popup.
Create a new Window subclass, which you can layout however you like. Then in your button click event handler, display it modally using myModalWindow.ShowDialog();. You can then have a property on the window class which you can access after it closes in order to access result data, i.e.:
myModalWindow.ShowDialog();
var data = myModalWindow.SomeResultProperty;
If you really want to have something returned from a method, I suppose you could create your own public method on your window class which internally calls ShowDialog() and then returns a value.

Categories