Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过接口调用视图模型中的服务 我已经定义了一个服务RestService.cs,并尝试调用 来自视图模型的服务FirstViewModel.cs 如何使用IntIRestService.cs_C#_Xamarin_Mvvmcross - Fatal编程技术网

C# 如何通过接口调用视图模型中的服务 我已经定义了一个服务RestService.cs,并尝试调用 来自视图模型的服务FirstViewModel.cs 如何使用IntIRestService.cs

C# 如何通过接口调用视图模型中的服务 我已经定义了一个服务RestService.cs,并尝试调用 来自视图模型的服务FirstViewModel.cs 如何使用IntIRestService.cs,c#,xamarin,mvvmcross,C#,Xamarin,Mvvmcross,RestService.cs namespace SqliteDemo.core.Services { public class RestService : IntIRestService { public async Task<List<People>> GetSalesPeopleAsync() { try { //Declare a

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;
        }
    }
}
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
            });
        }

    }
}
namespace SqliteDemo.core.Services.ServiceInterfeces
{
    public interface IntIRestService
    {
        Task<List<People>> GetSalesPeopleAsync();
    }
}
名称空间SqliteDemo.core.Services
{
公共类RestService:IntIRestService
{
公共异步任务GetSalesoperEasync()
{
尝试
{   
//声明一个Http客户端
var client=新的HttpClient();
//添加一个基本URl
client.BaseAddress=新Uri(Constants.MUrl);
//添加响应类型
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
//添加API
var response=await client.GetAsync(“使用Volley/master/richman.json的iCodersLab/Custom ListView”);
var content=await response.content.ReadAsStringAsync();
//返回内容;
}
捕获(例外情况除外)
{
var t=ex.消息;
}
返回null;
}
}
}
FirstViewModel.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;
        }
    }
}
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
            });
        }

    }
}
namespace SqliteDemo.core.Services.ServiceInterfeces
{
    public interface IntIRestService
    {
        Task<List<People>> GetSalesPeopleAsync();
    }
}
名称空间SqliteDemo.core.ViewModels
{
公共类第一视图模型
:MvxViewModel
{
公共事件事件处理程序mNetworkClick;
公共ICommand网络任务
{
得到;
设置
}
公共FirstViewModel()
{
networkTask=新的MvxCommand(参数=>
{
//这里的呼叫服务
});
}
}
}
IntIRestService.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;
        }
    }
}
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
            });
        }

    }
}
namespace SqliteDemo.core.Services.ServiceInterfeces
{
    public interface IntIRestService
    {
        Task<List<People>> GetSalesPeopleAsync();
    }
}
名称空间SqliteDemo.core.Services.serviceinfereces
{
公共接口服务
{
任务GetSalesoperEasync();
}
}

IntIrestService是接口,您不能实例化接口。
您需要实例化实现接口的具体类

IntIrestService intIrestService = new RestService();

谢谢,
intIrestService.GetSalesPeopleAsync()我可以用来调用服务,如何在异步任务完成后将控件返回到视图模型(在android中,我们使用侦听器,这里如何执行相同的操作)