Issue
So I am working on Android application. Right now I am creating MainPage where I insert Entry
which has the bottom line as always. The bottom line on my previewer is White
while on my phone it appears to be Black
.
So to fix the issue I decided to play with renderers and see if I can fix it.
I created Class in App called CustomEntryRenderer
which inherits from Entry.
Then I created Class in App.Android called CustomEntryRednererAndroid
which is supposed to change the color of bottom entry line. But it doesn`t affect it. I tried doing the same with some custom renderers I found on the internet.
For example deleting bottom line didn`t affect the program as well: removing line
Entry from MainPage.xaml:
<Entry
Grid.Row="4"
Grid.ColumnSpan="2"
TextColor="Silver"
Placeholder="Write Your nickname"
PlaceholderColor="Silver"
/>
CustomEntryRenderer:
public class CustomEntryRenderer : Entry
{
}
CustomEntryRendererAndroid:
[assembly: ExportRenderer(typeof(CustomEntryRenderer), typeof(MyEntryRenderer))]
namespace App3.Droid
{
public class MyEntryRenderer : EntryRenderer
{
public MyEntryRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || e.NewElement == null) return;
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.White);
else
Control.Background.SetColorFilter(Android.Graphics.Color.White, PorterDuff.Mode.SrcAtop);
}
}
}
Top answer for Android
And for some reason also in CustomEntryRendererAndroid.cs
I had to use Android.Graphic
instead of Xamarin.Forms.Color
. But I dont think that is the issue.
I have been trying for a couple of hours now and can`t find the way out of this situation. Would appreciate any ideas.
Solution
In xaml you are using the default Entry
control and not your CustomEntryRenderer
which is what your renderer is affecting. Also, you might want to rename it because it is not actually your renderer, but your custom control.
To resolve your issue you can either change your renderer typeof(CustomEntryRenderer)
to typeof(Entry)
to affect all Android entries in your app by default. For example, this worked for my test app for all Entries:
[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
namespace YourNameSpace
{
public class MyEntryRenderer : EntryRenderer
{
public MyEntryRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || e.NewElement == null) return;
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.White);
else
Control.Background.SetColorFilter(Android.Graphics.Color.White, PorterDuff.Mode.SrcAtop);
}
}
}
The other option is to switch your xaml code in MainPage
to actually use your custom control. For example, <local:CustomEntryRenderer/>
Answered By - Nick Peppers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.