首页 > 解决方案 > 在 ViewModel 构造函数和导航中添加 Api 接口停止工作 Prism

问题描述

我将 VS 17 用于 Xamarin 表单。我已经在我的 Xamarin.Forms 应用程序中设置了 Prism,我刚刚添加了对我的 Api 接口(在 ViewModel 构造函数中)的引用,它使应用程序停止导航到第二页。我需要这样做才能传递参数等。我遵循了本指南:

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(); 之后停止。然后什么也没有发生。没有错误信息!谢谢!

更新:

我的获取数据的服务类:

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);
        }
    }
}

编辑:

当我在没有 ApiService 代码的情况下进行调试时,该命令会将我带到新视图模型中的新构造函数。使用代码它不会到达构造函数。

标签: c#xamarinxamarin.formsprism

解决方案


根据您的代码,您已经声明了这样的构造函数:

Service()
{
    // ...
}

您没有设置访问修饰符,因此默认的是internal. 这是定义:

内部类型或成员只能在同一程序集中的文件中访问。

很可能您Service.cs在另一个程序集中声明了您的声明,而 Prism 无法访问其构造函数。您的导航不起作用,因为依赖注入失败。要修复它,只需将访问修饰符更改为public

public Service()
{
    // ...
}

推荐阅读