如何使用导航服务正确地将参数从一个视图模型传递到另一个视图模型?

时间:2019-07-23 16:48:38

标签: c# xamarin.forms mvvm-light

我正在尝试将参数从一个视图模型传递给另一个,在这里如何描述: https://mallibone.com/post/xamarin.forms-navigation-with-mvvm-light。 问题是,当我打电话给我的NavigateAsync(string pageKey, object parameter, bool animated = true)时,它被抛出了一个InvalidOperationException并且导航服务中设置的消息是:No suitable constructor found for page

我的实际代码如下:

我要浏览的页面:

     public partial class FullScreenImagePopupPage : PopupPage
     {
       public FullScreenImagePopupPage(ImageSource imageSource)
       {
          var vm = App.Locator.FullScreenImageVM;
         BindingContext = vm;
         InitializeComponent();

         if (imageSource != null)
            vm.ImageSourceFullscreen = imageSource;
        }
      }

它是viewmodel:

     public class FullScreenImageViewModel : BaseViewModel
{
    private ImageSource _ImageSourceFullscreen { get; set; }

    public ImageSource ImageSourceFullscreen
    {
        get { return _ImageSourceFullscreen; }
        set
        {
            _ImageSourceFullscreen = value;
            OnPropertyChanged();
        }
    }
    public FullScreenImageViewModel(INavigationService navigationService) : base(navigationService)
    {

    }
}

每次我想导航到FullScreenImagePopupPage时调用的命令: OpenFullImageCommand = new Command(async () =>await navigationService.NavigateAsync(Locator.FullScreenImagePopupPage, ImageSourceFullScreen));

NavigateAsync函数的实现:

     public async Task NavigateAsync(string pageKey, object parameter, bool animated = true)
    {
        var page = GetPage(pageKey, parameter);


        if (page is PopupPage)
            await CurrentNavigationPage.Navigation.PushPopupAsync(page as PopupPage, true);
        else
        {
            await CurrentNavigationPage.Navigation.PushAsync(page, animated);
        }
    }

    public Page GetPage(string pageKey, object parameter = null)
    {
        lock (_sync)
        {
            if (!_pagesByKey.ContainsKey(pageKey))
            {
                throw new ArgumentException(
                    $"No such page: {pageKey}. Did you forget to call NavigationService.Configure?");
            }

            var type = _pagesByKey[pageKey];
            ConstructorInfo constructor;
            object[] parameters;

            if (parameter == null)
            {
                constructor = type.GetTypeInfo()
                    .DeclaredConstructors
                    .FirstOrDefault(c => !c.GetParameters().Any());

                parameters = new object[]
                {
                };
            }
            else
            {
                var a = type.GetTypeInfo()
                    .DeclaredConstructors;

                constructor = type.GetTypeInfo()
                    .DeclaredConstructors
                    .FirstOrDefault(
                        c =>
                        {
                            var p = c.GetParameters();
                            return p.Length == 1
                                   && p[0].ParameterType == parameter.GetType();
                        });

                parameters = new[]
                {
                parameter
            };
            }

            if (constructor == null)
            {
                throw new InvalidOperationException(
                    "No suitable constructor found for page " + pageKey);
            }

            var page = constructor.Invoke(parameters) as Page;
            return page;
        }
    }

0 个答案:

没有答案