Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml Xamarin窗体日期时间选择器_Xaml_Xamarin_Xamarin.forms - Fatal编程技术网

Xaml Xamarin窗体日期时间选择器

Xaml Xamarin窗体日期时间选择器,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我在网上找到了一个自定义渲染器,但有一个错误。有人知道我如何控制约会时间吗?目前,我使用单独的日期选择器和时间选择器,但我希望它被结合起来 我将在下面发布我从另一篇文章中找到的代码。这是该帖子的链接 我已经附上日期时间选择器的代码 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

我在网上找到了一个自定义渲染器,但有一个错误。有人知道我如何控制约会时间吗?目前,我使用单独的日期选择器和时间选择器,但我希望它被结合起来

我将在下面发布我从另一篇文章中找到的代码。这是该帖子的链接


我已经附上日期时间选择器的代码

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:DaysBetweenDates"
         x:Class="DaysBetweenDates.MainPage">
<ContentPage.Padding>

    <OnPlatform x:TypeArguments="Thickness">
        <On Platform="iOS" Value="0, 20, 0, 0" />
    </OnPlatform>
</ContentPage.Padding>

<StackLayout Margin="10">
    <Label Text="Days Between Dates"
           Style="{DynamicResource TitleStyle}"
           Margin="0, 20"
           HorizontalTextAlignment="Center" />

    <Label Text="Start Date:" />

    <DatePicker x:Name="startDatePicker"
                Format="D"
                Margin="30, 0, 0, 30"
                DateSelected="OnDateSelected" />

    <Label Text="End Date:" />

    <DatePicker x:Name="endDatePicker"
                MinimumDate="{Binding Source={x:Reference startDatePicker},
                                      Path=Date}"
                Format="D"
                Margin="30, 0, 0, 30"
                DateSelected="OnDateSelected" />

    <StackLayout Orientation="Horizontal"
                 Margin="0, 0, 0, 30">
        <Label Text="Include both days in total: "
               VerticalOptions="Center" />
        <Switch x:Name="includeSwitch"
                Toggled="OnSwitchToggled" />
    </StackLayout>

    <Label x:Name="resultLabel"
           FontAttributes="Bold"
           HorizontalTextAlignment="Center" />
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:DaysBetweenDates"
         x:Class="DaysBetweenDates.MainPage">
<ContentPage.Padding>
    <OnPlatform x:TypeArguments="Thickness">
        <On Platform="iOS" Value="0, 20, 0, 0" />
    </OnPlatform>
</ContentPage.Padding>


<StackLayout Margin="10">
    <Label Text="Days Between Dates"
           Style="{DynamicResource TitleStyle}"
           Margin="0, 20"
           HorizontalTextAlignment="Center" />

    <Label Text="Start Date:" />

    <DatePicker x:Name="startDatePicker"
                Format="D"
                Margin="30, 0, 0, 30"
                DateSelected="OnDateSelected" />

    <Label Text="End Date:" />

    <DatePicker x:Name="endDatePicker"
                MinimumDate="{Binding Source={x:Reference startDatePicker},
                                      Path=Date}"
                Format="D"
                Margin="30, 0, 0, 30"
                DateSelected="OnDateSelected" />

    <StackLayout Orientation="Horizontal"
                 Margin="0, 0, 0, 30">
        <Label Text="Include both days in total: "
               VerticalOptions="Center" />
        <Switch x:Name="includeSwitch"
                Toggled="OnSwitchToggled" />
    </StackLayout>

    <Label x:Name="resultLabel"
           FontAttributes="Bold"
           HorizontalTextAlignment="Center" />

</StackLayout>

我已经附上日期时间选择器的代码

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:DaysBetweenDates"
         x:Class="DaysBetweenDates.MainPage">
<ContentPage.Padding>

    <OnPlatform x:TypeArguments="Thickness">
        <On Platform="iOS" Value="0, 20, 0, 0" />
    </OnPlatform>
</ContentPage.Padding>

