无法注册数据服务 Prism Xamarin Forms [英] Cant register Data Services Prism Xamarin Forms
问题描述
我正在尝试注册我的服务,以便我可以将参数发送到下一个 ViewModel.
I'm trying to register my Service so that I can send the parameter to the next ViewModel.
这是我的 App.Xaml.cs
This is my 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();
}
获取数据的我的服务类:
My Service class that fetches data:
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;
}
}
我从 App.Xaml.cs 这一行收到错误:
I'm getting the error from this line im App.Xaml.cs:
containerRegistry.Register<IService, Service>();
错误信息:
错误 CS0311:类型MyApp.Services.Service"不能用作泛型类型或方法IContainerRegistryExtensions.Register(IContainerRegistry)"中的类型参数TTo".没有从MyApp.Services.Service"到MyApp.Services.IService"的隐式引用转换.(CS0311) (MyApp)
Error CS0311: The type 'MyApp.Services.Service' cannot be used as type parameter 'TTo' in the generic type or method 'IContainerRegistryExtensions.Register(IContainerRegistry)'. There is no implicit reference conversion from 'MyApp.Services.Service' to 'MyApp.Services.IService'. (CS0311) (MyApp)
推荐答案
你的 Service
类需要声明它实现了 IService
your Service
class needs to declare that it implements IService
public class Service : IService
这篇关于无法注册数据服务 Prism Xamarin Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!