繁体   English   中英

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

[英]Xamarin Forms PCL - DatePicker doesn't open correctly on UWP

我没有任何东西,我想要从按钮中打开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绑定到一个给出当前日期的对象。 在其private void OnDateClicked(object sender, EventArgs ea)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显示此选定日期的日期。

预先感谢 !

这是已知问题 也许将来会修复。 我可以建议使用针对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更改为trueIsFocus显示它,因此您需要根据需要进行设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM