[英]UI hangs when adding large data in pivot item for WP8
我正在創建一個動態樞紐。 在其中綁定了一個集合,以透視ItemSouce。 觸發selectionchange事件時,我正在調用一個過程,該過程花費一些時間並更新ObservableCollection,從而更新UI。 我正在使用異步並等待,但是應用程序UI仍然掛起。 讓我知道是什么問題。 尋找一個非常快速的答復。
碼:
private void CraetePivotItems()
{
for (int count = 0; count < 100; count++)
{
EntityDetail item = new EntityDetail();
item.HeaderTitle = "Header " + count;
item.Name = string.Empty;
this.Detaildata.Add(item);
}
}
private async Task<string> CreateUserControlForPivotItem(int selectedIndex)
{
for (int count = 0; count < 1000000000; count++)
{
}
switch (selectedIndex)
{
case 0:
return "Item 1";
case 1:
return "Item 2";
case 2:
return "Item 3";
default:
return "Item N";
}
}
private void pvtItmCities_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
CraetePivotItems();
this.pvtItmCities.ItemsSource = this.Detaildata;
}
private async void pvtItmCities_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender != null)
{
////// Create the user control for the selected pivot item
string pivotItemContentControl = await CreateUserControlForPivotItem(((Pivot)sender).SelectedIndex);
(this.Detaildata[((Pivot)sender).SelectedIndex] as EntityDetail).Name = pivotItemContentControl;
//((System.Windows.Controls.ContentControl)((sender as Pivot).SelectedItem)).Content = pivotItemContentControl;
}
}
類
internal class EntityDetail : INotifyPropertyChanged
{
private string headerTitle = String.Empty;
public string HeaderTitle
{
get
{
return this.headerTitle;
}
set
{
if (value != this.headerTitle)
{
this.headerTitle = value;
NotifyPropertyChanged();
}
}
}
private string name = String.Empty;
public string Name
{
get
{
return this.name;
}
set
{
if (value != this.name)
{
this.name = value;
NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
XAML:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="HeaderTemplateSample">
<TextBlock Text="{Binding HeaderTitle}" Foreground="White"/>
</DataTemplate>
<DataTemplate x:Key="ItemTemplateSample">
<local:PivotItem1Content Foreground="White"/>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<phone:Pivot x:Name="pvtItmCities"
HeaderTemplate="{StaticResource HeaderTemplateSample}"
ItemTemplate="{StaticResource ItemTemplateSample}"
SelectionChanged="pvtItmCities_SelectionChanged" Loaded="pvtItmCities_Loaded" Title="pivot demo" LoadingPivotItem="OnLoadingPivotItem">
</phone:Pivot>
有什么問題??
我建議您手動創建一個單獨的線程並將其分配給該函數,它會在完成時更新UI線程。 使用Async Await並不意味着這將在另一個線程上。
http://msdn.microsoft.com/zh-CN/library/windowsphone/develop/cc221403%28v=vs.105%29.aspx
該鏈接向您展示如何分配后台工作人員(線程)以完成任務
我希望這能幫到您
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.