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