代码之家  ›  专栏  ›  技术社区  ›  KalleP

在ViewModel构造函数和导航中添加Api接口将停止工作

  •  2
  • KalleP  · 技术社区  · 7 年前

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

    更新:

    获取数据的我的服务类:

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

    这是我导航的方式(从listview单击):

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

    编辑:

    1 回复  |  直到 7 年前
        1
  •  2
  •   Wojciech Kulik    7 年前

    根据您的代码,您声明了如下构造函数:

    Service()
    {
        // ...
    }
    

    internal . 定义如下:

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

    Service.cs 您的导航无法工作,因为依赖项注入失败。要修复它,只需将访问修饰符更改为 public :

    public Service()
    {
        // ...
    }
    
    推荐文章