Xamarin Forms PCL - 在UWP上无法正确打开DatePicker

时间:2016-10-13 10:49:54

标签: c# xamarin

我没有得到任何东西,我想,从按钮,打开我的DatePicker。所以我编码:

private void OnDateClicked(object sender, EventArgs ea)
{
    Debug.WriteLine("PLOPPP");
    //DatePickerControl.IsVisible = true;
    //DatePickerControl.Focus();
    Device.BeginInvokeOnMainThread(() => {
        DatePickerControl.Focus();
    });
}

用户点击/触摸按钮后,没有任何反应..为什么?我只是想开启日期选择器,但我无法弄清楚它为什么不起作用><

XAML部分看起来像这样:

    <AbsoluteLayout x:Name="LayoutTools"
                    AbsoluteLayout.LayoutBounds="0.5, 0.05, 0.9, 0.075"
                    AbsoluteLayout.LayoutFlags="All">
      <!-- DATE -->
      <!--<control:SquareLayout x:Name="DateButton" BackgroundColor="{x:StaticResource NL_BlueNight}" ScalingBase="Height"
                            AbsoluteLayout.LayoutBounds="0, 0.5, 0.1, 1"
                            AbsoluteLayout.LayoutFlags="All"/>-->
      <AbsoluteLayout x:Name="DateButton" BackgroundColor="{x:StaticResource NL_BlueNight}" Opacity="0.8"
                      AbsoluteLayout.LayoutBounds="0, 0.5, 0.1, 1"
                      AbsoluteLayout.LayoutFlags="All">
        <control:CustomLabel Text="{Binding DaySelected}" FontFamily="{extension:FontFamily Roboto_Light}" FontSize="20" TextColor="Gray"
                             HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
                             AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                             AbsoluteLayout.LayoutFlags="All"/>
        <Button Clicked="OnDateClicked" BackgroundColor="Transparent" BorderColor="Transparent"
                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                AbsoluteLayout.LayoutFlags="All"/>
      </AbsoluteLayout>
    </AbsoluteLayout>
    <!-- <control:CustomDatePicker.../> -->

所以在这里,CustomLabel绑定到一个给出当天数的对象。在其上,Button调用private void OnDateClicked(object sender, EventArgs ea)方法。

然后,在这个方法中,我试图打开我放入XAML部分的DatePicker:

    <!-- code above -->
    <control:CustomDatePicker x:Name="DatePickerControl" Format="dd-MM-yyyy" Date="{Binding CurrentDate}" IsVisible="False"
                              MinimumDate="{Binding CurrentDate}"
                              FontFamily="{extension:FontFamily Roboto_Light}" FontSize="20" TextColor="White"
                              XAlign="Center" HasBorder="false" BackgroundColor="Transparent"
                              AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                              AbsoluteLayout.LayoutFlags="All"/>

我的想法是打开选择器,然后保存所有日期,但只在Label中显示所选日期的日期。

提前感谢!

1 个答案:

答案 0 :(得分:2)

这是known issue。也许将来会修复它。我可以使用渲染器为UWP建议我的解决方法:

[assembly: ExportRenderer(typeof(DatePickerRenderer), typeof(SomeDatePickerRenderer))]
namespace SuperForms.UWP.Renderers
{
    public class SomeDatePickerRenderer : DatePickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                // TODO: Focus() doesn't open date picker popup on UWP, it's known issue 
                // on Xamarin.Forms and should be fixed in 2.5. Had to open it manually.
                var flyout = new DatePickerFlyout() { Placement = FlyoutPlacementMode.Top };
                flyout.DatePicked += (s, args) =>
                {
                    Control.Date = args.NewDate;
                };

                FlyoutBase.SetAttachedFlyout(Control, flyout);
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
            {
                if (Element.IsFocused)
                {
                    FlyoutBase.ShowAttachedFlyout(Control);
                }
            }
        }
    }
}

现在每次IsFocus更改为true时都会显示,因此您需要根据自己的要求进行设置。