xaml - Binding Command/Attribute To Element in C#? -
i'm trying implement xlabs cameraviewmodel functionality xamarin forms app. unfortunately, given example uses xaml bind views data, need in code behind.
the following code used select picture , it's source.
public class cameraviewmodel : xlabs.forms.mvvm.viewmodel { ... private imagesource _imagesource; private command _selectpicturecommand; public imagesource imagesource { { return _imagesource; } set { setproperty(ref _imagesource, value); } } public command selectpicturecommand { { return _selectpicturecommand ?? (_selectpicturecommand = new command( async () => await selectpicture(),() => true)); } } ... }
and these commands bound xaml :
<button text="select image" command="{binding selectpicturecommand}" /> <image source="{binding imagesource}" verticaloptions="centerandexpand" />
how can apply same commands in code-behind created elements?
cameraviewmodel viewmodel = new cameraviewmodel(); var take_button = new button{ }; take_button.setbindings(button.commandproperty, //*???*//); var source_image = new image { }; source_image.setbinding(image.sourceproperty, //*???*//);
i've binded selectpicturecommand doing following:
take_button .command = viewmodel.selectpicturecommand;
however have doubts being correct way, , same logic cannot applies imagesource.
for button have:
var take_button = new button{ }; take_button.setbinding(button.commandproperty, new binding { path = nameof(viewmodel.selectpicturecommand), mode = bindingmode.twoway, source = viewmodel});
for image have:
var source_image = new image { }; source_image.setbinding(image.sourceproperty, new binding { path = nameof(viewmodel.imagesource), mode = bindingmode.twoway, source = viewmodel });
Comments
Post a Comment