【问题标题】:Adding Api interface in ViewModel constructor and navigation stops working Prism在 ViewModel 构造函数和导航中添加 Api 接口停止工作 Prism
【发布时间】:2018-11-30 07:04:11
【问题描述】:

我将 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 代码的情况下进行调试时,该命令会将我带到新视图模型中的新构造函数。使用代码它不会到达构造函数。

【问题讨论】:

  • 你说This is what I did to make the navigation stop working的那部分让我很困惑,那是怎么停止导航的?
  • 好吧,如果我删除上面的代码导航工作。只是因为添加了该代码,它突然停止工作。 @EpicKip
  • 从该代码中不清楚它停止工作的原因(它恰好起源于 idk)。您能否尝试制作一个测试项目并用尽可能少的代码重新创建问题?然后人们可以更轻松地帮助和识别问题:)
  • 我在这里有另一个帖子,我在这里展示了我的代码:stackoverflow.com/questions/53520601/…@EpicKip
  • 这里是 HTTPClient 代码:stackoverflow.com/questions/53542609/…@EpicKip

标签: c# xamarin xamarin.forms prism


【解决方案1】:

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

Service()
{
    // ...
}

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

内部类型或成员只能在同一个文件中访问 组装。

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

public Service()
{
    // ...
}

【讨论】:

  • 不客气 :)。奇怪的是,在这种情况下 Prism 不会抛出异常,也不会用空服务初始化 ViewModel。
猜你喜欢
  • 2019-12-28
  • 2016-04-03
  • 2019-07-10
  • 2017-04-08
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多