【问题标题】:Handling ICommand with Xamarin.Forms and MvvmCross使用 Xamarin.Forms 和 MvvmCross 处理 ICommand
【发布时间】:2015-05-07 06:09:34
【问题描述】:

我在使用 MvvmCross 和 Xamarin.Forms 简单处理 ICommand 时遇到问题。

我已经为 VisualStudio 创建了一个带有 MvvmCross 插件的项目,并且我有简单的 MainView.xaml 和 MainViewModel.cs

我已经更改了ViewModel:(只添加了一个字符串+一个ICommand)

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 
...........

这段代码有什么问题?

【问题讨论】:

  • 这里发生了什么:MainViewModel.cs:49?你提供的 MainViewModel 没有 49 行...

标签: xamarin mvvmcross xamarin.forms


【解决方案1】:

我刚刚像您一样使用 Ninja 创建了一个项目,并将您的代码粘贴到 MainViewModel 和 MainView 上,一切正常。 老实说,我不得不说我在Ninja创建后对解决方案结构进行了一些修改。

你可以在这里找到我的 NinjaMvxForms 空白项目修改和工作https://github.com/MobiliTips/NinjaMvxForms

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2018-12-04
    相关资源
    最近更新 更多