【问题标题】:Set image for Imagebutton in Xamarin for iOS在 Xamarin for iOS 中为 Imagebutton 设置图像
【发布时间】:2019-09-02 21:44:07
【问题描述】:

我正在尝试自定义一个 imagebutton 以在特定设备上使用特定图像,对于 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

【问题讨论】:

  • 真的有必要使用自定义渲染器吗? Device helper 类将让您在运行时确定平台和版本,因此您可以在共享表单代码中设置适当的图像。

标签: c# ios xamarin xamarin.forms imagebutton


【解决方案1】:

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

但是,为了回答您的问题,如果您想在 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);

【讨论】:

    【解决方案2】:

    在 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

    【讨论】:

    • 我不知道你可以从表单项目中引用文件。
    • @AnthonyR Okey,看看这个。(docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…)我觉得这里使用Custom Renderer并没有让程序更简洁。如果使用Custom Renderer,你需要添加一个Renderer文件iOS 和 Android 中的每一个,Forms 只需要在各自的平台上添加图像。当Forms无法实现时使用Custom Renderer,这样可以让代码尽可能的简单方便。
    【解决方案3】:

    您可以简单地使用 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>
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多