2017-05-14 78 views
-3
  • 我已經定義了一個服務RestService.cs,並試圖從視圖模型調用 服務FirstViewModel.cs
  • 接口調用視圖模型服務如何使用來實現這一IntIRestService.cs

RestService.cs如何通過

namespace SqliteDemo.core.Services 
{ 
    public class RestService : IntIRestService 
    { 
     public async Task<List<People>> GetSalesPeopleAsync() 
     { 
      try 
      { 
       //Declare a Http client 
       var client = new HttpClient(); 
       //Add a Base URl 
       client.BaseAddress = new Uri(Constants.MUrl); 
       //Add the response type 
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
       //Add the API 
       var response = await client.GetAsync("iCodersLab/Custom-ListView-Using-Volley/master/richman.json"); 

       var content = await response.Content.ReadAsStringAsync(); 
       //return content; 
      } 
      catch (Exception ex) 
      { 
       var t = ex.Message; 
      } 

      return null; 
     } 
    } 
} 

FirstViewModel.cs

namespace SqliteDemo.core.ViewModels 
{ 
    public class FirstViewModel 
     : MvxViewModel 
    { 
     public event EventHandler mNetworkClick; 

     public ICommand networkTask 
     { 
      get; 
      set; 
     } 


     public FirstViewModel() 
     { 
      networkTask = new MvxCommand<string>(param => 
      { 
       //Calling Service here 
      }); 
     } 

    } 
} 

IntIRestService.cs

namespace SqliteDemo.core.Services.ServiceInterfeces 
{ 
    public interface IntIRestService 
    { 
     Task<List<People>> GetSalesPeopleAsync(); 
    } 
} 

回答

0

IntIrestService是接口,你不能實例化接口。
您需要實例化實現接口的具體類。

IntIrestService intIrestService = new RestService(); 
+0

謝謝 'intIrestService.GetSalesPeopleAsync();'我可以用它來調用服務,如何獲得控制權交還給視圖模型一旦異步任務完成(在Android中,我們使用監聽器,如何在這裏做同樣的事情) – Devrath