about how xamarin.forms is displayactionsheet in the context of MVVM

Source: Internet
Author: User

I've been working on Xmarin.forms recently, and I've come across a tricky question about how to use Displayactionsheet in ViewModel in the context of MVVM, but I'm using XAML mode, which means that only in a background page can you use the page's exclusive disp Layactionsheet. Looking for an afternoon information, online said, can give each other a bridge, so that ViewModel and page connected together.

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 (Val UEs. Title, values. Buttonone, values. Buttontwo, values.                Displayvalues);  if  (values.                    OnCompleted! = null  
Baseviewmodel this place, of course, you write the ViewModel or application you belong to.
In Baseviewmodel we need to configure the bridge so that our ViewModel can connect to the page
 Public Async Task Displayactionsheet (Displayactionsheetmodel actionsheetmodel)        {            messagingcenter.send<baseviewmodel, displayactionsheetmodel> (this"  Displayactionsheet", Actionsheetmodel);        }

Then we can use it again in ViewModel.

 varActionsheetmodel =NewDisplayactionsheetmodel (); Actionsheetmodel.title="Is you sure?"; Actionsheetmodel.buttonone="Cancel"; Actionsheetmodel.displayvalues=New string[]            {                "Yes","No","I donw ' t know","I ' m sure!","..."            }; Actionsheetmodel.oncompleted+ = (accept) = ={disdata=Accept;            }; awaitDisplayactionsheet (Actionsheetmodel);

With a construction class you can easily pass the data to the page, then the problem

We can now pass the required properties in Displayactionsheet to the page using ViewModel, so how do we get the data from the page back to ViewModel?

Presumably everyone saw the oncompleted, so you can look at my model

  Public classDisplayactionsheetmodel { Public stringTitle {Get;Set; }  Public stringButtonone {Get;Set; }  Public stringButtontwo {Get;Set; }  Public string[] Displayvalues {Get;Set; }  Publicaction<string> oncompleted {Get;Set; } }

So that after you choose it will give you the choice of things to the value of Action<string>, and then we use

OnCompleted + = (accept) =            {                disdata = accept;            };
This way, assign the value of the selection to Disdata next look

Of course, I'm clicking on the second button before it pops up my selected I ' m sure

Private Async Task distest ()        {            await DisplayAlert ("Test", Disdata);        }

So there is a problem, what if I want to do something about the selected value after I choose?

I tried a lot of ways, like Task.waitall (), and the only thing that would play a role is to await task.delay (1000); Let the next operation wait 1 seconds, if I choose fast, no problem, but if I choose the slow, still not, the symptoms do not cure, so , looked for an afternoon information also did not find the right solution, please friends, do you have a good solution?

If there is, please help the younger brother pointing to the maze, younger brother grateful ...

about how xamarin.forms is displayactionsheet in the context of MVVM

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.