本文介绍了绑定一个按钮,一个ViewModel命令时,该按钮是一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是烤到最新的Andr​​oid导航抽屉。

I'm using the Navigation Drawer that's baked into the latest Android.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/Home_HomeView_DrawerLayout" >
<!-- The main content view -->
    <FrameLayout
        android:id="@+id/Home_HomeView_ContentFrame" />
<!-- The navigation drawer -->

    <!-- ... -->

</android.support.v4.widget.DrawerLayout>

在我的 HomeView ,我将根据所选项目 Home_HomeView_ContentFrame 片段

In my HomeView, I set the Home_HomeView_ContentFrame fragment based on the Selected Item

private void SelectItem(int position)
{
    var fragment = new HomeFragment(ViewModel);
    var arguments = new Bundle();
    fragment.Arguments = arguments;
    _actionBarTitle = ((HomeViewModel)ViewModel).NavigationItems[position].Text;

    SupportFragmentManager.BeginTransaction()
                          .Replace(Resource.Id.Home_HomeView_ContentFrame, fragment)
                          .Commit();

    _topDrawerList.SetItemChecked(position, true);
    ActionBar.Title = _actionBarTitle;


    _navigationDrawer.CloseDrawer(_drawerInnerLayout);
}

HomeFragment 是设置的BindingContext MvvmCross

The HomeFragment is to setup the BindingContext for MvvmCross

public sealed class HomeFragment : MvxFragment
{

    public HomeFragment(IMvxViewModel viewModel)
    {
        ViewModel = viewModel;
    }

    public override View OnCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle)
    {
        // A breakpoint below shows that the ViewModel does in fact contain the IPlayCommand as expected.
        BindingContext = new MvxAndroidBindingContext(Activity, new MvxSimpleLayoutInflater(layoutInflater), ViewModel);
        var rootView = layoutInflater.Inflate(Resource.Layout.Home_HomeFragment, viewGroup, false);

        Activity.Title = Resources.GetString(Resource.String.ApplicationName);
        return rootView;
    }
}

以及 Home_HomeFragment 的布局里面,我有一个绑定到一个IMvxCommand按钮。请注意,这个按钮是基于

And inside the Home_HomeFragment layout, I have a button that is bound to an IMvxCommand. Note, this button is based on @Stuart's example.

    <FutureState.AudioBook.Droid.Ui.Controls.FsmButton
        android:id="@+id/Home_HomeFragment_PlayPauseIcon"
        android:clickable="true"
        android:adjustViewBounds="true"
        local:MvxBind="Command PlayCommand; CommandParameter ." />

该视图模型,它的结合是目前相当简单的。

The ViewModel that it's binding to is currently quite simple.

public class HomeViewModel : ViewModelBase
{
    public IList<NavigationItem> NavigationItems;

    public HomeViewModel(IPlayCommand playCommand)
    {
        // playCommand is being resolved by the IoC
        _playCommand = playCommand;
    }

    private IPlayCommand _playCommand;
    public IPlayCommand PlayCommand
    {
        get { return _playCommand; }
        set
        {
            _playCommand = value;
            RaisePropertyChanged(() => PlayCommand);
        }
    }
}

而当我把一个断点的的BindingContext HomeFragment ,我可以肯定看到视图模型填充与PlayCommand。

And when I put a BreakPoint on the BindingContext line of the HomeFragment, I can definitely see the ViewModel populated with the PlayCommand.

我遇到的问题是,触摸按钮的行为不火的 PlayCommand 。我在哪里去了?

The problem I'm having is that the act of touching the button doesn't fire the PlayCommand. Where am I going wrong?

推荐答案

Android的充气不知道mvvmcross结合。

Android inflate doesn't know about mvvmcross binding.

要使用基于XML的绑定,则必须使用 BindingInflate - 看到的在 - 包括在n个碎片= 26 - https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-26-Fraggle/Rock.Droid/Views/SubFrag.cs

To use XML based binding, you must use BindingInflate - see the samples in http://mvvmcross.blogspot.com with code in https://github.com/MvvmCross/NPlus1DaysOfMvvmCross - including fragments in n=26 - https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-26-Fraggle/Rock.Droid/Views/SubFrag.cs

还有中的

这篇关于绑定一个按钮,一个ViewModel命令时,该按钮是一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:11