无法注册数据服务棱镜 Xamarin 表格
Cant register Data Services Prism Xamarin Forms
我正在尝试注册我的服务,以便我可以将参数发送到下一个 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();
}
我的服务 class 获取数据:
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>();
错误信息:
错误 CS0311:类型 'MyApp.Services.Service' 不能用作泛型类型或方法 'IContainerRegistryExtensions.Register(IContainerRegistry)' 中的类型参数 'TTo'。没有从 'MyApp.Services.Service' 到 'MyApp.Services.IService' 的隐式引用转换。 (CS0311) (MyApp)
您的 Service
class 需要声明它实现了 IService
public class Service : IService
我正在尝试注册我的服务,以便我可以将参数发送到下一个 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();
}
我的服务 class 获取数据:
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>();
错误信息:
错误 CS0311:类型 'MyApp.Services.Service' 不能用作泛型类型或方法 'IContainerRegistryExtensions.Register(IContainerRegistry)' 中的类型参数 'TTo'。没有从 'MyApp.Services.Service' 到 'MyApp.Services.IService' 的隐式引用转换。 (CS0311) (MyApp)
您的 Service
class 需要声明它实现了 IService
public class Service : IService