【问题标题】:Xamarin Calender Selected DateXamarin 日历选择日期
【发布时间】:2021-10-29 09:01:02
【问题描述】:

我使用 Xamarin Calendar 我的项目。

https://github.com/lilcodelab/Xamarin.Plugin.Calendar

我希望当我在日历上选择一天时自动加载列表视图。不幸的是, selectedDate 没有加载。在按钮的帮助下,我选择了选定的日期并自己加载列表视图。没有按钮。你有什么建议吗?

            <controls:Calendar x:Name="xxcalendar"
                               VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

            </controls:Calendar>
            
            <Button Margin="50,0,50,0" x:Name="xxbutton" Text="Listview_Loading" CornerRadius="10" Style="{DynamicResource ButtonStyle}" Clicked="xxbutton_Clicked"/>

            <ListView>...........</ListView>

【问题讨论】:

  • "selectedDate 未加载" - 这是什么意思?请发布相关代码并准确描述正在发生或未发生的事情
  • 您使用的是 MVVM 还是 MVC?请附上代码 sn-ps 否则我们帮不了你!
  • 我已经添加了相关代码。

标签: xaml listview xamarin xamarin.forms calendar


【解决方案1】:
  1. 实现了点击日期显示当天的事件。

  2. 已实现获取selectedDate信息。

这里是 xaml 页面:

<StackLayout>
    <controls:Calendar x:Name="ca"
        Events="{Binding Events}">
        <controls:Calendar.EventTemplate>
            <DataTemplate>
                <StackLayout
                Padding="15,0,0,0">
                    <Label
                        Text="{Binding Name}"
                        FontAttributes="Bold"
                        FontSize="Medium" />
                    <Label
                        Text="{Binding Description}"
                        FontSize="Small"
                        LineBreakMode="WordWrap" />
                </StackLayout>
            </DataTemplate>
        </controls:Calendar.EventTemplate>
    </controls:Calendar>
    <Label Text="{Binding Source={x:Reference ca},Path=SelectedDate}"></Label>
</StackLayout>

这是cs页面:

public EventCollection Events { get; set; }
public MainPage()
{
    InitializeComponent();
    Events = new EventCollection
    {
        [DateTime.Now] = new List<EventModel>
        {
            new EventModel { Name = "Cool event1", Description = "This is Cool event1's description!" },
            new EventModel { Name = "Cool event2", Description = "This is Cool event2's description!" }
        },
        [DateTime.Now.AddDays(5)] = new List<EventModel>
        {
            new EventModel { Name = "Cool event3", Description = "This is Cool event3's description!" },
            new EventModel { Name = "Cool event4", Description = "This is Cool event4's description!" }
        },
        [DateTime.Now.AddDays(-3)] = new List<EventModel>
        {
            new EventModel { Name = "Cool event5", Description = "This is Cool event5's description!" }
        },
        [new DateTime(2020, 3, 16)] = new List<EventModel>
        {
            new EventModel { Name = "Cool event6", Description = "This is Cool event6's description!" }
        }
    };
    BindingContext = this;
}

截图如下:

【讨论】:

  • 感谢您的回答。我在日历下添加了一个列表视图。借助按钮根据所选日期加载列表视图。我可以为选定的日期做些什么来触发列表视图?
  • 根据我帖子里的代码,我在xmal页面上使用了x:Name="ca",你只需要使用ca.SelectedDate;在cs页面获取时间,然后查询事件。
  • 使用日期时间 dateTime = (DateTime)ca.SelectedDate;在按钮的 Clicked 方法中获取您选择的日期。 “ca”是代码中的“xxcalendar”
  • 这就是问题所在。我不想使用按钮。我在你的日历中找不到任何适合我的活动。我使用按钮得到了我想要的结果。
  • 不管怎么做,都需要触发事件。单击日期时,我没有看到此工具触发事件。
猜你喜欢
  • 2019-10-06
  • 2013-03-30
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多