简体   繁体   English

Xamarin.Forms:Android<entry> 和依赖服务?</entry>

[英]Xamarin.Forms: Android <Entry> and dependency service?

I am learning C# and Xamarin.Forms at the moment, so please treat me like a complete beginner.我目前正在学习 C# 和 Xamarin.Forms,所以请把我当作一个完整的初学者。

I have an entry in my XML in Xamarin.Forms:我在 Xamarin.Forms 中的 XML 中有一个条目:

<Entry x:Name="temperature" Placeholder="Temperatur" Keyboard="Numeric" Margin="20,0"/>

In my CS:在我的 CS 中:

double temp = double.Parse(temperature.Text);

This all works fine.这一切都很好。 But I need to use a different kind of entry for Android.但我需要为 Android 使用不同类型的条目。 Because Xamarin.Forms Entry do not handle decimal separators very well when it comes to Samsung phones.因为 Xamarin.Forms 条目在涉及三星手机时不能很好地处理小数分隔符。

On Android I want to use NuGet "NumericEditText-Xamarin.Android" like this:在 Android 我想像这样使用 NuGet "NumericEditText-Xamarin.Android" :

<br.com.akamud.NumericEditText 
android:id="@+id/txtNumeric"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number|numberDecimal" />

It should handle my entry input, and then of course handle the entry after that with the shared code of Xamarin.Forms.它应该处理我的条目输入,然后当然使用 Xamarin.Forms 的共享代码处理之后的条目。 Is dependency service the best way to go about solving this?依赖服务是解决此问题的 go 的最佳方法吗?
Would someone be so kind to help me out in the right direction.有人会这么好心来帮助我朝着正确的方向前进吗?

Dependency service is used to invoke native api on specific project, if the issue is related to UI, a custom renderer is the better choice.依赖服务用于在特定项目上调用原生 api,如果问题与 UI 有关,自定义渲染器是更好的选择。

You need to create a custom renderer for a custom view, and replace the view with NumericEditText inside the custom renderer.您需要为自定义视图创建自定义渲染器,并在自定义渲染器中将视图替换为 NumericEditText。

Create Custom View创建自定义视图

public class EntryView : View
{
}

Usage of Custom view in page页面中自定义视图的使用

 xmlns:local ="clr-namespace:App2"
 <local:EntryView/>

Custom renderer自定义渲染器

[assembly: ExportRenderer(typeof(EntryView), typeof(MyRenderer))]
namespace App2.Droid
{

    public class MyRenderer : Xamarin.Forms.Platform.Android.ViewRenderer<EntryView, NumericEditText>
    {

        Context _context;
        NumericEditText editView;
        public MyRenderer(Context context) : base(context)
        {
            _context = context;
        }


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

            if(e.NewElement != null)
            {
                if (Control == null)
                {
                    editView = new NumericEditText(Context);
                    editView.InputType = InputTypes.NumberFlagDecimal|InputTypes.ClassNumber;
                    SetNativeControl(editView);
                }
            }
        }
    }
}

Refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view .请参阅https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM