我正在尝试注册我的服务,以便可以将参数发送到下一个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();
}
获取数据的我的Service类:
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的这一行得到错误:
containerRegistry.Register<IService, Service>();
错误消息:
错误CS0311:类型'MyApp.Services.Service'不能用作通用类型或方法'IContainerRegistryExtensions.Register(IContainerRegistry)'中的类型参数'TTo'。没有从“ MyApp.Services.Service”到“ MyApp.Services.IService”的隐式引用转换。 (CS0311)(MyApp)
答案 0 :(得分:1)
您的Service
类需要声明它实现了IService
public class Service : IService