簡體   English   中英

xamarin forms DatePicker 取消/確定事件

[英]xamarin forms DatePicker Cancel/OK event

我找到並嘗試了一個自定義呈現的 DatePicker 示例,對於 Android,對於 Xamarin Forms,並且沒有顯示在 UnFocus 中單擊了哪個按鈕。 至少對我來說不是。 它來自stackoverflow。 Xamarin.Forms Android DatePicker/TimePicker按鈕監聽器

本文中的示例對其他人有幫助嗎? 我真的需要知道何時單擊“確定”按鈕。

這擴展了 Nick Kovalsky 的回答 我還修復了該答案中的一個錯誤,這意味着從未使用過渲染器。

子類 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 https://stackoverflow.com/a/60786875/199364.
    /// </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>

重要提示:如果您的應用程序在其他平台上運行,您需要在每個平台上執行類似操作。 每個平台,試着找一個例子來修改。 尼克的答案有一個指向 iOS 的鏈接。


待定:我應該在這里添加一個示例,說明如何綁定到該UserCancelled屬性,以便您可以將其連接到頁面中的邏輯。

現在,請閱讀Bindable Properties和 google 以了解綁定到控件或視圖的BindableProperty的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM