【问题标题】:Outlined Material Entry for Xamarin forms for Android and iOS both适用于 Android 和 iOS 的 Xamarin 表单的概述材料条目
【发布时间】:2020-06-19 13:54:15
【问题描述】:

如何使用自定义渲染器在 Xamarin 表单中为 Android 和 iOS 创建概述的材料条目? 例如Outlined Material Textfield

【问题讨论】:

  • 您能否提供更多详细信息,说明您在此处尝试实现的目标。

标签: android ios xamarin.forms material-design


【解决方案1】:

欢迎来到 SO!

您可以使用Custom Renderer 自定义包含设计为Entry 的材质的视图。

在表单中创建一个EntryView

public class EntryView : ContentView
{
    public static readonly BindableProperty TextProperty =
      BindableProperty.Create("Text", typeof(string), typeof(EntryView), null);
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}

关于Android,然后创建EntryViewRenderer

public class EntryViewRenderer : ViewRenderer
{
    global::Android.Views.View view;
    global::Android.Widget.EditText editText;
    EntryView entryView;
    Activity activity;

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
    {
        base.OnElementChanged(e);
        if(e.NewElement != null)
        {
            entryView= e.NewElement as EntryView;
        }

        if (e.OldElement != null || Element == null)
        {
            return;
        }

        try
        {
            SetupUserInterface();
            SetupEventHandlers();
            AddView(view);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(@"           ERROR: ", ex.Message);
        }
    }

    private void SetupEventHandlers()
    {
        editText.TextChanged += EditText_TextChanged;
    }

    private void EditText_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
    {
        entryView.Text = editText.Text; 
        Console.WriteLine("chanegd +" + entryView.Text);
    }

    void SetupUserInterface()
    {
        activity = this.Context as Activity;
        view = activity.LayoutInflater.Inflate(Resource.Layout.EntryLayout, this, false);

        editText = view.FindViewById<EditText>(Resource.Id.editText1);
    }

    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
        base.OnLayout(changed, l, t, r, b);

        var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
        var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);

        view.Measure(msw, msh);
        view.Layout(0, 0, r - l, b - t);
    }
}

另外,你需要为这个View添加EntryLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Label"
            android:outlineSpotShadowColor="@color/cardview_shadow_start_color"/>
    </android.support.design.widget.TextInputLayout>

</LinearLayout>

现在,您可以在 Xamarin Forms 的 Xaml 中使用它:

xmlns:app18="clr-namespace:App18"

<app18:EntryView Text="abc"/>

效果:

更多信息可以参考这个讨论:Outlined Textbox in Xamarin.Forms

但是,关于 iOS ,还没有找到通过自定义渲染器实现这一目标的方法。

另外,还有一个付费的 Nuget PackagesSfTextInputLayout 可以在 Andoird 和 iOS 平台上实现。也可以看看that

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2017-12-21
    • 2019-12-15
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多