Xamarin Forms 日期和时间选择器 [英] Xamarin Forms date and time picker

查看:33
本文介绍了Xamarin Forms 日期和时间选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin Forms 制作跨平台应用程序,我需要创建用户可以选择日期和时间的简单视图,类似于:我想创建的视图,我在这里找到:Xamarin iOS 中的选择器.Android 的解决方案已准备就绪,但我需要在同一应用程序中为 iOS 创建解决方案

我无法与 Xamarin Forms 分开使用标准日期选择器和另一个标准时间选择器.我需要创建自定义解决方案(一个视图 - 简单地选择日期和时间).

我尝试在 Xamarin Forms 中创建视图,该视图由 2 个水平方向的列表(一个用于日期,一个用于时间)组成,但是当我选择一个位置时,列表不会滚动到视图中间,并且还有当我向上或向下滚动列表时不会自动选择中间位置元素.我想创建一些类似于 Xamarin-iOS 解决方案的东西:日期和时间选择器"但在 Xamarin 表单中.

我还尝试在项目日期和时间选择器"的 Xamarin-iOS 部分中创建.我有 main.storyboard 和视图控制器,但我不知道如何在 Xamarin Forms 中显示来自 Xamarin-iOS 的视图并传递选定的日期和时间.

你能帮我吗?

解决方案

如果你想在 iOS 平台的 Xamarin.Forms 上实现日期时间选择器.你可以使用 CustomRenderer.

<块引用>

在表单中

创建Picker

的子类

公共类 MyPicker:Picker{公共 MyPicker(){}}

并在xaml中添加

<!-- 在此处放置新控件--><local:MyPicker WidthRequest="150" BackgroundColor="AliceBlue"/></StackLayout>

<块引用>

在 iOS 中

创建Picker的渲染器.你可以设置格式 随心所欲的选择器.

使用系统;使用基金会;使用 UIKit;使用 ObjCRuntime;使用 Xamarin.Forms;使用 Xamarin.Forms.Platform.iOS;使用xxx;使用 xxx.iOS;[程序集:ExportRenderer(typeof(MyPicker),typeof(MyPickerRenderer))]命名空间 xxx.iOS{公共类 MyPickerRenderer:PickerRenderer{字符串 SelectedValue;公共 MyPickerRenderer(){}protected override void OnElementChanged(ElementChangedEventArgs<Picker> e){base.OnElementChanged(e);如果(控制!=空){设置时间选择器();}}void SetTimePicker(){UIDatePicker 选择器 = 新的 UIDatePicker{模式 = UIDatePickerMode.DateAndTime};picker.SetDate(NSDate.Now,true);picker.AddTarget(this,new Selector("DateChange:"),UIControlEvent.ValueChanged);Control.InputView = 选择器;UIToolbar 工具栏 = (UIToolbar)Control.InputAccessoryView;UIBarButtonItem done = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, (object sender, EventArgs click) =>{Control.Text = SelectedValue;工具栏.RemoveFromSuperview();picker.RemoveFromSuperview();Control.ResignFirstResponder();MessagingCenter.Send(this, "pickerSelected", SelectedValue);});UIBarButtonItem empty = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);toolbar.Items = new UIBarButtonItem[] { empty, done };}[导出(日期更改:")]void DateChange(UIDatePicker 选择器){NSDateFormatter 格式化程序 = new NSDateFormatter();formatter.DateFormat = "MM-dd HH:mm aa";//您可以根据需要设置格式Control.Text = formatter.ToString(picker.Date);SelectedValue= formatter.ToString(picker.Date);MessagingCenter.Send(this,"pickerSelected",SelectedValue);}}}

并使用 MessagingCenter 传递选定的日期和时间.

public MainPage(){初始化组件();MessagingCenter.Subscribe(this, "pickerSelected", (sender, arg) => {Console.WriteLine(arg);//arg 是选择的日期和时间});}

我已将演示上传到 github .您可以下载以进行测试.

效果

I am using Xamarin Forms to make Cross-platform application and I need to create simple view that user can choose date and time, similar to this: View that I want to create that i found here: Picker in Xamarin iOS. Solution for Android is ready, but I need to create solution for iOS in the same application

I can not use standard date picker and another standard time picker separately from Xamarin Forms. I need to create custom solution (one view - simple choose both date and time).

I have tried to create view in Xamarin Forms that consist of 2 lists in horizontal orientation (one for date, one for time) but when I select one position, the list is not scrolling to the middle of view and also there is not auto-selecting middle position element when I scroll the list up or down. I want to create something that works like Xamarin-iOS solution: "Date and time picker" but in Xamarin Forms.

I have tried also to create in Xamarin-iOS part of project "date and time picker". I have main.storyboard and view controller but I dont know how to display view from Xamarin-iOS inside Xamarin Forms and pass selected date and time.

Can you help me, please?

解决方案

If you want to implement date-time picker on Xamarin.Forms in iOS platform.You can use CustomRenderer.

in Forms

create a subclass of Picker

public class MyPicker:Picker
{
    public MyPicker()
    {

    }
}

And add it in xaml

<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
    <!-- Place new controls here -->

    <local:MyPicker  WidthRequest="150"  BackgroundColor="AliceBlue"/>

</StackLayout>

in iOS

create the renderer of Picker .And you can set the format of picker as you want.

using System;

using Foundation;
using UIKit;
using ObjCRuntime;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using xxx;
using xxx.iOS;

[assembly:ExportRenderer(typeof(MyPicker),typeof(MyPickerRenderer))]
namespace xxx.iOS
{
  public class MyPickerRenderer:PickerRenderer
  {

    string SelectedValue;

    public MyPickerRenderer()
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        if(Control!=null)
        {
            SetTimePicker();
        }

    }


    void  SetTimePicker()
    {
        UIDatePicker picker = new UIDatePicker
        {
            Mode = UIDatePickerMode.DateAndTime
        };

        picker.SetDate(NSDate.Now,true);

        picker.AddTarget(this,new Selector("DateChange:"),UIControlEvent.ValueChanged);

        Control.InputView = picker;


        UIToolbar toolbar = (UIToolbar)Control.InputAccessoryView;

        UIBarButtonItem done = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, (object sender, EventArgs click) =>
        {
            Control.Text = SelectedValue;
            toolbar.RemoveFromSuperview();
            picker.RemoveFromSuperview();
            Control.ResignFirstResponder();
            MessagingCenter.Send<Object, string>(this, "pickerSelected", SelectedValue);



        });

        UIBarButtonItem empty = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);

        toolbar.Items = new UIBarButtonItem[] { empty, done };

    }

    [Export("DateChange:")]
    void DateChange(UIDatePicker picker)
    {
        NSDateFormatter formatter = new NSDateFormatter();

        formatter.DateFormat = "MM-dd HH:mm aa"; //you can set the format as you want

        Control.Text = formatter.ToString(picker.Date);

        SelectedValue= formatter.ToString(picker.Date);

        MessagingCenter.Send<Object, string>(this,"pickerSelected",SelectedValue);
    }
  }
}

And use MessagingCenter to pass the selected date and time.

public MainPage()
{
    InitializeComponent();

    MessagingCenter.Subscribe<Object, string>(this, "pickerSelected", (sender, arg) => {
        Console.WriteLine(arg);
        //arg is the selected date and time
    });

}

I have uploaded the demo on github .You can download it for test.

The effect

这篇关于Xamarin Forms 日期和时间选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