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