.net 如何在MAUI的编辑器中打印从日期选择器收集的日期

x33g5p2x  于 2022-12-14  发布在  .NET
关注(0)|答案(1)|浏览(212)

如何打印从编辑器中的日期选择器或MAUI中的标签中收集的日期?我尝试打印日期,但它没有显示任何内容。

<DatePicker  x:Name="mDatePicker" Style="{StaticResource dateStyle}" MinimumDate="01/01/2020" MaximumDate="12/31/2022" Date="01/01/2022" />

                <Label Style="{StaticResource label1Style}" Text="Location" HorizontalOptions="Start" VerticalOptions="Start" />


                <Picker x:Name="picker" Title="Select a Company" Style="{StaticResource comboStyle}">
                    <Picker.ItemsSource>
                        <x:Array Type="{x:Type x:String}">
                            <x:String>Baboon</x:String>
                            <x:String>Capuchin Monkey</x:String>
                            <x:String>Blue Monkey</x:String>
                            <x:String>Squirrel Monkey</x:String>
                            <x:String>Golden Lion Tamarin</x:String>
                            <x:String>Howler Monkey</x:String>
                            <x:String>Japanese Macaque</x:String>
                        </x:Array>
                    </Picker.ItemsSource>
                </Picker>

                <Button HeightRequest="60" WidthRequest="300" Grid.Row="3" x:Name="inbtn" Text="In" Style="{StaticResource inButtonStyle}" SemanticProperties.Hint="navigate when clicked" Clicked="OnInClicked" HorizontalOptions="Center" />

                <Button HeightRequest="60" WidthRequest="300" Grid.Row="3" x:Name="outbtn" Text="Out" Style="{StaticResource outButtonStyle}" SemanticProperties.Hint="navigate when clicked" Clicked="OnOutClicked" HorizontalOptions="Center" />

            </VerticalStackLayout>
v09wglhw

v09wglhw1#

您可以尝试将Reference设置为DatePicker,并将path设置为Date
请参考以下代码:

<DatePicker x:Name="mDatePicker"   MinimumDate="01/01/2020" MaximumDate="12/31/2022" Date="01/01/2022" DateSelected="DatePicker_DateSelected" /> 

        <Entry Placeholder="text" Text="{Binding Source={x:Reference mDatePicker}, Path=Date }"   />

注:

如果使用TimePicker,则可以将Path设置为Time

<TimePicker x:Name="timePicker" />
    <Label Text="{Binding Source={x:Reference timePicker},
                          Path=Time,
                          StringFormat='The TimeSpan is {0:c}'}" />

更新日期:

从你发布的新帖子中,我发现你想把你的DesktopStartupPage.xaml.cs中定义的变量time1绑定到一个Entry上,如下所示:

public DateTime time1 { get; set; }

<Entry Placeholder="text" Text="{Binding Source={Binding time1},
                              Path=Time,
                              StringFormat='The TimeSpan is {0}'}" Style="{StaticResource tableContStyle}" Grid.Row="1" Grid.Column="0"

/〉
然后我们需要直接绑定变量,如果你想在改变变量的值后更新UI,你可以为你的页面实现接口INotifyPropertyChanged
请参考以下代码:

主页.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiScrollApp1212.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="10"
            VerticalOptions="Center">

            <DatePicker x:Name="mDatePicker"   MinimumDate="01/01/2020" MaximumDate="12/31/2022" Date="01/01/2022" DateSelected="DatePicker_DateSelected" />

            <Entry Placeholder="text" Text="{Binding Source={x:Reference mDatePicker}, Path=Date, StringFormat='The TimeSpan is {0:c}' }"   />

            <Button HeightRequest="60" WidthRequest="300"  x:Name="inbtn" Text="In"   Clicked="OnInClicked" HorizontalOptions="Center" />

            <!--<Entry Placeholder="text" Text="{Binding Source={Binding Time1},  
                              Path=Date,
                              StringFormat='The TimeSpan is {0}'}"  />-->

            <Entry Placeholder="text" Text="{Binding Time1, StringFormat='The TimeSpan is {0}'}"  />


        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

主页面.xaml.cs

public partial class MainPage : ContentPage, INotifyPropertyChanged 
{
    //public DateTime time1 { get; set; }

    DateTime _time1;
    public DateTime Time1
    {
        get => _time1;
        set => SetProperty(ref _time1, value);
    }

    public MainPage()
      {
            InitializeComponent();

        Time1 = DateTime.Now;

        this.BindingContext = this;
    }


    private void DatePicker_DateSelected(object sender, DateChangedEventArgs e)
    {
        Time1 = mDatePicker.Date;
    }
    private void OnInClicked(object sender, EventArgs e)
    {
        //Time1 = DateTime.Now;
        Console.WriteLine(Time1);

       // GetCachedLocation();
    }


    bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (Object.Equals(storage, value))
            return false;

        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

相关问题