在 Xamarin 中为 Imagebutton 设置图像 iOS

Set image for Imagebutton in Xamarin for iOS

我正在尝试自定义图像按钮以在特定设备上使用特定图像,因为 android 将显示一张图像,而在 iOS 中它将使用自定义渲染器显示另一张图像

我已经完成了 Android 方面的工作,但注意到 iOS 使用了不同的成员变量,而不仅仅是 android 方面使用的 "this.SetBackground(Some Resource)"。

namespace FlipXamarin.Controls
{
    public class FingerPrintLabel : ImageButton
    {
        public FingerPrintLabel()
        {
        }
    }
}
Android
[assembly: ExportRenderer(typeof(FingerPrintLabel), typeof(FingerPrintLabelRenderer))]
namespace FlipXamarin.Droid.Renderers
{

    public class FingerPrintLabelRenderer : ImageButtonRenderer
    {

        public FingerPrintLabelRenderer(Context context) : base(context) 
        {

        }


        protected override void OnElementChanged(ElementChangedEventArgs<ImageButton> e)
        {
            base.OnElementChanged(e);

            this.SetBackground(Resources.GetDrawable(Resource.Drawable.fingerprint_2x));
        }
    }
}
iOS
[assembly: ExportRenderer(typeof(FingerPrintLabel), typeof(FingerPrintLabelRenderer))]
namespace FlipXamarin.iOS.CustomRenderers
{
    public class FingerPrintLabelRenderer : ImageButtonRenderer
    {

        protected override void OnElementChanged(ElementChangedEventArgs<ImageButton> e)
        {
            base.OnElementChanged(e);

            ///if(iOS.version == 8) show fingerprint icon
            ///if(iOS.version == 10 and) show FaceId icon
        }
    }
}

Android 应该只显示指纹图标,而 iOS 会显示指纹或 faceId 图标。

Android Screen

正如@Jason 在评论中指出的那样,您不需要自定义渲染器来显示不同的图像,因为您可以使用 Device 帮助程序 class 从共享项目中完成所有这些操作.

但是,为了回答您的问题,如果您想从 iOS 中的 CustomRenderer 更新 ImageButton 的图像,您可以这样做:

//Do your validation as you wish, this was just an example.
var image = UIDevice.CurrentDevice.CheckSystemVersion(10, 0)
                ? UIImage.FromBundle("facescan.jpg")
                : UIImage.FromBundle("fingerprint.jpg");

//The Control property is an UIButtton
Control?.SetImage(image?.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIControlState.Normal);

在 Xamarin Forms 中,您可以安装 Xam.Plugin.DeviceInfo 来确定哪个版本号。

Xaml 如下:

<ImageButton x:Name="MyImageButton" Source="fingerprint.png"/>

然后在ContentPage中,如果已经安装了DeviceInfo Nuget,可以使用如下:

using System.ComponentModel;

if (Device.RuntimePlatform == Device.iOS)
{        
    Console.WriteLine("version is -- " + CrossDeviceInfo.Current.Version);
    if (Convert.ToInt32(CrossDeviceInfo.Current.Version) >= 10)
    {
        MyImageButton.Source = "FaceId.png";
    }
    else
    {
        MyImageButton.Source = "fingerprint.png";
    }
}
else if (Device.RuntimePlatform == Device.Android)
{
    MyImageButton.Source = "fingerprint.png";
}

如果不知道安装Nuget包,可以参考这个Install and use a package in Visual Studio

您可以简单地使用 XAML MArkup OnPlatform。

这是一个示例:

  <ImageButton HorizontalOptions="Center"
               VerticalOptions="Center">
       <ImageButton.Source>
           <OnPlatform x:TypeArguments="ImageSource"
                                        iOS="https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201606271147"
                                        Android="https://d3nevzfk7ii3be.cloudfront.net/igi/hAgr2LwD6AECIwsh.large" />
       </ImageButton.Source>
   </ImageButton>

这导致: