使用Xamarin.Forms和MvvmCross处理ICommand
我对使用MvvmCross和Xamarin.Forms简单处理ICommand有问题 我已经为VisualStudio创建了一个带有MvvmCross插件的项目,我得到了简单的MainView.xaml和MainViewModel.cs 我更改了ViewModel:只添加了一个字符串+一个ICommand使用Xamarin.Forms和MvvmCross处理ICommand,xamarin,mvvmcross,xamarin.forms,Xamarin,Mvvmcross,Xamarin.forms,我对使用MvvmCross和Xamarin.Forms简单处理ICommand有问题 我已经为VisualStudio创建了一个带有MvvmCross插件的项目,我得到了简单的MainView.xaml和MainViewModel.cs 我更改了ViewModel:只添加了一个字符串+一个ICommand using System.Diagnostics; using System.Windows.Input; using Cirrious.MvvmCross.ViewModels; name
using System.Diagnostics;
using System.Windows.Input;
using Cirrious.MvvmCross.ViewModels;
namespace XamarinFormsMvvm.Core.ViewModels
{
public class MainViewModel : BaseViewModel
{
private string sampleText = "Hello from the Ninja Coder!";
private string sampleText2 = "Hello2";
public MainViewModel()
{
}
public string SampleText
{
get { return this.sampleText; }
set { this.SetProperty(ref this.sampleText, value); }
}
public string SampleText2
{
get { return this.sampleText2; }
set { this.SetProperty(ref this.sampleText2, value); }
}
public ICommand StartStopCommand
{
get
{
return new MvxCommand(() =>
{
Debug.WriteLine("Test");
});
}
}
}
}
我的视图添加了标签+按钮
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinFormsMvvm.Forms.Views.MainView"
xmlns:viewModels="clr-namespace:XamarinFormsMvvm.Core.ViewModels;assembly=XamarinFormsMvvm.Core"
Title="MainView Page"
>
<ContentPage.BindingContext>
<viewModels:MainViewModel />
</ContentPage.BindingContext>
<ContentView>
<StackLayout>
<Label Text='{Binding SampleText}' VerticalOptions='Center' HorizontalOptions='Center'/>
<Label Text='{Binding SampleText2}' VerticalOptions='Center' HorizontalOptions='Center'/>
<Button Command="{Binding StartStopCommand}" Text="asd" VerticalOptions="Center" HorizontalOptions="Center"/>
</StackLayout>
</ContentView>
</ContentPage>
运行该命令后,我遇到异常:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
03-05 18:32:16.898 I/MonoDroid( 2699): at Cirrious.CrossCore.Mvx.TryResolve[IMvxCommandHelper] (IMvxCommandHelper& service) [0x00000] in <filename unknown>:0
03-05 18:32:16.898 I/MonoDroid( 2699): at Cirrious.MvvmCross.ViewModels.MvxCommandBase..ctor () [0x00000] in <filename unknown>:0
03-05 18:32:16.898 I/MonoDroid( 2699): at Cirrious.MvvmCross.ViewModels.MvxCommand..ctor (System.Action execute, System.Func`1 canExecute) [0x00000] in <filename unknown>:0
03-05 18:32:16.898 I/MonoDroid( 2699): at Cirrious.MvvmCross.ViewModels.MvxCommand..ctor (System.Action execute) [0x00000] in <filename unknown>:0
03-05 18:32:16.898 I/MonoDroid( 2699): at XamarinFormsMvvm.Core.ViewModels.MainViewModel.get_StartStopCommand () [0x00001] in c:\Users\Tomasz\Documents\Visual Studio 2013\Projects\XamarinFormsMvvm\XamarinFormsMvvm.Core\ViewModels\MainViewModel.cs:49
...........
这段代码有什么问题?我刚刚用Ninja创建了一个项目,正如您所做的那样,在MainViewModel和MainView上通过了您的代码,一切都很好。 老实说,在忍者创造了解决方案结构之后,我对它做了一些修改
你可以在这里找到我的NinjaMvxForms空白项目修改和工作这里发生了什么:MainViewModel.cs:49?您提供的MainViewModel没有49行。。。