如何读取xamarin.forms中的图像源或图像名称?

Sag*_*til 3 c# xamarin.ios xamarin.android xamarin xamarin.forms

我正在处理 Xamarin 表单,我在图像内部使用了 TapGestureRecognizer,现在点击该图像我需要在 c# 中获取图像的源。我怎样才能得到它?我这样做的原因是,单选按钮在 Xamarin 表单中不可用,点击图像时,我将检查图像的来源,如果源已选中图像,那么我需要将源更改为未选中,反之亦然。

这是我的 XAML 代码

   <Image  Scale="0.7"  HorizontalOptions="Start" x:Name="radioButton" Source="unchecked.png">
       <Image.GestureRecognizers>
           <TapGestureRecognizer Tapped="radioButton_Clicked"> 
           </TapGestureRecognizer>
       </Image.GestureRecognizers>
   </Image>
Run Code Online (Sandbox Code Playgroud)

这是我的 C# 代码

    private void radioButton_Clicked(object sender, EventArgs e)
    {
        var imageSource = "";//get image source here
        if (imageSource == "Checked.png")
        {
            radioButton.Source = "Unchecked.png";
        }
        else
        {
            radioButton.Source = "Checked.png";
        }
    }
Run Code Online (Sandbox Code Playgroud)

Pra*_*nth 5

您可以在 radioButton_Clicked 操作上实现这样的效果

 private void radioButton_Clicked(object sender, EventArgs e)
{
    var imageSource = (Image)sender;//get image source here
    var selectedImage = imageSource.Source as FileImageSource;

    if (selectedImage.File == "Checked.png")
    {
        radioButton.Source = "Unchecked.png";
    }
    else
    {
        radioButton.Source = "Checked.png";
    }
}
Run Code Online (Sandbox Code Playgroud)

或者您可以使用自定义插件来支持复选框,请参阅此 https://github.com/XLabs/Xamarin-Forms-Labs