在 ViewModel 构造函数和导航中添加 Api 接口停止工作 Prism
Adding Api interface in ViewModel constructor and navigation stops working Prism
我正在为 Xamarin Forms 使用 VS 17。我在我的 Xamarin.Forms 应用程序中设置了 Prism,我刚刚添加了对我的 Api 界面的引用(在 ViewModel Constructor 中),它使应用程序停止导航到第二页。我需要这样做才能传递参数等。我遵循了本指南:
https://blog.qmatteoq.com/prism-for-xamarin-forms-basic-navigation-and-dependency-injection-part-2/
这是我让导航停止工作的方法:
private readonly IService _Service;
private ObservableCollection<TodoItem> _topSeries;
public ObservableCollection<TodoItem> TopSeries
{
get { return _topSeries; }
set { SetProperty(ref _topSeries, value); }
}
这是构造函数:
public SecondPageViewModel(IService Service, INavigationService navigationService)
{
_Service = Service;
_navigationService = navigationService;
}
因为我添加了上面的代码,所以我什至无法访问上面的视图模型。我试图在 DelegateCommand 上设置断点(在第一个 ViewModel 上),但它只是在 InitializeComponent() 之后停止;然后什么也没有发生。没有错误信息!谢谢!
更新:
我的服务 class 获取数据:
public class Service : IService
{
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
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<View.MainPage, MainPageViewModel>();
containerRegistry.RegisterForNavigation<View.SecondPage, SecondPageViewModel>();
containerRegistry.Register<IService, Service>();
}
我的界面:
public interface IService
{
Task<List<TodoItem>> DataAsync();
}
这是我导航的方式(从列表视图中单击):
private EventItem _selectedEvent { get; set; }
public EventItem SelectedEvent
{
get { return _selectedEvent; }
set
{
if (_selectedEvent != value)
{
if (Device.RuntimePlatform == Device.iOS)
{
_selectedEvent = null;
}
else
{
_selectedEvent = value;
}
NavigationParameters navParams = new NavigationParameters();
navParams.Add("PassedValue", _todoItem.name);
_navigationService.NavigateAsync("SecondPage", navParams);
}
}
}
编辑:
当我在没有 Api 服务代码的情况下进行调试时,命令会将我带到新视图模型中的新构造函数。使用代码,它不会到达构造函数。
根据您的代码,您声明了这样的构造函数:
Service()
{
// ...
}
您没有设置访问修饰符,因此默认为internal
。这是定义:
Internal types or members are accessible only within files in the same
assembly.
很可能您在另一个程序集中声明了 Service.cs
,而 Prism 无法访问其构造函数。
您的导航不起作用,因为依赖项注入失败。要修复它,只需将您的访问修饰符更改为 public
:
public Service()
{
// ...
}
我正在为 Xamarin Forms 使用 VS 17。我在我的 Xamarin.Forms 应用程序中设置了 Prism,我刚刚添加了对我的 Api 界面的引用(在 ViewModel Constructor 中),它使应用程序停止导航到第二页。我需要这样做才能传递参数等。我遵循了本指南:
https://blog.qmatteoq.com/prism-for-xamarin-forms-basic-navigation-and-dependency-injection-part-2/
这是我让导航停止工作的方法:
private readonly IService _Service;
private ObservableCollection<TodoItem> _topSeries;
public ObservableCollection<TodoItem> TopSeries
{
get { return _topSeries; }
set { SetProperty(ref _topSeries, value); }
}
这是构造函数:
public SecondPageViewModel(IService Service, INavigationService navigationService)
{
_Service = Service;
_navigationService = navigationService;
}
因为我添加了上面的代码,所以我什至无法访问上面的视图模型。我试图在 DelegateCommand 上设置断点(在第一个 ViewModel 上),但它只是在 InitializeComponent() 之后停止;然后什么也没有发生。没有错误信息!谢谢!
更新:
我的服务 class 获取数据:
public class Service : IService
{
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
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<View.MainPage, MainPageViewModel>();
containerRegistry.RegisterForNavigation<View.SecondPage, SecondPageViewModel>();
containerRegistry.Register<IService, Service>();
}
我的界面:
public interface IService
{
Task<List<TodoItem>> DataAsync();
}
这是我导航的方式(从列表视图中单击):
private EventItem _selectedEvent { get; set; }
public EventItem SelectedEvent
{
get { return _selectedEvent; }
set
{
if (_selectedEvent != value)
{
if (Device.RuntimePlatform == Device.iOS)
{
_selectedEvent = null;
}
else
{
_selectedEvent = value;
}
NavigationParameters navParams = new NavigationParameters();
navParams.Add("PassedValue", _todoItem.name);
_navigationService.NavigateAsync("SecondPage", navParams);
}
}
}
编辑:
当我在没有 Api 服务代码的情况下进行调试时,命令会将我带到新视图模型中的新构造函数。使用代码,它不会到达构造函数。
根据您的代码,您声明了这样的构造函数:
Service()
{
// ...
}
您没有设置访问修饰符,因此默认为internal
。这是定义:
Internal types or members are accessible only within files in the same assembly.
很可能您在另一个程序集中声明了 Service.cs
,而 Prism 无法访问其构造函数。
您的导航不起作用,因为依赖项注入失败。要修复它,只需将您的访问修饰符更改为 public
:
public Service()
{
// ...
}