Await, not awaiting!

HaywardIHaywardI Member ✭✭

Hi,

Some advise appreciated.

I have a form that I want to use for multiple purposes. i.e. List what has been ordered and amend what has been ordered.

I can display the form complete with all the items ordered without a problem. For this I do not want the item clicked to do anything as all I am doing is displaying

In my simple view (as the form was identical) I thought I would use a variable to control the item clicked event so I set a variable that I would check in the item clicked event, if it was true then I would have a little piece of code that would allow the modification, if it was false, it would ignore it as though Item clicked did nothing.

In page A, if edit button clicked I would set the variable to True await the navigation to B page and when it returned I would reset the variable to false. Seemed simple enough.

However, it didn't work. What I discovered was that when I awaited the page, it would run, display page B and then return execution to page A which would then reset the variable to false! In other word it didn't await until page B closed which was what I was expecting.

Now I could just have two pages, one for display and one of amendments, but that seems cumbersome as 95% of the code is identical.

Any suggestion for how I can reuse the same page/code?

Tagged:

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @HaywardI said:
    In page A, if edit button clicked I would set the variable to True await the navigation to B page and when it returned I would reset the variable to false. Seemed simple enough.

    However, it didn't work. What I discovered was that when I awaited the page, it would run, display page B and then return execution to page A which would then reset the variable to false! In other word it didn't await until page B closed which was what I was expecting.

    As you've discovered, await Navigation.PushAsync does not wait until a page has been displayed and subsequently closed.

    As per the documentation at https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.navigationpage.pushasync?view=xamarin-forms, PushAsync returns "A task for asynchronously pushing a page onto the navigation stack, with optional animation". So, when you await PushAsync, you wait for the page to be pushed onto the navigation stack. That's it.

    If you want to know when the child page is closed, you can use the OnAppearing() override in the parent page. Or you could use MessagingCenter, with the child page sending a message when it is closing. Or you could use an event. Or you could use MVVM and have both pages bind to ViewModels, so that you don't actually need to know when the child page closes - the child page updates the ViewModel as the user changes data, the parent page gets automatically updated with changes notified from the ViewModel.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'm just going to say it...

    Read the documentation. I can only imagine the number of hours wasted by jumping in and assuming you somehow instinctively knew what would happen... then you had to experiment... Eventually you "discovered" (your words) how it really worked. 5 minutes reading the documentation could have cleared it right up. Or... here's a thought... Don't try to architect a solution in an eco-system you haven't first read about and worked through the learning materials. 5 hours doing some lessons would have saved you (conservative guessing) 50 hours of head scratching.

    Clearly you've launched Visual Studio then just jumped in thinking "Oh, I'll figure it out as I go along". That's not a good approach.

    Start with working the free textbook from cover to cover.
    https://blogs.msdn.microsoft.com/microsoft_press/2016/03/31/free-ebook-creating-mobile-apps-with-xamarin-forms/

    Feel free to hit up on-line tutorials sites. There are plenty. Here's mine:

    When you say

    Its my first time using Xamarin.

    If you really mean

    Its my first time doing any coding at all

    Then stop and don't get into Xamarin at all yet. First you need to learn C# which is the language all your logic and actual 'code' will be written in. Xamarin is an eco-system that your code operates within so that the same code can run on iOS, Android, UWP, MacOS and Tizen without having to create separate solutions for each platform. But if you can't first write code - any code - then it does you no good to try to figure out cross-platform deployment.

    Once you are at least somewhat comfortable in C# then look into MVVM design. It is the foundation concept that any app in the last 15 years should be based on, whether that's a Windows WPF program or a Xamarin cross-platform app. If you have no experience with MVVM design... no experience with databinding code to UI... etc. etc. then you need to first get a handle on that.

    Then realize you can't just jump into make your "goal application" as your first project. Your first application can't be an entire inventory and ordering system for your uncle's restaurant, or scheduling app for your sisters delivery service.

    You have to start with building a bird house... then a dog house... then an outhouse... shed... garage... Before you try to build a 3 story apartment building. You have to apply that same "start small and learn" approach to software development. You start by building small apps alike alarm clocks and mortgage calculators, and work up from there learning UI development, OOP and other basics along the way. Then you take those skills to your next bigger project. In other words you have to learn how to be a coder before you can be a software architect.

    When you're ready to start designing an intermediately complex app just tackle one issue at a time in a "Sandbox" or R&D app.
    Start with the first basic need: People
    That's not Xamarin in any way. Just define a Person class. And then inherit from that for a User class. And you'll want a Rights class to be the permissions for that User object.

    Then probably some type of grouping... whether its groups of people or groups of things. Learn how to work with collections of objects.

    But learn each of your bullet points one at a time in a little R&D app before you try to take on putting them all together. You need to understand them as a single thing before you can architect how they will interact.

    In the end... 90% of of the typical app has nothing to do with Xamarin. Its all basic-to-intermediate C#, application architecture and design that would be the same in Xamarin, WinForms, WPF, Android, ...

  • HaywardIHaywardI Member ✭✭

    Hi John,

    Thank you. I have been looking up OnAppearing() and have also found OnDisappearing() which, based on it's name may be better for me as I could reset the variable when the page disappears.

    HOWEVER, I have no idea how to implement, so a pointer to where I can learn for myself would be appreciated.

    Thanks in advance
    Iain

  • HaywardIHaywardI Member ✭✭

    Clint,

    Thank you for the pointers.

    Perhaps I am a little brave, but my learning style is hands on, that's me. But as you've relayed back to other posts I have made recently you'll also see that I have been programming for over 30 years on a variety of different platforms and in a variety of different languages. I learn best when I try to do something, fail and then try to find out why and I appreciate help from forums to help me on my way. I not looking to be spoon fed answers and I have happy to go dig as hard won answers are usually retained.

    This problem, 50 hours? No, try 30 minutes, then realising the limits of my knowledge I seek pointers.

    With regard to the above, actually without Johns further input I have already learned how to implement OnDisappreaing and it did the job I was looking for.

    However, I still appreciate the links you have provided as I am happy to recognise the limits of my knowledge and already recognise that a little more theory will help me. So thank you.

    Iain.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    To be honest, some of your original post contradicts itself so its hard to follow what you're actually trying to accomplish and what you do and don't want.

    In page A, if edit button clicked I would set the variable to True await the navigation to B page and when it returned I would reset the variable to false.

    then

    What I discovered was that when I awaited the page, it would run, display page B and then return execution to page A which would then reset the variable to false!

    So what's the problem? Its automatically doing exactly what you described you wanted: Returning the variable to false.

    Now I could just have two pages, one for display and one of amendments, but that seems cumbersome

    Isn't that what you just described by having page A showing the data and page B for editing the data?

    I don't know why you need two pages at all. If you have a page showing the data then you already have a page on which you can edit/update that same data. Don't you? Put the data in Entry boxes for example... And disable them if the EditData bool is false. In other words just bind the IsEnabled of each control to the CanEdit bool in your ViewModel. They are enable if CanEdit is true. Done. Easy peezy.

    Also Button.Clicked is a UI event, not a logic driving event. You shouldn't be driving the workflow of your app from Button.Click. Buttons have a Command property for driving logic in your ViewModel. Click is handled in the code-behind of the View, where command is handled in the ViewModel. This makes Click suitable for UI joy like flashing colors or playing a click sound; but not for logic and work.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I have been programming for over 30 years on a variety of different platforms and in a variety of different languages.

    Awesome. 20+ years myself.

    Let me ask... Did you ever do Windows WPF development using XAML and MVVM design pattern? Because Xamarin is based upon that. If you already understand the foundation concepts of WPF such as separation of UI and data... data binding through ViewModels... commands and command parameters... etc. Then you have 90%+ of what you need for Xamarin.

    Perhaps I am a little brave, but my learning style is hands on,

    With about 15+ years mentoring/teaching I've hear that a LOT. Though usually from kids that honestly don't know what they're talking about. They really mean to say "I'm too impatient to bother learning first and I want to just jump in and write games."

    When I shifted from WPF to Xamarin I thought

    Wow, I already know XAML and MVVM. This should be an easy transition.

    I was wrong. Even having that experience it was still a big learning curve. Jumping in with both feet and banging on the keyboard is not a good approach for Xamarin even under the best of conditions and experience. If you don't have that MVVM foundation under you then its going to mean a huge amount of wasted time simply because you spend X hours going way off in the wrong direction simply because you don't know how much you don't know.

    Going through Xamarin University saved my arse. Even with 20 years of professional development, half of that in WPF, proper guided learning was a must.

    Sure you can ask questions after wasting some time. But honestly, how much of a tutorial are you going to get on something complex and deep, from withing a forum thread? There is a reason people make entire lesson plans, tutorial sites, hour long video lessons for the concepts that everything else is based on top of. Think of it this way... Would you build your house using the same approach? Just buy a bunch of pipe and lumber and concrete and "figure it out as you go" cutting and banging with a hammer? Would you want that house? Would you want to hire a developer that writes code like that?

    I've said my peace. Just sharing an outlook from one seasoned developer to another.

  • HaywardIHaywardI Member ✭✭

    Clint,

    I agree :-) And no never used xaml before. But I can say I like it, it makes development sooo much easier (reminds me of FMS back in my Dec days) and if you want a laugh at my expense, I've started putting ";" in other code I write in other languages and I keep kicking myself for it!

  • JohnHardmanJohnHardman GBUniversity mod

    @HaywardI said:
    This problem, 50 hours? No, try 30 minutes, then realising the limits of my knowledge I seek pointers.

    If you are asking the person sitting next to you, 30 minutes is fine. However, in a community forum where people are providing their own time to answer questions, you need to try for significantly more than 30 minutes before asking IMHO. Otherwise you are unnecessarily distracting people when you could easily have found the answers online yourself.

  • HaywardIHaywardI Member ✭✭

    John you make a fair point. But I hope you understand I know what I don't know so hitting my head against a brick wall for 50+ hours won't help; therefore all I am looking for is threads when I get really stuck. You also don't see what I do actually work out for myself. 4 weeks ago I had never used Xamarin, never coded in c# (c or C++). Now not only am I gaining confidence in C# (and thinking in c# when I want to do something) I can see the power of xaml and I have built a 'mostly' functional application that interfaces into a backend that I have also built to support the mobile app. I sure anyone who has coded in the environment for years could easily see some of the errors of my ways and improve what I have done, but for someone with 4 weeks part time experience of the environment I think I'm doing really rather well :-)

    I am also deeply respectful of the help I receive and try to minimise any requests I make. In this case, all I needed to know was OnAppearing() existed, I then found OnDisappearing(), I then coded and what I was trying to achieve was successful. I will go though the material Clint referred to as I am now seeing my limits and some more theory will help.

Sign In or Register to comment.