1

My issue was born in Does MvvmCross work in Xamarin Studio?. MvvmCross works fine in Visual Studio. However, I've been mandated to deploy this corporately using Xamarin Studio which is what their build server uses. I ran into the issue with System.Windows.Input.ICommand not being found by the compiler during my Xamarin Studio build. ICommands appear to be used quite extensively throughout MvvmCross for user commands (MvxCommand, which implements System.Windows.Input.ICommand). I've tried creating my own version of ICommand via the following code:

using System;

namespace Something.Common
{
    public interface ICommand
    {
        event EventHandler CanExecuteChanged;
        bool CanExecute(object parameter);
        void Execute(object parameter);
    }
}

All good, but still doesn't fix MvxCommand, because it implements the interface System.Windows.Input.ICommand. So I created my own version, MvxCommandEx, which is basically copied from Stuart's MvxCommand and implements my own ICommand (Something.Common.ICommand).

Lo and behold, it builds. It deploys. It got me all excited. But.... it didn't work. Any place I've bound a UI element to my custom ICommand just doesn't do anything. It's as if the binding from the Click event of the control to the view model's command is just not there anymore, whether I bind it in the .axml layout file... or use the CreateBindingSet method built-in to the view.

Now... I can get around this for some things... for instance, if I use a standard android Button, and invoke the view model's command manually from the built-in Click event, like:

btnAddScope.Click += (o, i) => { _ViewModel.RequestAddScope.Execute(null); };

it works, and I'm okay doing it this way in the interim until Xamarin releases their PCL support. But I'm using an MvxListView in another section of the app that was bound the old way using a syntax like:

lst.ItemClick = _ViewModel.RequestViewScope;

where lst is a MvxListView.

This won't work, however, because lst.ItemClick expects a System.Windows.Input.ICommand, and my ICommand isn't in that namespace.

Stuart provided explanations for this that supposedly are supposed to work. However, I'm feeling stupid for not being able to implement the ICommand in a way that actually works, when it feels like it should work... so before I go down a different avenue to address this, I wanted to see if anyone could shed light on what I'm doing wrong.

4

1 回答 1

0

简单的解决方案是:

  • 为 Xamarin 版本提供您自己的实现System.Windows.Input.ICommand并使用此实现
  • 或使用在 mac 上构建的 mvvmcross 二进制文件。

我推荐第一个。

如果您删除您的 Something.Common.ICommand 代码 abd,然后将您的 MyCommand 类实现与您的视图模型放在同一个项目/程序集中,那么它们应该在 Windows 和 Mac 上构建和运行良好。

于 2013-07-03T22:40:51.243 回答