2012-01-03 42 views
0

我正在編寫一個WP7應用程序,該應用程序在其頁面的OnNavigatedTo()覆蓋處理程序中的主UI線程上恢復其狀態。在這個處理程序中,它將頁面的列表框ItemsSource屬性設置爲反序列化的ObservableCollection數據項。反序列化來自獨立存儲,速度很快,不會掛起線程。什麼時候應該設置ListBox的ItemsSource屬性?

但是,當頁面出現時,列表框爲空。在設置斷點並檢查頁面狀態時,Items屬性正確填充且非空。

如果我遲疑ItemsSource屬性的設置,像這樣:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 

    int delayMs = 100; // Why 100 ? 
    runDelayedDispatch(Deployment.Current.Dispatcher, 
    delayMs, delegate() 
    { 
    deserializeFromStorageAndSetItemsSource(); 
    }); 
} 

... 

// Does a BeginInvoke() after the specified delay. 
public static void runDelayedDispatch(Dispatcher dispatcher, 
    int delayInMilliseconds, Action action) 
{ 
    Action dispatcherAction = delegate() 
    { 
    dispatcher.BeginInvoke(action); 
    }; 

    BackgroundWorker worker = new BackgroundWorker(); 
    worker.DoWork += (s, e) => Thread.Sleep(delayInMilliseconds); 
    worker.RunWorkerCompleted += (s, e) => dispatcherAction.Invoke(); 
    worker.RunWorkerAsync(); 
} 

然後一切工作正常。

我在做什麼錯?我應該從不同的處理程序或稍後在應用程序生命週期中的隔離存儲中讀取數據嗎?

文章有關的應用程序生命週期還沒有有何啓示這個:(

http://msdn.microsoft.com/en-us/library/system.windows.controls.page.onnavigatedto(v=vs.95).aspx http://msdn.microsoft.com/en-us/library/cc838245(v=vs.95).aspx http://windowsphonegeek.com/articles/WP7-Application-Lifecycle-and-Tombstoning http://visualstudiomagazine.com/articles/2011/06/01/pcmob_app-lifecycle.aspx

謝謝!

+0

runDelayedDispatch是一個很酷的功能 - 不過考慮將delayInMilliseconds改爲TimeSpan。 – 2012-01-03 06:17:10

+0

有什麼好處? :) – swinefeaster 2012-01-03 06:28:24

+0

順便說一句,我從這裏得到它:http://stackoverflow.com/questions/4726239/easy-way-to-excecute-method-after-a-given-delay – swinefeaster 2012-01-03 06:30:32

回答

0

所有你需要做的就是刷新使用UI INotifyPropertyChanged

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 

    int delayMs = 100; // Why 100 ? 
    runDelayedDispatch(Deployment.Current.Dispatcher, 
    delayMs, delegate() 
    { 
    deserializeFromStorageAndSetItemsSource(); 
    }); 

    NotifyPropertyChange("UI"); 
} 

public static void runDelayedDispatch(Dispatcher dispatcher, 
    int delayInMilliseconds, Action action) 
{ 
    Action dispatcherAction = delegate() 
    { 
    dispatcher.BeginInvoke(action); 
    }; 

    BackgroundWorker worker = new BackgroundWorker(); 
    worker.DoWork += (s, e) => Thread.Sleep(delayInMilliseconds); 
    worker.RunWorkerCompleted += (s, e) => dispatcherAction.Invoke(); 
    worker.RunWorkerAsync(); 

    NotifyPropertyChanged("UI"); 
} 

public event PropertyChangedEventHandler PropertyChanged; 

private void NotifyPropertyChanged(String info) 
{ 
    if (PropertyChanged != null) 
    { 
    PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 
+0

謝謝。你能否重新格式化你的代碼片段?我正在嘗試... – swinefeaster 2012-01-03 06:28:46

+0

什麼是「DisplayItem」?它從哪裏來的? – swinefeaster 2012-01-03 06:29:16

+0

那是在我的代碼對不起 – Apoorva 2012-01-03 06:47:04

1

聽起來好像您的數據上下文設置不正確,綁定設置不正確,或者您的INotifyPropertyChanged未觸發。

PS:我認爲你應該改寫你的問題,以擺脫你的嘗試繞過這個問題的延遲 - 這是給你的答案沿線,你不想要的,我敢肯定,這是不需要的在所有。相反,把全部RELEVANT代碼爲你的問題的列表和頁面,所以我們可以看到你在做什麼。

0

我找到了,謝謝大家!我回到基礎知識並從一個簡單的骨骼列表框中測試了這個場景,並且它在OnNavigatedTo()中工作時沒有任何問題。

罪魁禍首是當DataContext發生變化以便將列表框項目的寬度綁定到列表框寬度時,我正在接收通知,但我可以通過掛接到LayoutUpdated事件來輕鬆完成此操作。

這裏是我刪除的罪魁禍首代碼: http://www.codeproject.com/Articles/38559/Silverlight-DataContext-Changed-Event

由於球員和抱歉浪費你的時間:|

相關問題