Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为iOS版Xamarin中的Imagebutton设置图像_C#_Ios_Xamarin_Xamarin.forms_Imagebutton - Fatal编程技术网

C# 为iOS版Xamarin中的Imagebutton设置图像

C# 为iOS版Xamarin中的Imagebutton设置图像,c#,ios,xamarin,xamarin.forms,imagebutton,C#,Ios,Xamarin,Xamarin.forms,Imagebutton,我正在尝试自定义一个imagebutton,以便在特定设备上使用特定的图像。android将显示一个图像,而在iOS中,它将使用自定义渲染器显示另一个图像 我已经完成了Android方面的工作,但注意到iOS使用了不同的memeber变量,而不仅仅是Android方面使用的“this.SetBackground(一些资源)” namespace FlipXamarin.Controls { public class FingerPrintLabel : ImageButton {

我正在尝试自定义一个imagebutton,以便在特定设备上使用特定的图像。android将显示一个图像,而在iOS中,它将使用自定义渲染器显示另一个图像

我已经完成了Android方面的工作,但注意到iOS使用了不同的memeber变量,而不仅仅是Android方面使用的“this.SetBackground(一些资源)”

namespace FlipXamarin.Controls
{
    public class FingerPrintLabel : ImageButton
    {
        public FingerPrintLabel()
        {
        }
    }
}
Android
[程序集:ExportRenderer(typeof(FingerPrintLabel)、typeof(FingerPrintLabelRenderer))]
命名空间FlipXamarin.Droid.Renderers
{
公共类FingerPrintLabelRenderer:ImageButtonRenderer
{
公共指纹LabelRenderer(上下文):基本(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
this.SetBackground(Resources.GetDrawable(Resource.Drawable.fingerprint_2x));
}
}
}
iOS
[程序集:ExportRenderer(typeof(FingerPrintLabel)、typeof(FingerPrintLabelRenderer))]
命名空间FlipXamarin.iOS.CustomRenders
{
公共类FingerPrintLabelRenderer:ImageButtonRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
///如果(iOS.version==8)显示指纹图标
///如果(iOS.version==10和)显示FaceId图标
}
}
}
Android应该只显示指纹图标,而iOS将显示指纹或faceId图标


正如@Jason在评论中指出的,您不需要自定义渲染器来显示不同的图像,因为您可以使用
设备
助手类从共享项目中执行所有这些操作

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

正如@Jason在评论中所指出的,您不需要自定义渲染器来显示不同的图像,因为您可以使用
设备
助手类从共享项目中执行所有这些操作

但是,为了回答您的问题,如果您想在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表单中,您可以安装以确定哪个版本号

Xaml如下所示:

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

如果不知道如何安装Nuget软件包,可以参考此信息。

在Xamarin表单中,可以安装以确定哪个版本号

Xaml如下所示:

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

如果不知道如何安装Nuget软件包,您可以参考此信息。

您只需在平台上使用XAML标记即可

以下是一个示例:

  <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>

其结果是:


您只需在平台上使用XAML标记即可

以下是一个示例:

  <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>

其结果是:


是否真的需要使用自定义渲染器?Device helper类将让您在运行时确定平台和版本,因此您可以从共享表单代码中设置适当的映像。是否确实需要使用自定义呈现程序?Device helper类将让您在运行时确定平台和版本,因此您可以从共享表单代码中设置适当的映像。我不知道您可以引用表单项目中的文件。@Anthony好的,看看这个。()我认为在这里使用自定义渲染器并不能使程序更加简洁。如果使用自定义渲染器,则需要在iOS和Android中分别添加一个渲染器文件,窗体只需要在各自的平台上添加图像。自定义渲染器是在表单无法实现时使用的,因此您可以使代码尽可能简单方便。我不知道您可以引用表单项目中的文件。@Anthony好的,看看这个。()我认为在这里使用自定义渲染器并不能使程序更简洁。如果您使用自定义渲染器,您需要向iOS和Android中的每一个添加一个渲染器文件,表单只需要向各自的平台添加图像。自定义呈现程序在表单无法实现时使用,因此您可以使代码尽可能简单方便。