简体   繁体   English

消耗宁静的Web服务

[英]Consuming a restful web service

I am trying to consume an interface but I am having some difficulty here. 我正在尝试使用一个接口,但是在这里遇到了一些困难。 I am trying to set it to a xamrin list view in behind a content page 我正在尝试将其设置为内容页面后面的xamrin列表视图

public class xxxApiClient : IApi
 {
        readonly string url = "http://localhost:81/ ";
        readonly IHttpService httpService;

        public xxxApiClient(IHttpService httpService)
        {
            this.httpService = httpService;
        }

        public Task<List<JobsList>> GetJobs() => httpService.Get<List<JobsList>>($"{url}JobsLists");
}

How ever I am not to sure how I cosume getjobs correclty I am trying the following 我如何不确定我如何承担getjobs correclty我正在尝试以下

public partial class JobsPage : ContentPage ,xxxWC.Interface.IApi
{
        public xxxWC.Interface.IApi api = new ful;

          public JobsPage ()
        {
            InitializeComponent ();
        }

        private Task SetItemSource()
    .   {
    .       JobListing.ItemsSource =   FuelAp
 }

How do I use the get jobs correctly above in the method setItemSource?. 如何在上面的setItemSource方法中正确使用get作业?

The bit I am having hard time to understand is here. 我很难理解的地方在这里。

How do I call the base GetJobs method I have already created in API Client. 如何调用已经在API客户端中创建的基本GetJobs方法。

      Task<List<JobsList>> IApi.GetJobs()
        {
            throw new NotImplementedException();
        }

        private Task SetItemSource()
     {
            JobListings.ItemsSource =await GetJobs();
    }
}

Edit 2 Ok based on suggestions below I updated My Code as such 根据下面的建议编辑2确定我这样更新了我的代码

    IHttpService httpService;
   xxxApiClient _api = newxxxApiClient(httpService);
      public JobsPage ()
    {
        InitializeComponent ();
    }

    private Task SetItemSource()
 {
        JobListings.ItemsSource =await GetJobs();
}

But i get the below error 但我得到以下错误

Severity Code Description Project File Line Suppression State Error CS0236 A field initializer cannot reference the non-static field, method, or property 'JobsPage.httpService' xxxCallManagmentAppMobile C:\\Work\\xxxCallAppDev\\XamForms\\xxxCallManagmentApp\\xxxCallManagmentAppMobile\\FuelCallManagmentAppMobile\\Views\\JobsPage.xaml.cs 17 Active 严重性代码说明项目文件行抑制状态错误CS0236字段初始化程序无法引用非静态字段,方法或属性'JobsPage.httpService'xxxCallManagmentAppMobile C:\\ Work \\ xxxCallAppDev \\ XamForms \\ xxxCallManagmentApp \\ xxxCallManagmentAppMobile \\ FuelCallManagmentAppMobile \\ Views \\ JobsPage。 xaml.cs 17有效

Can someone explain why 有人可以解释为什么

Edit 3 Ok i got a bit further but still having some issues. 编辑3好吧,我走了一些,但仍然有一些问题。 as the main method is not awaited how do I call set SetItemSource. 由于尚未等待主要方法,我如何调用set SetItemSource。

xxxApiClient _api ;
      public JobsPage ()
    {
        InitializeComponent ()
            SetItemSource();

    }

    private async Task SetItemSource()
 {
        JobListings.ItemsSource = await client.GetJobs();
}

Assuming that IApi has been mapped to xxxApiClient implementation 假设IApi已映射到xxxApiClient实现

Try resolving the service using the DependencyService so that it is available to be used in the view 尝试使用DependencyService解决服务,以便可以在视图中使用它

public partial class JobsPage : ContentPage {
    public readonly IApi client;

    public JobsPage () {
        InitializeComponent ();
        client = DependencyService.Get<IApi>();
    }

    private async Task SetItemSource() {
        JobListing.ItemsSource = await client.GetJobs();
        //...
    }
}

As for calling the SetItemSource , it is async so should be awaited. 至于调用SetItemSource ,它是异步的,因此应等待。 That can't be done in the constructor. 那不能在构造函数中完成。

Consider creating a event that can be raised and its handler used to await the desired behavior. 考虑创建一个可以引发的事件,并将其处理程序用于等待所需的行为。

private event EventHandler loadingData = delegate { };
private async void onLoadingData(object sender, Eventargs args) {
    JobListing.ItemsSource =   await client.GetJobs();
}

Full code 完整代码

public partial class JobsPage : ContentPage {
    public readonly IApi client;

    public JobsPage () {
        InitializeComponent ();
        //resolving client
        client = DependencyService.Get<IApi>();
        //subscribing to event 
        loadingData += onLoadingData;
        //raising event
        loadingData(this, EventArgs.Empty);
    }

    private async Task SetItemSource() {
        JobListing.ItemsSource = await client.GetJobs();
        //...
    }

    private event EventHandler loadingData = delegate { };
    private async void onLoadingData(object sender, Eventargs args) {
        JobListing.ItemsSource =   await client.GetJobs();
    }
}

Although a custom event was created, you could just as easily used on of the event/eventhandler of the view. 尽管创建了自定义事件,但您可以轻松地使用视图的事件/事件处理程序。

All of that code should actually live inside of a view model and then bound to the view in a binding context. 所有这些代码实际上应该存在于视图模型内部,然后在绑定上下文中绑定到视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM