猿问

如何在 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>

这是我的 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";

        }

    }


哆啦的时光机
浏览 83回答 2
2回答

叮当猫咪

你可以在 radioButton_Clicked 动作上实现这样&nbsp;private void radioButton_Clicked(object sender, EventArgs e){&nbsp; &nbsp; var imageSource = (Image)sender;//get image source here&nbsp; &nbsp; var selectedImage = imageSource.Source as FileImageSource;&nbsp; &nbsp; if (selectedImage.File == "Checked.png")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; radioButton.Source = "Unchecked.png";&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; radioButton.Source = "Checked.png";&nbsp; &nbsp; }}或者您可以使用自定义插件支持复选框参考此&nbsp;https://github.com/XLabs/Xamarin-Forms-Labs

临摹微笑

您可以将发件人投射到图像控制var&nbsp;imageSender&nbsp;=&nbsp;(Image)sender;手势
随时随地看视频慕课网APP
我要回答