0

I am working in windows mobile application and follow MVVM structure.

I have one Method where I have bound "lst_activity" which is Observable collection object.

and that list is return in get method.

But Problem is When I call "List_ActivityType()" this function it is not wait for the response.

How to make getter & Setter property async in c#?

Is there any alternative for the same?

 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)
                {
                } 
            }
RMR
  • 599
  • 3
  • 12
  • 35
  • 1
    See this question: http://stackoverflow.com/questions/6602244/how-to-call-an-async-method-from-a-getter-or-setter – Ofir Nov 17 '16 at 10:56
  • 6
    You shouldn't have such a complex logic inside getter/seter. Consider creating async method that populates data and execute it f.e. in page load. – pwas Nov 17 '16 at 10:57
  • 1
    Read the answer of **Stephen Cleary**: [how-to-call-an-async-method-from-a-getter-or-setter](http://stackoverflow.com/questions/6602244/how-to-call-an-async-method-from-a-getter-or-setter) – mybirthname Nov 17 '16 at 10:57
  • Issue is still not resolve. Main Problem is my property (get property) is bind before getting response and before fill list collection object. That's why error raised. My code is in MVVM patter. How to put async method in Page load? My business login in ViewModel Class – RMR Nov 17 '16 at 12:59

1 Answers1

-1

Instead await,may be you can use Dispatcher.BeginIvoke(()=> {List_ActivityType() }); Basically dispatcher queue's your process.

Vadi
  • 57
  • 4
  • Main Problem is my property (get property) is bind before getting response and before fill list collection object. That's why error raised. – RMR Nov 17 '16 at 12:57