Pass data parameter from model to next page Prism Xamarin Forms

前端 未结 1 1123
鱼传尺愫
鱼传尺愫 2021-01-26 07:24

I need to pass data from ViewModel 1 to ViewModel 2 using Prism.

TodoItem is my Model with the string:

public TodoItem _todotItem { get; set         


        
1条回答
  •  甜味超标
    2021-01-26 08:03

    In order to capture the parameters on the second page, you must use Prism.Navigation.INavigationAware. Please, check the code below, maybe will help you out.

    ./Views/ItemView.xaml

    
    
    
        
            
            
                
                    
                
                
                    
                        
                    
                
            
        
    
    
    

    ./ViewModels/ViewModelBase.cs

    using Prism;
    using Prism.Mvvm;
    using Prism.Navigation;
    using System;
    
    namespace Project.ViewModels
    {
        // INavigationAware provides a way for objects involved in navigation to be notified of navigation activities.
        public class ViewModelBase : BindableBase, INavigationAware, IDestructible
        {
            // ...
    
            protected INavigationService NavigationService { get; private set; }
    
            public ViewModelBase(INavigationService navigationService) => NavigationService = navigationService;
    
            /// 
            /// Called when the implementer is being navigated away from.
            /// 
            public virtual void OnNavigatedFrom(INavigationParameters parameters)
            {
    
            }
    
            /// 
            /// Called when the implementer has been navigated to.
            /// 
            public virtual void OnNavigatedTo(INavigationParameters parameters)
            {
    
            }
    
            // ...
        }
    }
    

    ./ViewModels/ItemViewModel.cs

    using Prism.Navigation;
    using System;
    using System.Collections.ObjectModel;
    using System.Threading.Tasks;
    using System.Windows.Input;
    using Xamarin.Forms;
    
    namespace Project.ViewModels
    {
        public class ItemViewModel : ViewModelBase
        {
            // ...
    
            private ObservableCollection _items;
            public ObservableCollection Items
            {
                get => _items;
                set => SetProperty(ref _items, value);
            }
    
            public ICommand ItemTappedCommand => new AsyncCommand(ItemTappedCommandAsync);
    
            public ItemViewModel(INavigationService navigationService)
                : base(navigationService)
            {
                Items = new ObservableCollection();
                // Load the data
            }
    
            // ...
    
            private async Task ItemTappedCommandAsync(object item)
            {
                var navParams = new NavigationParameters
                {
                    { "ItemSelected", (Item)item }
                };
    
                await NavigationService.NavigateAsync(nameof(ItemDetailView), navParams);
            }
        }
    }
    

    ./ViewModels/ItemDetailViewModel.cs

    using Prism.Navigation;
    using System;
    using System.Collections.ObjectModel;
    using System.Threading.Tasks;
    using System.Windows.Input;
    using Xamarin.Forms;
    
    namespace Project.ViewModels
    {
        public class ItemDetailViewModel : ViewModelBase
        {
            // ...
    
            public override void OnNavigatingTo(INavigationParameters parameters)
            {
                // Capture the parameter
                System.Diagnostics.Debug.WriteLine(parameters);
            }
    
            // ...
        }
    }
    

    0 讨论(0)
提交回复
热议问题