Xamarin Android中的Mvvm导航问题

时间:2019-03-23 22:11:49

标签: c# android xamarin

我在Xamarin Android中使用Mvvm导航。但是我遇到了问题。例外在下面

ex = {System.InvalidOperationException: No CurrentActivity found
  at GalaSoft.MvvmLight.Views.NavigationService.NavigateTo (System.String pageKey, System.Object parameter) [0x00007].

这是App.cs代码

using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Threading;
using GalaSoft.MvvmLight.Views;
using SGDD_Portal.ViewModel;

namespace SGDD_Portal
{
    public static class App
    {
        private static ViewModelLocator _locator;

        public static ViewModelLocator Locator
        {
            get
            {
                if (_locator == null)
                {
                    // Initialize the MVVM Light DispatcherHelper.
                    // This needs to be called on the UI thread.
                    DispatcherHelper.Initialize();

                    // Configure and register the MVVM Light NavigationService
                    var nav = new NavigationService();
                    SimpleIoc.Default.Register<INavigationService>(() => nav);
                    nav.Configure(ViewModelLocator.SecondPageKey, typeof(SecondActivity));
                    nav.Configure(ViewModelLocator.ThirdPageKey, typeof(Third));

                    // Register the MVVM Light DialogService
                    SimpleIoc.Default.Register<IDialogService, DialogService>();

                    _locator = new ViewModelLocator();
                }

                return _locator;
            }
        }
    }
}

这是ViewModelLocator代码

using System.Diagnostics.CodeAnalysis;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
using Microsoft.Practices.ServiceLocation;
using SGDD_Portal.Design;
using SGDD_Portal.Model;

namespace SGDD_Portal.ViewModel
{
    /// <summary>
    /// This class contains static references to the most relevant view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// See http://www.mvvmlight.net
    /// </para>
    /// </summary>
    public class ViewModelLocator
    {
        /// <summary>
        /// The key used by the NavigationService to go to the second page.
        /// </summary>
        public const string SecondPageKey = "SecondPage";
        public const string ThirdPageKey = "ThirdPage";

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        /// <summary>
        /// This property can be used to force the application to run with design time data.
        /// </summary>
        public static bool UseDesignTimeData
        {
            get
            {
                return false;
            }
        }

        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (!ViewModelBase.IsInDesignModeStatic
                && !UseDesignTimeData)
            {
                // Use this service in production.
                SimpleIoc.Default.Register<IDataService, DataService>();
            }
            else
            {
                // Use this service in Blend or when forcing the use of design time data.
                SimpleIoc.Default.Register<IDataService, DesignDataService>();
            }

            SimpleIoc.Default.Register<MainViewModel>();
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
        }
    }
}

这是fab按钮单击的绑定

fabMain.SetCommand(
               "Click",
               Vm.CompanyCommand
      );

这是MainViewModel

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using GalaSoft.MvvmLight.Views;
using Microsoft.Practices.ServiceLocation;
using SGDD_Portal.Model;

namespace SGDD_Portal.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.mvvmlight.net
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private INavigationService _navigationService;


        public MainViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;



        }

        private RelayCommand _companyCommand;

        public RelayCommand CompanyCommand
        {
            get
            {
                return _companyCommand
                       ?? (_companyCommand = new RelayCommand(
                          () =>
                           {
                               try
                               {

                               _navigationService.NavigateTo(ViewModelLocator.ThirdPageKey);
                               }
                               catch(Exception ex)
                               {


                               }
                           }));
            }
        }


    }

}

0 个答案:

没有答案