<StackLayout Margin="10">
    <Label Text="Days Between Dates"
           Style="{DynamicResource TitleStyle}"
           Margin="0, 20"
           HorizontalTextAlignment="Center" />

    <Label Text="Start Date:" />

    <DatePicker x:Name="startDatePicker"
                Format="D"
                Margin="30, 0, 0, 30"
                DateSelected="OnDateSelected" />

    <Label Text="End Date:" />

    <DatePicker x:Name="endDatePicker"
                MinimumDate="{Binding Source={x:Reference startDatePicker},
                                      Path=Date}"
                Format="D"
                Margin="30, 0, 0, 30"
                DateSelected="OnDateSelected" />

    <StackLayout Orientation="Horizontal"
                 Margin="0, 0, 0, 30">
        <Label Text="Include both days in total: "
               VerticalOptions="Center" />
        <Switch x:Name="includeSwitch"
                Toggled="OnSwitchToggled" />
    </StackLayout>

    <Label x:Name="resultLabel"
           FontAttributes="Bold"
           HorizontalTextAlignment="Center" />
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:DaysBetweenDates"
         x:Class="DaysBetweenDates.MainPage">
<ContentPage.Padding>
    <OnPlatform x:TypeArguments="Thickness">
        <On Platform="iOS" Value="0, 20, 0, 0" />
    </OnPlatform>
</ContentPage.Padding>


<StackLayout Margin="10">
    <Label Text="Days Between Dates"
           Style="{DynamicResource TitleStyle}"
           Margin="0, 20"
           HorizontalTextAlignment="Center" />

    <Label Text="Start Date:" />

    <DatePicker x:Name="startDatePicker"
                Format="D"
                Margin="30, 0, 0, 30"
                DateSelected="OnDateSelected" />

    <Label Text="End Date:" />

    <DatePicker x:Name="endDatePicker"
                MinimumDate="{Binding Source={x:Reference startDatePicker},
                                      Path=Date}"
                Format="D"
                Margin="30, 0, 0, 30"
                DateSelected="OnDateSelected" />

    <StackLayout Orientation="Horizontal"
                 Margin="0, 0, 0, 30">
        <Label Text="Include both days in total: "
               VerticalOptions="Center" />
        <Switch x:Name="includeSwitch"
                Toggled="OnSwitchToggled" />
    </StackLayout>

    <Label x:Name="resultLabel"
           FontAttributes="Bold"
           HorizontalTextAlignment="Center" />

</StackLayout>
这是我的密码

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    void OnDateSelected(object sender, DateChangedEventArgs args)
    {
        Recalculate();
    }

    void OnSwitchToggled(object sender, ToggledEventArgs args)
    {
        Recalculate();
    }

    void Recalculate()
    {
        TimeSpan timeSpan = endDatePicker.Date - startDatePicker.Date +
        (includeSwitch.IsToggled ? TimeSpan.FromDays(1) : TimeSpan.Zero);

        resultLabel.Text = String.Format("{0} day{1} between dates",
                                        timeSpan.Days, timeSpan.Days == 1 ? "" : "s");
    }
}
这是我的密码

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    void OnDateSelected(object sender, DateChangedEventArgs args)
    {
        Recalculate();
    }

    void OnSwitchToggled(object sender, ToggledEventArgs args)
    {
        Recalculate();
    }

    void Recalculate()
    {
        TimeSpan timeSpan = endDatePicker.Date - startDatePicker.Date +
        (includeSwitch.IsToggled ? TimeSpan.FromDays(1) : TimeSpan.Zero);

        resultLabel.Text = String.Format("{0} day{1} between dates",
                                        timeSpan.Days, timeSpan.Days == 1 ? "" : "s");
    }
}

在Xamarin.forms Android上,您可以尝试下面的代码

创建DateTimePicker.cs类

 public class DateTimePicker : Entry, INotifyPropertyChanged
{
    public DatePicker _datePicker { get; private set; } = new DatePicker() { MinimumDate = DateTime.Today, IsVisible = false };
    public TimePicker _timePicker { get; private set; } = new TimePicker() { IsVisible = false };
    string _stringFormat { get; set; }
    public string StringFormat { get { return _stringFormat ?? "dd/MM/yyyy HH:mm"; } set { _stringFormat = value; } }
    public DateTime DateTime
    {
        get { return (DateTime)GetValue(DateTimeProperty); }
        set { SetValue(DateTimeProperty, value); OnPropertyChanged("DateTime"); }
    }

    private TimeSpan _time
    {
        get
        {
            return TimeSpan.FromTicks(DateTime.Ticks);
        }
        set
        {
            DateTime = new DateTime(DateTime.Date.Ticks).AddTicks(value.Ticks);
        }
    }

