Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

mvvm - mvvmcross touch command binding in android

I'm looking for a way to do a "Touch" command binding between axml and ViewModel, or some else like FocusChanged etc.

A simple "Click" command works fine like so: local:MvxBind="{'Touch':{'Path':'CameraButtonCommand'}}" />

    public IMvxCommand CameraButtonCommand
    {
        get
        {
            return new MvxRelayCommand(
                () =>
                {
                    RequestNavigate<AugRealityViewModel>(true);
                })
            ;
        }
    }

However, I've tried other event types for the controll(in this case it's ImageButton) and they are not being processed. When I've checked the events listings in the View Class I see those:

    public event EventHandler Click;
    public event EventHandler<View.CreateContextMenuEventArgs> ContextMenuCreated;
    public event EventHandler<View.FocusChangeEventArgs> FocusChange;
    public event EventHandler<View.KeyEventArgs> KeyPress;
    public event EventHandler<View.LongClickEventArgs> LongClick;

Only Click event has the general EventHandler attached to it, while other have genericed EventHandlers, and I'm wondering if that's the reason why it doesn't work.

I've also tried to attach a method to those events in the View class getting the proper control by FindViewById method and it works as expected this time around. But somehow I can't do it in the axml through Commands.

Also one more thing. The "Click" event is sending the "EventArgs" object as one of the parameters, and also the object reference. I can see that with ease if I do this behaviour in View Class, but when I do this by binding, I don't see those arguments when I'm processing the Command in ViewModel.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The framework can automatically bind any events which require EventHandler types. However, for any events which require a templated EventHandler (with custom EventArgs) then you are correct - you'll need to include a custom Binding.

The good news is that custom bindings are easy to write and to include.

For example, to bind:

public event EventHandler<View.LongClickEventArgs> LongClick;

you can include something like:

public class LongPressEventBinding
    : MvxBaseAndroidTargetBinding
{
    private readonly View _view;
    private IMvxCommand _command;

    public LongPressEventBinding(View view)
    {
        _view = view;
        _view.LongClick += ViewOnLongClick;
    }

    private void ViewOnLongClick(object sender, View.LongClickEventArgs eventArgs)
    {
        if (_command != null)
        {
            _command.Execute();
        }
    }

    public override void SetValue(object value)
    {
        _command = (IMvxCommand)value;
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            _view.Click -= ViewOnLongClick;
        }
        base.Dispose(isDisposing);
    }

    public override Type TargetType
    {
        get { return typeof(IMvxCommand); }
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }
}

Which can be configured in setup using something like:

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);

        registry.RegisterFactory(new MvxCustomBindingFactory<View>("LongPress", view => new LongPressEventBinding(view)));
    }

Note that you can't write a single class that binds to all the different event types - as the compiler requires you to include the correct Type for the EventArgs. However, you could fairly easily change public class LongClickEventBinding to something like public class CustomEventBinding<TViewType, TEventArgsType> if you wanted to.


With regards to what argument you should pass into the IMvxCommand Execute method, I guess this depends a bit on the method in question, and it also depends on whether you need the ViewModel to support multiple platforms, or whether it is just for Android.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...