效果视频:
教程:
1.DotNet MAUI项目Android端安装安卓nuget包;.
NuGet\Install-Package XamarinLibrary.Net.Android.Loper7.DateTimePicker -Version 0.6.3.2
2.参考原作者Android原生源码用法,创建layout.xml用于显示原生的DateTimePicker;
参考示例源码
Platforms/Android/Resources/layout/DateTimePickerLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
......>
<com.loper7.date_time_picker.DateTimePicker
...... />
</LinearLayout>
3.在DotNet MAUI方面将DateTimePicker相关控件使用ViewHandler方案在Dot Net MAUI上显示,涉及Android的Inflate函数相关知识。
3.1参考示例源码Handlers/TimePickerViewHandler.Android.cs
public partial class TimePickerViewHandler:ViewHandler<TimePickerView, LinearLayout>
{
......
protected override LinearLayout CreatePlatformView()
{
var inflater = LayoutInflater.From(Context);
var result = inflater.Inflate(Resource.Layout.DateTimePickerLayout, null) as LinearLayout;
var picker = result.FindViewById<DateTimePicker>(Resource.Id.picker);
......
......
return result;
}
}
3.2初始化配置Handler
参考示例源码MauiProgram.cs
var builder = MauiApp.CreateBuilder();
builder
......
.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
handlers.AddHandler(typeof(TimePickerView), typeof(TimePickerViewHandler));
#endif
});
其他资料:
DotNet MAUI 演示DateTimePicker的Sample的源码:
https://github.com/jingliancui/DotNetDateTimePickerSample
原生Android(Native Android)的DateTimePicker的源码:
https://github.com/loper7/DateTimePicker
原生Android(Native Android)的DateTimePicker的使用方法:
https://github.com/loper7/DateTimePicker#如何使用
微软官方ViewHandler教程示例:
https://learn.microsoft.com/en-us/dotnet/maui/user-interface/handlers/create
如果对本示例使用的nuget包有疑问,可以在公众号留言或者Github发起相关issue与我进行相关技术交流。