简体   繁体   English

MVVM Xamarin Forms - 绑定视图模型属性的命令参数

[英]MVVM Xamarin Forms - Command Pararameter to Bind View Model property

I have the following image with an tap gesture recognizer that calls an command from the View Model:我有以下图像,其中带有从视图模型调用命令的点击手势识别器:

<Image IsVisible="False" Aspect="Fill" Grid.Column="1" HorizontalOptions="Start" VerticalOptions="Center" Source="po.png" x:Name="img2">
    <Image.GestureRecognizers>
        <TapGestureRecognizer x:Name="gRcgImg2" Command="{Binding FalaCommand}" CommandParameter="po" />
    </Image.GestureRecognizers>
</Image>

I need to change that command parameter when another image is clicked, and I need to do it all by the view model, could you provide me ways to do that?当单击另一个图像时,我需要更改该命令参数,并且我需要通过视图模型来完成这一切,你能提供我这样做的方法吗?

The best way is bind some value that has this information.最好的方法是绑定一些具有此信息的值。 For example, you can pass Image.Source and add correct behavior inside your code.例如,您可以传递 Image.Source 并在代码中添加正确的行为。 For Example:例如:

CommandParameter="{Binding Image.Source RelativeSource={RelativeSource AncestorType=Image}}"

with this you can filter and detect what image is sending the tap command.有了这个,您可以过滤和检测发送点击命令的图像。

[edited] here, how you can change Image.Source from command: [编辑] 此处,如何从命令更改Image.Source

public ICommand MyCommand => new Command((ImageSource) obj) =>
{
    obj = ImageSource.FromFile(filename);
});

Maybe you'll need refresh image, I'm not sure.也许你需要刷新图像,我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM