Last active
September 21, 2017 08:37
-
-
Save Krumelur/2c465479fd9099d0a3ddf4570fa2f753 to your computer and use it in GitHub Desktop.
Customise "Done" button in Forms picker with an Effect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
using System.Reflection; | |
using Android.Views; | |
using Android.App; | |
using static Android.Views.View; | |
using Android.Content; | |
using PickerTest.Droid; | |
[assembly:ResolutionGroupName("Xamarin")] | |
[assembly:ExportEffect(typeof(PickerTunerEffect), "PickerTunerEffect")] | |
namespace PickerTest.Droid | |
{ | |
public class PickerTunerEffect : PlatformEffect | |
{ | |
public PickerTunerEffect() | |
{ | |
} | |
public IntPtr Handle => throw new NotImplementedException(); | |
public void Dispose() | |
{ | |
} | |
public void OnClick(Android.Views.View v) | |
{ | |
} | |
protected override void OnAttached() | |
{ | |
Control.SetOnClickListener(new CustomPickerListener(Container)); | |
} | |
protected override void OnDetached() | |
{ | |
} | |
class CustomPickerListener : Java.Lang.Object, IOnClickListener | |
{ | |
public CustomPickerListener(ViewGroup renderer) | |
{ | |
_renderer = renderer; | |
} | |
ViewGroup _renderer; | |
public void OnClick(global::Android.Views.View v) | |
{ | |
if (_renderer == null) | |
{ | |
return; | |
} | |
var dynClickHandler = _renderer.GetType().GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance); | |
dynClickHandler.Invoke(_renderer, null); | |
var field = typeof(Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer).GetField("_dialog", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance); | |
var alertDialog = field.GetValue(_renderer) as AlertDialog; | |
var btn = alertDialog.GetButton((int)DialogButtonType.Negative); | |
btn.Text = "Custom Title!"; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using PickerTest.iOS; | |
using UIKit; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.iOS; | |
[assembly:ResolutionGroupName("Xamarin")] | |
[assembly:ExportEffect(typeof(PickerTunerEffect), "PickerTunerEffect")] | |
namespace PickerTest.iOS | |
{ | |
public class PickerTunerEffect : PlatformEffect | |
{ | |
public PickerTunerEffect() | |
{ | |
} | |
UIToolbar _toolbar; | |
UIBarButtonItem _doneBtn; | |
protected override void OnAttached() | |
{ | |
var textField = Control; | |
_toolbar = textField.InputAccessoryView as UIToolbar; | |
_doneBtn = _toolbar.Items[1]; | |
_toolbar.SetItems(new UIBarButtonItem[] { | |
new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), | |
new UIBarButtonItem("Custom Title!", UIBarButtonItemStyle.Done, HandleButtonClicked) | |
}, false); | |
} | |
protected override void OnDetached() | |
{ | |
_toolbar.Items = new UIBarButtonItem[] { }; | |
_toolbar = null; | |
_doneBtn = null; | |
} | |
void HandleButtonClicked(object sender, EventArgs e) | |
{ | |
UIApplication.SharedApplication.SendAction(_doneBtn.Action, _doneBtn.Target, sender: null, forEvent: null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment