如何使Windows运行时页面刷新,当它进入前台

本文关键字:前台 刷新 Windows 何使 运行时 | 更新日期: 2023-09-27 18:12:26

我想让Windows运行时页面刷新/重新加载每次它去前台。我曾尝试向Loaded事件添加处理程序,但Loaded事件仅在页面首次加载时触发,这与HTML的DOMContentLoaded事件不同。正确的做法是什么?

谢谢。

public sealed partial class MainPage : Page {
    public MainPage() {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Disabled;
        this.Loaded += (s, e) => {
            Debug.WriteLine("Loaded at " + DateTime.Now.ToString());
            // Code that should be executed when the page goes to the foreground each time
        };
    }
}

如何使Windows运行时页面刷新,当它进入前台

OnNavigatedTo方法(如您在问题中放置的MSDN页面上记录的)将在每次页面导航到时被调用。

您可以使用NavigationEventArgs来确定发生了什么样的导航并适当地处理它。在您的示例中,您将添加代码来刷新页面内容。

正如Romasz指出的那样,当您的应用程序恢复时,这将不会帮助您在WinRT XAML堆栈中,因为在这种情况下页面不会调用OnNavigatedTo

你必须在你的应用程序中检测应用程序的简历,并发送消息到你的页面(或它的数据源/视图模型)刷新。

请注意应用程序的resume/suspend指南(参见MSDN)。