I'm trying to register my Service so that I can send the parameter to the next ViewModel.
This is my App.Xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<View.MainPage, MainPageViewModel>();
containerRegistry.Register<IService, Service>();
}
My Interface:
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;
}
}
I'm getting the error from this line im App.Xaml.cs:
containerRegistry.Register<IService, Service>();
Error message:
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
public class Service : IService
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.