简体   繁体   English

Windows Phone:“手动”刷新绑定的列表框

[英]Windows Phone: refresh a binded listbox “manually”

the question seems to be easy, but I didn't find responses to this question. 这个问题似乎很简单,但是我没有找到对此问题的答复。

I have my list of items working perfectly, binded to my MVVM. 我有工作正常的项目列表,已绑定到我的MVVM。 When I update an element, all is coordinated well, changes are reflected, etc. 当我更新一个元素时,所有元素都可以很好地协调,更改会得到反映等。

One of my fields is calculated depending on the current day. 我的字段之一是根据当天计算的。 So, if the user press HOME and exits the App, and tomorrow he comes back, the list is not refreshed, it is showing the previous day data. 因此,如果用户按下HOME并退出该应用程序,并且明天他回来,则该列表不会刷新,而是显示前一天的数据。

To resolve this, I thought in use the OnNavigatedTo and OnNavigatedFrom events, saving the "entrance" day at start and compare it with the current day in the OnNavigatedTo event (which is fired when resuming the App). 为了解决此问题,我认为在使用OnNavigatedToOnNavigatedFrom事件时,请在开始时保存“进入”日期,并将其与OnNavigatedTo事件中的当前日期进行比较(在恢复应用程序时触发)。 Detecting this day change, I could refresh the list explicity. 检测到这一天的变化,我可以明确地刷新列表。

The question is, how I refresh the list? 问题是,如何刷新列表? Or maybe I'm complicating the things a bit and there is a better way to do this. 也许我正在使事情复杂化,并且有更好的方法可以做到这一点。

EDIT: Final solution. 编辑:最终解决方案。

For those who need the same functionality, here is the solution I found: 对于那些需要相同功能的人,这是我发现的解决方案:

    // Declare this var in the MainPage class
    // Holds the starting app day. If when going back to this page it has changed, refresh the list
    private DateTime loadDate;

    // Save the current day. If when going back to here it has changed, refresh the list
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        loadDate = DateTime.Today;
    }

    // Read the current day and compare with saved. If when going back to here it has changed, refresh the list
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        // Read the current day and compare
        if (loadDate != DateTime.Today)
        {
            // The day has changed. Loop the list to refresh every item
            foreach (Item item in listBoxControl.Items)
            {
                item.CalculateMyOwnFieldNotBindedToDB();
            }
        }
    }

The problem is if you raise property change on a List without actually changing the List, it will be actually ignored because the view will detect that the List object has actually not changed. 问题是,如果在列表上引发属性更改而没有实际更改列表,则该视图实际上将被忽略,因为视图将检测到列表对象实际上并未更改。 One workaround for that could be to just set the List to null and then set it back to the original List. 一种解决方法是将List设置为null,然后再将其设置回原始List。
Another solution would be to just loop through the items in the List and Raise a property change just on your date field like this it will not require the whole list to be refreshed just the actual property. 另一个解决方案是仅遍历列表中的项目,并仅在您的日期字段上引发属性更改,就像这样,它不需要仅刷新实际属性即可刷新整个列表。

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

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