简体   繁体   中英

Xamarin.Forms Custom Renderer in Android stops working when text gravity is specified

So I'm working on a Xamarin.Forms Project and I wanted to make a custom renderer for the Entry Class that adds a border to the bottom of the entry only. I had completed this part, but then realized that Xamarin.Forms does not automatically center the text in the entry vertically. However, when I added this feature, the page with the Entries on it would no longer load at all.

Here is my code for the renderer

using Xamarin.Forms.Platform.Android;
using HuntFishNy.Droid.Renderer;
using Xamarin.Forms;
using HuntFishNy.Views;


[assembly: ExportRenderer(typeof(BottomBorderEntry),typeof(BottomBorderEntryRenderer))]

namespace HuntFishNy.Droid.Renderer
{


class BottomBorderEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.Background = Resources.GetDrawable(Resource.Drawable.BottomBorderRectangle);
            Control.SetForegroundGravity(Android.Views.GravityFlags.CenterVertical);

        }
    }


   }
}

For reference, I am doing all my layout work in XAML and the bit with the bottom border worked just fine on its own. Everything works so long as I leave out the line where I set the gravity.

Also, this is what printed out in the debug console if that helps at all

07-01 11:33:45.971 D/Mono (22308): Assembly Loader probing location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.981 D/Mono (22308): Image addref HuntFishNy[0x96ab1880] -> /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll[0x9b32ae00]: 2 07-01 11:33:45.981 D/Mono (22308): Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.981 D/Mono (22308): Config attempting to parse: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNy.dll.config'. 07-01 11:33:45.981 D/Mono (22308): Config attempting to parse: '/Users/builder/data/lanes/2923/52635947/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/HuntFishNy/HuntFishNy.config'. 07-01 11:33:45.981 D/Mono (22308): Unloading image /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll [0x9b32ae00]. 07-01 11:33:45.981 D/Mono (22308): Assembly Loader probing location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.981 D/Mono (22308): Image addref HuntFishNy[0x96ab1940] -> /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll[0x9b32ae00]: 2 07-01 11:33:45.981 D/Mono (22308): Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.981 D/Mono (22308): Config attempting to parse: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNy.dll.config'. 07-01 11:33:45.981 D/Mono (22308): Config attempting to parse: '/Users/builder/data/lanes/2923/52635947/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/HuntFishNy/HuntFishNy.config'. 07-01 11:33:45.981 D/Mono (22308): Unloading image /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll [0x9b32ae00]. 07-01 11:33:45.991 D/Mono (22308): Assembly Loader probing location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.991 D/Mono (22308): Image addref HuntFishNy[0x96ab18e0] -> /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll[0x9b32ae00]: 2 07-01 11:33:45.991 D/Mono (22308): Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.991 D/Mono (22308): Config attempting to parse: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNy.dll.config'. 07-01 11:33:45.991 D/Mono (22308): Config attempting to parse: '/Users/builder/data/lanes/2923/52635947/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/HuntFishNy/HuntFishNy.config'. 07-01 11:33:45.991 D/Mono (22308): Unloading image /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll [0x9b32ae00]. 07-01 11:33:45.991 D/Mono (22308): Assembly Loader probing location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.991 D/Mono (22308): Image addref HuntFishNy[0x96ab1880] -> /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll[0x9b32ae00]: 2 07-01 11:33:45.991 D/Mono (22308): Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.991 D/Mono (22308): Config attempting to parse: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNy.dll.config'. 07-01 11:33:45.991 D/Mono (22308): Config attempting to parse: '/Users/builder/data/lanes/2923/52635947/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/HuntFishNy/HuntFishNy.config'. 07-01 11:33:45.991 D/Mono (22308): Unloading image /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll [0x9b32ae00]. 07-01 11:33:45.991 D/Mono (22308): Assembly Loader probing location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.991 D/Mono (22308): Image addref HuntFishNy[0x96ab1940] -> /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll[0x9b32ae00]: 2 07-01 11:33:45.991 D/Mono (22308): Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll'. 07-01 11:33:45.991 D/Mono (22308): Config attempting to parse: '/storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNy.dll.config'. 07-01 11:33:45.991 D/Mono (22308): Config attempting to parse: '/Users/builder/data/lanes/2923/52635947/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/HuntFishNy/HuntFishNy.config'. 07-01 11:33:45.991 D/Mono (22308): Unloading image /storage/emulated/0/Android/data/HuntFishNy.Droid/files/. override /HuntFishNY.dll [0x9b32ae00]. 07-01 11:33:55.252 D/Mono (22308): [0x9beb5100] worker finishing

Control.Gravity = GravityFlags.CenterVertical;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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