2013-12-16 52 views
1

我正在使用已導入最新Android的導航抽屜。當按鈕位於片段中時,將按鈕綁定到ViewModel命令

<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片段根據所選項目

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

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的按鈕。請注意,這個按鈕是基於@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 ." /> 

視圖模型,它的結合是目前相當簡單。

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); 
     } 
    } 
} 

而且當我把一個斷點在BindingContextHomeFragment的,我可以肯定地看到填入PlayCommand視圖模型。

我遇到的問題是觸摸按鈕的行爲不會觸發PlayCommand。我哪裏錯了?

回答

2
+0

啊導航抽屜樣品,就是這樣。我不得不添加Sub ViewModels和尷尬的綁定,使其工作...但我看到現在發生了什麼。謝謝! –

相關問題