xamarin 表单 DatePicker Cancel/OK 事件
xamarin forms DatePicker Cancel/OK event
我找到并尝试了一个自定义呈现的 DatePicker 示例,用于 Android,用于 Xamarin Forms,但不显示在 UnFocus 中单击了哪个按钮。至少不适合我。它来自计算器。
本文中的示例对其他人有帮助吗?我真的需要知道什么时候点击确定按钮。
这扩展了 。我还修复了该答案中的一个错误,这意味着从未使用过渲染器。
子类 DatePicker,以便您可以添加新的 BindableProperty 和新方法。将其放入您的跨平台项目中。
OKCancelDatePicker.cs:
using Xamarin.Forms;
// Replace with YOUR namespace.
namespace TestBugs
{
/// <summary>
/// NOTE: Requires custom renderer on each platform.
/// </summary>
public class OKCancelDatePicker : DatePicker
{
public static readonly BindableProperty UserCancelledProperty = BindableProperty.Create(nameof(UserCancelled), typeof(bool), typeof(OKCancelDatePicker), false);
/// <summary>
/// Bind to "UserCancelled", to propagate this change elsewhere (e.g. to a VM, or to trigger some logic).
/// </summary>
public bool UserCancelled {
get => (bool)GetValue(UserCancelledProperty);
set => SetValue(UserCancelledProperty, value);
}
/// <summary>
/// Optionally add code here. Though usually you'll detect the change by binding to UserCancelled.
/// </summary>
public void OnPickerClosed()
{
if (UserCancelled) {
// User cancelled.
_ = 0; // Dummy code, to set a breakpoint on. You can remove this.
} else {
// User selected OK.
_ = 0; // Dummy code, to set a breakpoint on. You can remove this.
}
}
}
}
为 Android 创建渲染器。将其放入您的 .Android 项目中。
OKCancelDatePickerRenderer.cs:
using Android.App;
using Android.Content;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
// Replace these with YOUR namespaces.
using TestBugs; // Contains OKCancelDatePicker.
using TestBugs.Droid; // Contains OKCancelDatePickerRenderer.
[assembly: ExportRenderer(typeof(OKCancelDatePicker), typeof(OKCancelDatePickerRenderer))]
// Replace this with YOUR Android namespace.
namespace TestBugs.Droid
{
/// <summary>
/// Based on Nick Kovalsky's
/// </summary>
public class OKCancelDatePickerRenderer : DatePickerRenderer
{
public OKCancelDatePickerRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
{
base.OnElementChanged(e);
//Disposing
if (e.OldElement != null) {
_element = null;
}
//Creating
if (e.NewElement != null) {
_element = e.NewElement as OKCancelDatePicker;
}
}
protected OKCancelDatePicker _element;
protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
{
// This mimics what the original renderer did.
var dialog = new DatePickerDialog(Context, (o, e) =>
{
_element.Date = e.Date;
((IElementController)_element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}, year, month, day);
// These use our custom actions when buttons pressed.
dialog.SetButton((int)DialogButtonType.Positive, Context.Resources.GetString(global::Android.Resource.String.Ok), OnOk);
dialog.SetButton((int)DialogButtonType.Negative, Context.Resources.GetString(global::Android.Resource.String.Cancel), OnCancel);
return dialog;
}
private void OnCancel(object sender, DialogClickEventArgs e)
{
// This is what the original renderer did when Cancel pressed.
_element.Unfocus();
// This is our custom logic.
_element.UserCancelled = true;
_element?.OnPickerClosed();
}
private void OnOk(object sender, DialogClickEventArgs e)
{
// This is what the original renderer did when OK pressed.
_element.Date = ((DatePickerDialog)sender).DatePicker.DateTime;
_element.Unfocus();
// This is our custom logic.
_element.UserCancelled = false;
_element?.OnPickerClosed();
}
}
}
用法:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TestBugs"
x:Class="TestBugs.DatePickerTestPage">
<ContentPage.Content>
<StackLayout>
<Label Text="Date Picker Test Page" HorizontalOptions="CenterAndExpand" />
<local:OKCancelDatePicker />
</StackLayout>
</ContentPage.Content>
</ContentPage>
重要提示:如果您的应用程序在其他平台上运行,您需要在每个平台上执行类似的操作。每个平台,尝试找到一个例子来修改。 Nick 的回答有一个 link 对一个 iOS。
待定:我应该在此处添加一个示例,说明如何绑定到 UserCancelled
属性,以便您可以将其连接到页面中的逻辑。
现在,请阅读 Bindable Properties 和 google 以了解绑定到 BindableProperty
控件或视图的示例。
我找到并尝试了一个自定义呈现的 DatePicker 示例,用于 Android,用于 Xamarin Forms,但不显示在 UnFocus 中单击了哪个按钮。至少不适合我。它来自计算器。
本文中的示例对其他人有帮助吗?我真的需要知道什么时候点击确定按钮。
这扩展了
子类 DatePicker,以便您可以添加新的 BindableProperty 和新方法。将其放入您的跨平台项目中。
OKCancelDatePicker.cs:
using Xamarin.Forms;
// Replace with YOUR namespace.
namespace TestBugs
{
/// <summary>
/// NOTE: Requires custom renderer on each platform.
/// </summary>
public class OKCancelDatePicker : DatePicker
{
public static readonly BindableProperty UserCancelledProperty = BindableProperty.Create(nameof(UserCancelled), typeof(bool), typeof(OKCancelDatePicker), false);
/// <summary>
/// Bind to "UserCancelled", to propagate this change elsewhere (e.g. to a VM, or to trigger some logic).
/// </summary>
public bool UserCancelled {
get => (bool)GetValue(UserCancelledProperty);
set => SetValue(UserCancelledProperty, value);
}
/// <summary>
/// Optionally add code here. Though usually you'll detect the change by binding to UserCancelled.
/// </summary>
public void OnPickerClosed()
{
if (UserCancelled) {
// User cancelled.
_ = 0; // Dummy code, to set a breakpoint on. You can remove this.
} else {
// User selected OK.
_ = 0; // Dummy code, to set a breakpoint on. You can remove this.
}
}
}
}
为 Android 创建渲染器。将其放入您的 .Android 项目中。
OKCancelDatePickerRenderer.cs:
using Android.App;
using Android.Content;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
// Replace these with YOUR namespaces.
using TestBugs; // Contains OKCancelDatePicker.
using TestBugs.Droid; // Contains OKCancelDatePickerRenderer.
[assembly: ExportRenderer(typeof(OKCancelDatePicker), typeof(OKCancelDatePickerRenderer))]
// Replace this with YOUR Android namespace.
namespace TestBugs.Droid
{
/// <summary>
/// Based on Nick Kovalsky's
/// </summary>
public class OKCancelDatePickerRenderer : DatePickerRenderer
{
public OKCancelDatePickerRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
{
base.OnElementChanged(e);
//Disposing
if (e.OldElement != null) {
_element = null;
}
//Creating
if (e.NewElement != null) {
_element = e.NewElement as OKCancelDatePicker;
}
}
protected OKCancelDatePicker _element;
protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
{
// This mimics what the original renderer did.
var dialog = new DatePickerDialog(Context, (o, e) =>
{
_element.Date = e.Date;
((IElementController)_element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}, year, month, day);
// These use our custom actions when buttons pressed.
dialog.SetButton((int)DialogButtonType.Positive, Context.Resources.GetString(global::Android.Resource.String.Ok), OnOk);
dialog.SetButton((int)DialogButtonType.Negative, Context.Resources.GetString(global::Android.Resource.String.Cancel), OnCancel);
return dialog;
}
private void OnCancel(object sender, DialogClickEventArgs e)
{
// This is what the original renderer did when Cancel pressed.
_element.Unfocus();
// This is our custom logic.
_element.UserCancelled = true;
_element?.OnPickerClosed();
}
private void OnOk(object sender, DialogClickEventArgs e)
{
// This is what the original renderer did when OK pressed.
_element.Date = ((DatePickerDialog)sender).DatePicker.DateTime;
_element.Unfocus();
// This is our custom logic.
_element.UserCancelled = false;
_element?.OnPickerClosed();
}
}
}
用法:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TestBugs"
x:Class="TestBugs.DatePickerTestPage">
<ContentPage.Content>
<StackLayout>
<Label Text="Date Picker Test Page" HorizontalOptions="CenterAndExpand" />
<local:OKCancelDatePicker />
</StackLayout>
</ContentPage.Content>
</ContentPage>
重要提示:如果您的应用程序在其他平台上运行,您需要在每个平台上执行类似的操作。每个平台,尝试找到一个例子来修改。 Nick 的回答有一个 link 对一个 iOS。
待定:我应该在此处添加一个示例,说明如何绑定到 UserCancelled
属性,以便您可以将其连接到页面中的逻辑。
现在,请阅读 Bindable Properties 和 google 以了解绑定到 BindableProperty
控件或视图的示例。