[英]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.