简体   繁体   English

如何使用Xamarin.Forms自定义渲染器在ButtonOnClick()中执行特定于平台(Android)的方法。

[英]How to execute a platform (Android) specific method within a ButtonOnClick() using a Xamarin.Forms custom renderer.

Introduction: 介绍:

I am starting with code from: 我从以下代码开始:

https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/Entry/Android https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/Entry/Android

to study custom renderers. 研究自定义渲染器。 I am doing this because there is code that only executes on the android platform. 我这样做是因为有仅在android平台上执行的代码。 Lets call this "androidMethod()" and belongs in the Android codebase (not the shared library). 让我们将其称为“ androidMethod()”,它属于Android代码库(而不是共享库)。 I have noticed that most of the examples I have found have utilized customRenderers for making platform specific UI modifications (like the example in the link) but I intend to make no changes to the UI, rather I am trying to place a platform specific method in a Xamarin.Forms ButtonOnClick() method as the code below indicates. 我注意到,我发现的大多数示例都使用了customRenderers来进行特定于平台的UI修改(如链接中的示例),但是我打算不对UI进行任何更改,而是尝试在其中放置特定于平台的方法Xamarin.Forms ButtonOnClick()方法,如下面的代码所示。

The code below is similar to the code you will find in the MyEntryRenderer.cs from the link but you will see that it was modified to apply to a button instead of an entry. 下面的代码与您可以从链接的MyEntryRenderer.cs中找到的代码相似,但是您会看到它已被修改以应用于按钮而不是条目。

MyButtonRenderer.cs: MyButtonRenderer.cs:

using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomRenderer;
using CustomRenderer.Android;
using Android.Content;

[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace CustomRenderer.Android
{
    class MyButtonRenderer : ButtonRenderer
    {
        private Button androidButton;

        public MyButtonRenderer(Context context) : base(context)
        {
        }

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

            if (Control != null)
            {
                //I want to be able to do something like this:
                ButtonOnClick(androidMethod());
            }
        }
    }
}

How do I get androidMethod(); 我如何获得androidMethod(); to execute in this context. 在这种情况下执行。 The samples I find are limited so please try to limit your response to something that would be compatible with the example. 我发现的示例数量有限,因此请尝试将您的回答限制为与示例兼容。 Thankyou! 谢谢!

如果要执行特定于平台的方法,我将使用DepenencyService而不是Custom Renderer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有用于Android的自定义渲染器的Xamarin.Forms会产生编译器错误 - Xamarin.Forms with custom renderer for Android yields compiler error Xamarin.Forms 上的自定义渲染器可以找到 OnElementChanged 方法 - Custom renderer at Xamarin.Forms could find a OnElementChanged method Xamarin.Forms中的条目自定义渲染器(为Android添加所有边框) - Entry Custom Renderer(add all borders for Android) in Xamarin.Forms 如何使用 DependencyService 和接口委托将 xamarin.android 特定功能的方法传递给 xamarin.forms? - How to pass a method of xamarin.android specific functionnality to a xamarin.forms using DependencyService and a delegate of Interface? Xamarin.Forms Wysiwyg:使用自定义渲染器将项目从Xamarion.iOS移至Xamarin.Forms - Xamarin.Forms Wysiwyg: move project from Xamarion.iOS to Xamarin.Forms using custom renderer 如何在Xamarin.Forms中为ToolbarItem创建自定义渲染器? - How can I make a custom renderer for ToolbarItem in Xamarin.Forms? 如何从自定义渲染器设置 Xamarin.Forms 元素 BindableProperties? - How to set Xamarin.Forms Elements BindableProperties from a Custom Renderer? 如何实现 Xamarin.Forms 自定义 RadioButton 渲染器? (转发) - How to implement Xamarin.Forms custom RadioButton renderer? (repost) 使用 XAML 和自定义呈现器的 Xamarin.Forms UserControl - Xamarin.Forms UserControl using XAML and Custom Renderer 使用Xamarin.Forms自定义渲染器时转发事件/命令 - Forwarding events/commands when using Xamarin.Forms Custom Renderer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM