我正在windows mobile應用程序中工作,並遵循MVVM結構。如何在c#中使getter&Setter屬性異步?
我有一個方法,我綁定了「lst_activity」,它是Observable集合對象。
並且該列表在get方法中返回。
但問題是當我調用「List_ActivityType()」這個函數時,它不會等待響應。
如何讓C#的getter & setter屬性異步?
有沒有相同的選擇?
ObservableCollection<Activity_Type> lst_activity = new ObservableCollection<Activity_Type>();
public ObservableCollection<Activity_Type> Get_ActivityTypeData
{
get
{
await List_ActivityType();
return lst_activity;
}
}
public async Task List_ActivityType()
{
try
{
if (NetworkInterface.GetIsNetworkAvailable() == true)
{
IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;
if (setting.Contains("UserLogin_Code"))
{
string user_code = Convert.ToString(setting["UserLogin_Code"]);
if (!string.IsNullOrEmpty(user_code))
{
ShowProgressBar();
string str = ServiceURI.Get_Activity;
Uri geturi = new Uri(string.Format(str));
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, geturi);
var response1 = await CommandClass.GetResponse<SilverHrms.Data.JsonPraserClasses.Activity_Type_RootObject>(request);
lst_activity.clear();
if (response1 != null && response1.Flag == true)
{
for (int i = 0; i < response1.lstListComboData.Count; i++)
{
if (i == 0)
{
lst_activity.Add(new Activity_Type
{
Text = "--Select--",
Value = ""
});
}
lst_activity.Add(response1.lstListComboData.ElementAt(i));
}
}
else
{
//Show_msg("Error", response1.Message);
}
CloseProgressBar();
}
}
else
{
App.RootFrame.Navigate(new Uri(PageNavigationURI.Login, UriKind.RelativeOrAbsolute));
}
}
else
{
if (!App.OnOff)
{
CloseProgressBar();
CommandClass.NetworkErrorMsg();
}
}
}
catch (Exception ex)
{
}
}
看到這個問題: http://stackoverflow.com/questions/6602244/how-to-call-an-async-method-from-a -getter-or-setter – Ofir
你不應該在getter/seter裏有這麼複雜的邏輯。考慮創建填充數據並執行它的異步方法。在頁面加載。 – pwas
閱讀** Stephen Cleary **的答案:[如何調用異步方法從getter或setter](http://stackoverflow.com/questions/6602244/how-從一個獲取或設置者調用異步方法) – mybirthname