0

我正在创建一个动态支点。我在其中绑定了一个集合以透视 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>

什么问题??

4

1 回答 1

0

我建议您手动创建一个单独的线程并将其分配给函数,它会在完成时更新 UI 线程。使用 Async Await 并不意味着这将在不同的线程上。

http://msdn.microsoft.com/en-us/library/windowsphone/develop/cc221403%28v=vs.105%29.aspx

此链接向您展示如何分配后台工作人员(线程)来完成您的任务

我希望这能帮到您

于 2013-10-01T09:47:34.730 回答