System.InvalidOperationException: не найдено CurrentActivity - PullRequest
0 голосов
/ 26 апреля 2019

Проблема навигации в Xamarin android. Когда я нажимаю для навигации, я получаю следующее исключение System.InvalidOperationException: не найдено CurrentActivity.

это код класса app.cs

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

namespace SGDD
{
  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.DetailPageKey, typeof(DetailActivity));

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

          _locator = new ViewModelLocator();
        }

        return _locator;
      }
    }
  }
}

Это код ViewModelLocatar

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

namespace SGDD.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 DetailPageKey = "DetailPage";

    /// <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>();
      }
    }
    public DetailViewModel DetailViewModel
    {
      get
      {
        return ServiceLocator.Current.GetInstance<DetailViewModel>();
      }
    }

    /// <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>();
      SimpleIoc.Default.Register<DetailViewModel>();
    }

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

Это Декларация ViewModel в действии

 private DetailViewModel DetailViewModel
    {
      get
      {
        return App.Locator.DetailViewModel;
      }
    }

Это привязка с щелчком.

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

ЭтоDeatilViewModel Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Views;
using SGDD.ViewModel;

namespace SGDD_Portal.ViewModel
{
    public class DetailViewModel : ViewModelBase
    {
    private INavigationService _navigationService;
    public DetailViewModel(INavigationService navigationService)
    {
      _navigationService = navigationService;


    }




    private RelayCommand _companyCommand;

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


                       _navigationService.NavigateTo(ViewModelLocator.DetailPageKey);




                           }));
      }
    }


  }
}
...