Xamarin.Forms button in ListView to return to the previous page with parameters

Source: Internet
Author: User

Today, when I do a list display, I get the problem of how can a button's buttons work and contain parameters in the ListView?

In fact, it's a bit like the control in rep can't get it. In Xamarin, when you button the Bind event is not included in the data source of the ListView, then the event of the button is invalid.

So what's the solution? Looking for an afternoon and finally found a solution

Xaml:

<AbsolutelayoutIsVisible= "True"horizontaloptions= "Endandexpand">                      <Renderers:cicobuttonx:name= "ButtonName"Borderradius= "2"BackgroundColor="{StaticResource Color}"heightrequest= "The "widthrequest= "The "Absolutelayout.layoutflags= "Positionproportional"Absolutelayout.layoutbounds= ". 5,.5,-1,-1"TextColor= "White"Command="{Binding path=bindingcontext.functionname, source={x:reference name=listviewname}}"CommandParameter="{Binding Id}" />                      <ImageSource= "Ic_add_white"Absolutelayout.layoutflags= "Positionproportional"Absolutelayout.layoutbounds= ". 5,.5,-1,-1"/>                    </Absolutelayout>

The key to the problem is how the button saves the data and how to find the command method in the ViewModel

On the internet to find such a binding to the wording "

{Binding path=bindingcontext.functionname, source={x:reference Name=listviewname}}

Path refers to your method name, BindingContext the equivalent of your context, and then uses it to point out the method name in your ViewModel, followed by where to bind the data, so name, rightfully, is the name in your ListView.

ViewModel:

Private ICommand _backcommand;  Public ICommand Backcommand        {            get            {                               return _backcommand?? (_backcommand = new Command<string> (async (id) = {await BackPage (ID);}));            }        }  Private Async Task BackPage (string id)        {                      var page = Navigation.NavigationStack.FirstOrDefault (p = p is Outpag e);            var vm = page. BindingContext as Outviewmodel;            if (VM! = null)            {                                //The thing you want to do            }            else            {                "Can ' t find". Totoast ();            }            Await Navigation.popasync (true);        }

So, the Command method in ViewModel is going to be written like this, new command<string> (async (id) = {await backexamstaffpage (ID);} This corresponds to the commandparamete of the button in the page.

Navigation.NavigationStack.FirstOrDefault (P = p is outpage); from all the open pages, find the previous page you want to return (if you don't need parameters, just pop up)

Page. BindingContext as Outviewmodel; Use BindingContext to convert to the ViewModel you need, then, and then, what do you want to do?

Xamarin.Forms button in ListView to return to the previous page with parameters

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.