问题描述
我正在使用Xamarin Forms来制作跨平台应用程序,我需要创建一个简单的视图,用户可以选择日期和时间,类似于:在这里找到的要创建的视图:在Xamarin iOS中进行选择. Android解决方案已经准备就绪,但我需要在同一应用程序中为iOS创建解决方案
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
我不能与Xamarin Forms分开使用标准日期选择器和其他标准时间选择器.我需要创建自定义解决方案(一个视图-简单地选择日期和时间).
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).
我试图在Xamarin Forms中创建视图,该视图由2个水平方向的列表组成(一个用于日期,一个用于时间),但是当我选择一个位置时,该列表不会滚动到视图的中间,并且当我向上或向下滚动列表时,不会自动选择中间位置元素.我想创建类似Xamarin-iOS解决方案的东西:日期和时间选择器",但使用Xamarin表单.
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.
我也尝试过在Xamarin-iOS中创建项目日期和时间选择器"的一部分.我有main.storyboard和视图控制器,但我不知道如何在Xamarin Forms中从Xamarin-iOS显示视图并传递选定的日期和时间.
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.
可以帮我吗?
推荐答案
如果要在iOS平台上的Xamarin.Forms上实现日期时间选择器.可以使用 CustomRenderer .
If you want to implement date-time picker on Xamarin.Forms in iOS platform.You can use CustomRenderer.
创建Picker
public class MyPicker:Picker
{
public MyPicker()
{
}
}
并将其添加到xaml
And add it in xaml
<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<!-- Place new controls here -->
<local:MyPicker WidthRequest="150" BackgroundColor="AliceBlue"/>
</StackLayout>
创建Picker
的渲染器.您可以设置 format 您想要的选择器.
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);
}
}
}
并使用MessagingCenter
传递所选的日期和时间.
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
});
}
我已将演示文件上传到 github 上.您可以下载它进行测试.
I have uploaded the demo on github .You can download it for test.
这篇关于Xamarin Forms日期和时间选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!