    private DateTime _date
    {
        get
        {
            return DateTime.Date;
        }
        set
        {
            DateTime = new DateTime(DateTime.TimeOfDay.Ticks).AddTicks(value.Ticks);
        }
    }

    BindableProperty DateTimeProperty = BindableProperty.Create("DateTime", typeof(DateTime), typeof(DateTimePicker), DateTime.Now, BindingMode.TwoWay, propertyChanged: DTPropertyChanged);

    public DateTimePicker()
    {
        BindingContext = this;
        _datePicker.SetBinding<DateTimePicker>(DatePicker.DateProperty, p => p._date);
        _timePicker.SetBinding<DateTimePicker>(TimePicker.TimeProperty, p => p._time);
        _timePicker.Unfocused += (sender, args) => _time = _timePicker.Time;
        _datePicker.Focused += (s, a) => UpdateEntryText();

        GestureRecognizers.Add(new TapGestureRecognizer()
        {
            Command = new Command(() => _datePicker.Focus())
        });
        Focused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() => _datePicker.Focus());
        };
        _datePicker.Unfocused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                _timePicker.Focus();
                _date = _datePicker.Date;
                UpdateEntryText();
            });
        };
    }

    private void UpdateEntryText()
    {
        Text = DateTime.ToString(StringFormat);
    }

    static void DTPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var timePicker = (bindable as DateTimePicker);
        timePicker.UpdateEntryText();
    }
}

在Xamarin.forms Android上,您可以尝试下面的代码

创建DateTimePicker.cs类

 public class DateTimePicker : Entry, INotifyPropertyChanged
{
    public DatePicker _datePicker { get; private set; } = new DatePicker() { MinimumDate = DateTime.Today, IsVisible = false };
    public TimePicker _timePicker { get; private set; } = new TimePicker() { IsVisible = false };
    string _stringFormat { get; set; }
    public string StringFormat { get { return _stringFormat ?? "dd/MM/yyyy HH:mm"; } set { _stringFormat = value; } }
    public DateTime DateTime
    {
        get { return (DateTime)GetValue(DateTimeProperty); }
        set { SetValue(DateTimeProperty, value); OnPropertyChanged("DateTime"); }
    }

    private TimeSpan _time
    {
        get
        {
            return TimeSpan.FromTicks(DateTime.Ticks);
        }
        set
        {
            DateTime = new DateTime(DateTime.Date.Ticks).AddTicks(value.Ticks);
        }
    }

    private DateTime _date
    {
        get
        {
            return DateTime.Date;
        }
        set
        {
            DateTime = new DateTime(DateTime.TimeOfDay.Ticks).AddTicks(value.Ticks);
        }
    }

    BindableProperty DateTimeProperty = BindableProperty.Create("DateTime", typeof(DateTime), typeof(DateTimePicker), DateTime.Now, BindingMode.TwoWay, propertyChanged: DTPropertyChanged);

    public DateTimePicker()
    {
        BindingContext = this;
        _datePicker.SetBinding<DateTimePicker>(DatePicker.DateProperty, p => p._date);
        _timePicker.SetBinding<DateTimePicker>(TimePicker.TimeProperty, p => p._time);
        _timePicker.Unfocused += (sender, args) => _time = _timePicker.Time;
        _datePicker.Focused += (s, a) => UpdateEntryText();

        GestureRecognizers.Add(new TapGestureRecognizer()
        {
            Command = new Command(() => _datePicker.Focus())
        });
        Focused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() => _datePicker.Focus());
        };
        _datePicker.Unfocused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                _timePicker.Focus();
                _date = _datePicker.Date;
                UpdateEntryText();
            });
        };
    }

    private void UpdateEntryText()
    {
        Text = DateTime.ToString(StringFormat);
    }

    static void DTPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var timePicker = (bindable as DateTimePicker);
        timePicker.UpdateEntryText();
    }
}

使DateTimePicker继承一个ContentView而不仅仅是一个条目,然后创建Stacklayout,将条目以及日期和时间选择器添加到内容中

