代码之家  ›  专栏  ›  技术社区  ›  KalleP

无法注册数据服务Prism Xamarin表单

  •  0
  • KalleP  · 技术社区  · 7 年前

    我正在尝试注册我的服务,以便将参数发送到下一个ViewModel。

    这是我的App.Xaml.cs

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
    
            containerRegistry.RegisterForNavigation<NavigationPage>();
            containerRegistry.RegisterForNavigation<View.MainPage, MainPageViewModel>();
            containerRegistry.Register<IService, Service>();
        }
    

    public interface IService
    {
        Task<List<TodoItem>> DataAsync();
    }
    

    获取数据的我的服务类:

    public class Service
    {
    
    public List<TodoItem> TodoList { get; private set; }
    HttpClient client;
    
        Service()
        {
            client = new HttpClient();
            client.MaxResponseContentBufferSize = 256000;
        }
    
            public async Task<List<TodoItem>> DataAsync()
            {
    
                TodoList = new List<TodoItem>();
    
    
                var uri = new Uri(string.Format(Constants.RestUrl, string.Empty));
    
                try
                {
                    var response = await client.GetAsync(uri);
                    if (response.IsSuccessStatusCode)
                    {
                        var content = await response.Content.ReadAsStringAsync();
                        TodoList = JsonConvert.DeserializeObject<List<TodoItem>>(content);
                        Debug.WriteLine(content);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(@"ERROR {0}", ex.Message);
                }
    
                return TodoList;
            }
        }
    

    我从这行im App.Xaml.cs获取错误:

    containerRegistry.Register<IService, Service>();
    

    错误消息:

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jason    7 年前

    你的 Service 类需要声明它实现了 IService

    public class Service : IService