无法使用 UpdateLayout() [UWP] 刷新 CalendarView [英] Unable to refresh a CalendarView with UpdateLayout() [UWP]
问题描述
在我的 UWP 应用程序中,如果用户单击按钮,我会尝试刷新 calendarview
上的密度条,但问题是,尽管 calendarView.UpdateLayout();
应该重新运行 CalendarViewDayItemChanging
事件,它仅在第一次加载 CalendarView
时运行.难道我做错了什么?
In my UWP app, I'm trying to refresh the density bars on a calendarview
if the user clicks a button, the problem is that, although calendarView.UpdateLayout();
should re-run the CalendarViewDayItemChanging
event, it runs only when the CalendarView
is loaded the first time. Am I doing something wrong?
public MainPage()
{
this.InitializeComponent();
densityColors.Add(Colors.Green);
}
private void CalendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
{
item = args.Item;
if (item < DateTimeOffset.Now)
{
item.SetDensityColors(densityColors);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
densityColors[0]=Colors.Blue;
calendarView.UpdateLayout();
}
推荐答案
UpdateLayout
不会在这里做任何事情,因为没有 layout 更新和 CalendarViewDayItemChanging
仅在您移动到另一个日历视图时触发.
UpdateLayout
won't do anything here since there's no layout update and CalendarViewDayItemChanging
is only triggered when you move to another calendar view.
在您的情况下,您只需要手动定位所有 CalendarViewDayItems
并在现有视图上更新它们的颜色.
In your case, you just need to manually locate all the CalendarViewDayItems
and update their colors on the existing view.
首先,创建一些Children
扩展方法来帮助检索所有相同类型的子项.
First, create a little Children
extension methods to help retrieve all the children of the same type.
public static class Helper
{
public static List<FrameworkElement> Children(this DependencyObject parent)
{
var list = new List<FrameworkElement>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is FrameworkElement)
{
list.Add(child as FrameworkElement);
}
list.AddRange(Children(child));
}
return list;
}
}
然后,只需在按钮单击处理程序中调用它即可获取所有 CalendarViewDayItem
,循环它们并相应地设置每个颜色.
Then, simply call it in your button click handler to get all CalendarViewDayItem
s, loop them through and set each's color accordingly.
var dayItems = calendarView.Children().OfType<CalendarViewDayItem>();
foreach (var dayItem in dayItems)
{
dayItem.SetDensityColors(densityColors);
}
请注意,您无需担心新的 CalendarViewDayItem
进入视图,因为它们将由您的 CalendarView_CalendarViewDayItemChanging
回调处理.
Note you don't need to worry about new CalendarViewDayItem
s coming into view as they will be handled by your CalendarView_CalendarViewDayItemChanging
callback.
这篇关于无法使用 UpdateLayout() [UWP] 刷新 CalendarView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!