如何通过界面调用视图模型中的服务

时间:2017-05-14 05:05:20

标签: c# xamarin mvvmcross

  • 我已经定义了一个服务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();
    }
}

1 个答案:

答案 0 :(得分:0)

IntIrestService是界面,您无法实例化界面 您需要实例化实现接口的具体类。

IntIrestService intIrestService = new RestService();