在几个在线示例中,我发现了这一点:
public partial class ForecastPage : PhoneApplicationPage
{
Forecast forecast;
public ForecastPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// code here
}
}
但在其他人中我发现 Load
事件的用法如下
public partial class Person : PhoneApplicationPage
{
private PersonViewModel _ViewModel;
public Person()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(SearchView_Loaded);
}
void SearchView_Loaded(object sender, RoutedEventArgs e)
{
// code here
}
}
我知道 OnNavigatedTo
在 Load
事件之前触发,但两者都在 UI 绘制到手机之前触发,所以我的问题是 有什么优势吗 正在使用另一种方法吗?
最佳答案
我不同意 Tigran。
public View()
{
InitializeComponent();
personList.ItemsSource = PersonDataSource.CreateList(100);
Loaded += (sender, args) => Debug.WriteLine("Loaded");
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Debug.WriteLine("Navigated");
}
向前向后跳时,输出为
Navigated Loaded Navigated Loaded Navigated Loaded
因此,OnNavigated
在页面导航 完成时调用,但在加载页面控件之前(期间)调用,而 Loaded
在以下时间调用页面已准备就绪,所有控件都已加载。
关于c# - OnNavigatedTo 与 Load 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9403103/