请参阅DateTimePicker2.cs:

  public class DateTimePicker2 : ContentView, INotifyPropertyChanged
{
    public Entry _entry { get; private set; } = new Entry();
    public DatePicker _datePicker { get; private set; } = new DatePicker() { MinimumDate = DateTime.Today, IsVisible = false };
    public TimePicker _timePicker { get; private set; } = new TimePicker() { IsVisible = false };
    string _stringFormat { get; set; }
    public string StringFormat { get { return _stringFormat ?? "dd/MM/yyyy HH:mm"; } set { _stringFormat = value; } }
    public DateTime DateTime
    {
        get { return (DateTime)GetValue(DateTimeProperty); }
        set { SetValue(DateTimeProperty, value); OnPropertyChanged("DateTime"); }
    }

    private TimeSpan _time
    {
        get
        {
            return TimeSpan.FromTicks(DateTime.Ticks);
        }
        set
        {
            DateTime = new DateTime(DateTime.Date.Ticks).AddTicks(value.Ticks);
        }
    }

    private DateTime _date
    {
        get
        {
            return DateTime.Date;
        }
        set
        {
            DateTime = new DateTime(DateTime.TimeOfDay.Ticks).AddTicks(value.Ticks);
        }
    }

    BindableProperty DateTimeProperty = BindableProperty.Create("DateTime", typeof(DateTime), typeof(DateTimePicker2), DateTime.Now, BindingMode.TwoWay, propertyChanged: DTPropertyChanged);

    public DateTimePicker2()
    {
        BindingContext = this;

        Content = new StackLayout()
        {
            Children =
            {
                _datePicker,
                _timePicker,
                _entry
            }
        };

        _datePicker.SetBinding<DateTimePicker2>(DatePicker.DateProperty, p => p._date);
        _timePicker.SetBinding<DateTimePicker2>(TimePicker.TimeProperty, p => p._time);
        _timePicker.Unfocused += (sender, args) => _time = _timePicker.Time;
        _datePicker.Focused += (s, a) => UpdateEntryText();

        GestureRecognizers.Add(new TapGestureRecognizer()
        {
            Command = new Command(() => _datePicker.Focus())
        });
        _entry.Focused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() => _datePicker.Focus());
        };
        _datePicker.Unfocused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                _timePicker.Focus();
                _date = _datePicker.Date;
                UpdateEntryText();
            });
        };
    }

    private void UpdateEntryText()
    {
        _entry.Text = DateTime.ToString(StringFormat);
    }

    static void DTPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var timePicker = (bindable as DateTimePicker2);
        timePicker.UpdateEntryText();
    }
}
Xaml中的用法:

<StackLayout>
        <local:DateTimePicker2></local:DateTimePicker2>
    </StackLayout>

使DateTimePicker继承一个ContentView而不仅仅是一个条目,然后创建Stacklayout,将条目以及日期和时间选择器添加到内容中

请参阅DateTimePicker2.cs:

  public class DateTimePicker2 : ContentView, INotifyPropertyChanged
{
    public Entry _entry { get; private set; } = new Entry();
    public DatePicker _datePicker { get; private set; } = new DatePicker() { MinimumDate = DateTime.Today, IsVisible = false };
    public TimePicker _timePicker { get; private set; } = new TimePicker() { IsVisible = false };
    string _stringFormat { get; set; }
    public string StringFormat { get { return _stringFormat ?? "dd/MM/yyyy HH:mm"; } set { _stringFormat = value; } }
    public DateTime DateTime
    {
        get { return (DateTime)GetValue(DateTimeProperty); }
        set { SetValue(DateTimeProperty, value); OnPropertyChanged("DateTime"); }
    }

    private TimeSpan _time
    {
        get
        {
            return TimeSpan.FromTicks(DateTime.Ticks);
        }
        set
        {
            DateTime = new DateTime(DateTime.Date.Ticks).AddTicks(value.Ticks);
        }
    }

    private DateTime _date
    {
        get
        {
            return DateTime.Date;
        }
        set
        {
            DateTime = new DateTime(DateTime.TimeOfDay.Ticks).AddTicks(value.Ticks);
        }
    }

    BindableProperty DateTimeProperty = BindableProperty.Create("DateTime", typeof(DateTime), typeof(DateTimePicker2), DateTime.Now, BindingMode.TwoWay, propertyChanged: DTPropertyChanged);

