0
在之前的問題中,我問過如何訪問回調線程中的UI元素。我得到了很多很好的答案,其中之一就是實現一個包裝類,如這樣的:UI線程/調度問題(BeginInvoke)
public static class UIThread
{
private static readonly Dispatcher Dispatcher;
static UIThread()
{
Dispatcher = Deployment.Current.Dispatcher;
}
public static void Invoke(Action action)
{
if (Dispatcher.CheckAccess())
{
action.Invoke();
}
else
{
Dispatcher.BeginInvoke(action);
}
}
}
而且你可以通過調用稱這個爲使用
UIThread.Invoke(() => TwitterPost.Text = "hello there");
但是我試圖延長這一以下在我的回調函數
UIThread.Invoke(() => loadUserController(jsonObject));
以下方法:
private void loadUserController(JObject jsonObject)
{
string profile_image_url = (string)jsonObject["profile_image_url"];
string screen_name = (string)jsonObject["screen_name"];
string name = (string)jsonObject["name"];
string location = (string)jsonObject["location"];
int statuses_count = (int)jsonObject["statuses_count"];
if (!string.IsNullOrEmpty(profile_image_url))
{
ProfileImage.Source = new BitmapImage(new Uri("blahblahbalhb.jpg", UriKind.Absolute));
}
// Set the screen name and display name if it differs
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(screen_name))
{
ScreenName.Text = screen_name;
if (!screen_name.Equals(name))
{
_Name.Text = name;
}
}
if (!string.IsNullOrEmpty(location))
{
Location.Text = location;
}
Tweets.Text = statuses_count.ToString() + " Tweets";
}
然後,圖像將不會呈現,直到另一個動作強制重繪(單擊按鈕),但文本控件將被更新。如果在我的回調函數中,我將調用setImageFile(string imageFile),其實現爲:
private void setImageFile(string imageFile)
{
if (this.Dispatcher.CheckAccess())
{
ProfileImage.Source = new BitmapImage(new Uri("fdsfdfdsf.jpg", UriKind.Absolute));
}
else
{
this.Dispatcher.BeginInvoke(new Action<string>(setImageFile), imageFile);
}
}
然後圖像將立即呈現。這是爲什麼發生?調度員的哪些屬性我不完全理解?