微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin.MacOS的图像按钮

如何解决Xamarin.MacOS的图像按钮

Xamarin.MacOS中是否有图像按钮渲染器? 我正在使用Xamarin.Forms,并且Xamarin.MacOS中根本不显示通用代码中的所有图像按钮。 Xamarin.MacOS支持图像按钮吗?

解决方法

Xamarin.MacOS中是否有图像按钮渲染器?

很遗憾,在检查了 Xamarin.Forms.Platform.macOS 的源代码之后,现在没有ImageButtonRenderer。我猜没有控制的原因与ImageButton类似。我不确定设计者将来是否会添加。无论如何,现在不存在。

Xamarin.MacOS是否支持图像按钮?

如上所述, MacOS 没有像ImageButton那样的Forms控件。因此,如果在Xamarin Froms中使用ImageButton,它将不会在Xamarin.MacOS中显示。

但是,有一种解决方法。您可以在Forms中使用Button,并在Xamarin.MacOS的ButtonRenderer中添加Image。因为NSButton有一个Image property

例如,ButtonRenderer的代码如下:

[assembly: ExportRenderer (typeof(MyButton),typeof(MyButtonRenderer))]
namespace CustomRenderer.MacOS
{
    public class MyButtonRenderer : ButtonRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged (e);

            if (Control != null) {
                // do whatever you want to the UITextField here!
                Control.Image= NSImage.ImageNamed("tree")
                Control.ImageScaling = NSImageScale.AxesIndependently;
            }
        }
    }
}

图像源(tree.png)与iOS相同,可以添加到资源文件夹中,并设置 Propertied:构建操作-> BundleResource

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。