首先,對於問題的長度感到抱歉。即使NotifyPropertyChanged正常工作,ProgressBar在屏幕上也不會更新
我正在更新Silverlight for Windows Phone 7應用程序,該應用程序當前將數據存儲在多個XML文件中。我正在更新應用程序以使用CompactSQL DB,並且在安裝新版本後首次運行時必須將XML文件中的數據遷移到數據庫中。
我想要一個進度條(IsIndeterminate = false)向用戶顯示每個文件遷移到數據庫時的進度(因爲該操作最多可能需要2分鐘)。
問題是,儘管NotifyProperyChanged事件觸發並正確更新了欄的值,進度條並未更新(甚至不顯示)。當我在XAML中設置值時,它看起來很好(靜態 - 但至少它是在屏幕上繪製的)。
我不知道爲什麼進度條根本沒有顯示在設備上。
我INotifyChanged設置
private int migrateCount;
public int MigrateCount //Prop used for ProgressBar.Value
{
get
{
return this.migrateCount;
}
set
{
this.migrateCount = value;
NotifyPropertyChanged("MigrateCount");
}
}
public int MigrateTotal { get; set; } //Prop used for ProgressBar.Maximum
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
void MainPage_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "MigrateCount")
{
ProBar.Maximum = MigrateTotal; //ProBar is the name of my ProgressBar
ProBar.Value = MigrateCount;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public MainPage()
{
this.PropertyChanged += new PropertyChangedEventHandler(MainPage_PropertyChanged);
...
}
在調用加載的MigrateDB方法,而不是因爲的OnNavigatedTo終止OS任何應用程序時間過長加載。
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
string[] dirs = store.GetDirectoryNames();
if (dirs.Length > 1) //Checks to see if there's any data to migrate
{
MigrateDB();
LoadData(); //Loads data from the DB once it's populated
}
}
該操作需要很長時間。每分鐘大約有100個XML文件,我希望用戶有30到300個文件。
private void MigrateDB()
{
string[] DirList;
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
DirList = store.GetDirectoryNames("*");
MigrateTotal = DirList.Length - 1; // -1 to account for the "Shared" Dir
foreach(...)
{
... Does lots of IsoStore operations/XML serialising and DB updating
MigrateCount++;
}
}
...
}
謝謝,我想這可能去過的是與線程,但是我是在這一切是如何工作的損失。我最終修復了BackgroundWorker類,但是如果沒有你的幫助,我不會找到它。 :) – Styff 2012-04-03 12:35:23