简体   繁体   中英

Windows Phone update binding from background thread

In my Windows Phone 8 Application I have Listbox below

<ListBox x:Name="ListBox1"  ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock  Text="{Binding snippet.DownloadPercentage}" 
TextWrapping="Wrap"
FontFamily="Portable User Interface"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>

I am downloading file asyn and would like to give progress percentage to UI like below; but it does not update UI. It shows always 0 which is initial int value. If I access DownloadPercentage property at main thread the it updates with no problem.

 private void ClientOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs downloadProgressChangedEventArgs)
        {
            // not working
            item.snippet.DownloadPercentage = progressPercentage;

            // not working      
            Dispatcher.BeginInvoke(delegate {
               item.snippet.DownloadPercentage = progressPercentage;

            });    
            // not working
            ProgressChangedEventHandler workerProgressChanged = delegate {
                item.snippet.DownloadPercentage = progressPercentage;
            };
            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.ProgressChanged += workerProgressChanged;
            worker.ReportProgress(progressPercentage);

            // WORKING!
            #region ProgressIndicator
            _progressIndicator.Text = string.Format("Downloading ({0}%) {1}", progressPercentage, item.snippet.Title);
            _progressIndicator.IsVisible = true;
            _progressIndicator.IsIndeterminate = true;
            SystemTray.SetProgressIndicator(this, _progressIndicator);
            #endregion

        }

what can I do?

Solution; after @DecadeMoon's hint I had to implement INotifyPropertyChanged over Snippet class.

public class Snippet : INotifyPropertyChanged
    { 
 [JsonProperty("downloadPercentage")]
        public int DownloadPercentage
        {
            get { return _downloadPercentage; }
            set
            {
                _downloadPercentage = value;
                RaisePropertyChanged("DownloadPercentage");
            }
        }
 public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

The object whose property you have bound to in the view must implement INotifyPropertyChanged . In your case, you're binding to snippet.DownloadPercentage , therefore the snippet class must implement INotifyPropertyChanged and must raise the PropertyChanged event in the setter of the DownloadPercentage property.

You must also make sure that you only modify the DownloadPercentage property from the UI thread, otherwise you'll get an exception if modified from another thread. This is generally done by using the dispatcher:

Dispatcher.BeginInvoke(() =>
{
    item.snippet.DownloadPercentage = progressPercentage;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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