About how xamarin. forms DisplayActionSheet, xamarin. formsmvvm

Source: Internet
Author: User

About how xamarin. forms DisplayActionSheet, xamarin. formsmvvm

Xmarin. forms has encountered a tricky problem, that is, how to use DisplayActionSheet in ViewModel in MVVM, but I use the XAML mode, that is, only on the background page, only the DisplayActionSheet exclusive to Page can be used. I found the materials for one afternoon and said on the Internet that I could build a bridge for each other to connect the ViewModel and Page.

First, you need to use MessagingCenter to write a DisplayActionSheet Method on your Page.

  

MessagingCenter.Subscribe <BaseViewModel, DisplayActionSheetModel> (this, "DisplayActionSheet", async (sender, values) =>

             {

                 string result = String.Empty;

                 result = await DisplayActionSheet (values.Title, values.ButtonOne, values.ButtonTwo, values.DisplayValues);

                 if (values.OnCompleted! = null)

                 {

                     values.OnCompleted (result);

                 }

             });

BaseViewModel Of course you can write your own ViewModel or Application

In BaseViewModel we need to configure the bridge so that our ViewModel connects to Page

  public async Task DisplayActionSheet (DisplayActionSheetModel actionSheetModel)

         {

             MessagingCenter.Send <BaseViewModel, DisplayActionSheetModel> (this, "DisplayActionSheet", actionSheetModel);

         }

Then we can use it in ViewModel.

 var actionSheetModel = new DisplayActionSheetModel();
            actionSheetModel.Title = "Are you Sure?";
            actionSheetModel.ButtonOne = "Cancel";
            actionSheetModel.DisplayValues = new string[]
            {
                "Yes","No","I donw't know","I'm sure!","..."
            };
            actionSheetModel.OnCompleted += (accept) =>
            {
                DisData = accept;
            };
            await DisplayActionSheet(actionSheetModel);

Using a constructor class, you can easily pass data to the Page. The problem arises.

Now we can use ViewModel to pass the attributes required by DisplayActionSheet to the Page. How can we get the data in the Page back to the ViewModel?

You may have seen OnCompleted, so you can take a look at my Model.

 public class DisplayActionSheetModel
    {
        public string Title { get; set; }
        public string ButtonOne { get; set; }
        public string ButtonTwo { get; set; }
        public string[] DisplayValues { get; set; }
        public Action<string> OnCompleted { get; set; }
    }

After you select this option, it will assign the selected item to Action <string>. Then we use

OnCompleted + = (accept) =>{ DisData = accept ;};
In this way, assign the selected value to DisData. Next, let's take a look.



Of course, after I click the second button, the selected I'm sure will pop up.

 

 private async Task DisTest()
        {
            await DisplayAlert("Test", DisData);
        }

Then there will be a problem. What if I want to perform corresponding operations on the selected value after the selection?

I tried many methods, such as Task. waitAll (); the only thing that will play a role is await Task. delay (1000); let the next operation wait for 1 second. If my selection is fast and there is no problem, but if my selection is slow, it will still not work, and it will not cure the problem, I did not find a proper solution after finding the information for one afternoon. Do you have a good solution?

If you have any, please give me some advice. Thank you...


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.