    public DateTimePicker2()
    {
        BindingContext = this;

        Content = new StackLayout()
        {
            Children =
            {
                _datePicker,
                _timePicker,
                _entry
            }
        };

        _datePicker.SetBinding<DateTimePicker2>(DatePicker.DateProperty, p => p._date);
        _timePicker.SetBinding<DateTimePicker2>(TimePicker.TimeProperty, p => p._time);
        _timePicker.Unfocused += (sender, args) => _time = _timePicker.Time;
        _datePicker.Focused += (s, a) => UpdateEntryText();

        GestureRecognizers.Add(new TapGestureRecognizer()
        {
            Command = new Command(() => _datePicker.Focus())
        });
        _entry.Focused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() => _datePicker.Focus());
        };
        _datePicker.Unfocused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                _timePicker.Focus();
                _date = _datePicker.Date;
                UpdateEntryText();
            });
        };
    }

    private void UpdateEntryText()
    {
        _entry.Text = DateTime.ToString(StringFormat);
    }

    static void DTPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var timePicker = (bindable as DateTimePicker2);
        timePicker.UpdateEntryText();
    }
}
Xaml中的用法:

<StackLayout>
        <local:DateTimePicker2></local:DateTimePicker2>
    </StackLayout>


有一个错误-如果你告诉我们具体的错误是什么,这将非常有用。我使用的是android而不是IOS,所以我不确定这是否是导致错误的原因。我得到的错误是严重性代码描述项目文件行抑制状态错误CS1061'EditText'不包含'ResignFirstResponder'的定义,并且找不到可访问的扩展方法'ResignFirstResponder'接受类型为'EditText'的第一个参数是否缺少using指令或程序集引用?有一个错误-如果你告诉我们具体的错误是什么,这将非常有用。我使用的是android而不是IOS,所以我不确定这是否是导致错误的原因。我得到的错误是严重性代码描述项目文件行抑制状态错误CS1061'EditText'不包含'ResignFirstResponder'的定义,并且找不到接受'EditText'类型的第一个参数的可访问扩展方法'ResignFirstResponder'是否缺少using指令或程序集引用?您好工作正常,但我想用xaml调用它。。可能吗?我试着在xaml中调用控件,但无效嗨,谢谢!:只是想澄清一下,我是否必须使用app.xaml.cs中的代码才能工作?因为我的不起作用,因为我只使用xaml调用函数。不,xaml也起作用。为了方便起见,我只是在app.xaml.cs中进行了测试,更新后的xaml中的代码运行良好。我的代码不起作用。。不确定是因为我使用了xmlns:control吗?错误是什么?是否添加了xmlns:local=clr namespace:XamarinDemo.Controls,DateTimePicker类在ContentPage中的位置?您好,这是可行的,但我想在xaml中调用它。。可能吗?我试着在xaml中调用控件,但无效嗨,谢谢!:只是想澄清一下,我是否必须使用app.xaml.cs中的代码才能工作?因为我的不起作用,因为我只使用xaml调用函数。不,xaml也起作用。为了方便起见,我只是在app.xaml.cs中进行了测试,更新后的xaml中的代码运行良好。我的代码不起作用。。不确定是因为我使用了xmlns:control吗?错误是什么?是否添加了xmlns:local=clr namespace:XamarinDemo.Controls,DateTimePicker类在ContentPage中的位置?谢谢,我有一个问题。既然SetBindingproperty,lambda已经过时了,如何进行相应的更改以使其工作?如何使用Nullable DateTime?如何在xaml中使用BindingProperty?如果添加以下标记,则在生成项目错误时会出现错误:找不到DateTime:的属性、BindableProperty或事件,或者值与值之间的类型不匹配property@Serena_datePicker.SetBindingDatePicker.DateProperty,_日期的名称_timePicker.SetBindingTimePicker.TimeProperty,时间的名称@Jiren将BindableProperty设置为public static并删除BindingContext=this行谢谢你,我有一个问题。既然SetBindingproperty,lambda已经过时了,如何进行相应的更改以使其工作?如何使用Nullable DateTime?如何在xaml中使用BindingProperty?如果添加以下标记,则在生成项目错误时会出现错误:找不到DateTime:的属性、BindableProperty或事件,或者值与值之间的类型不匹配property@Serena_datePicker.SetBindingDatePicker.DateProperty,_日期的名称_timePicker.SetBindingTimePicker.TimeProperty,时间的名称@Jiren将BindableProperty设置为public static并删除BindingContext=this行