我正在使用最新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
片段
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
是为MvvmCross设置BindingContext
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 ." />
它绑定的ViewModel目前非常简单。
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
的{{1}}行放置BreakPoint时,我绝对可以看到使用PlayCommand填充的ViewModel。
我遇到的问题是触摸按钮的动作不会触发HomeFragment
。我哪里错了?
答案 0 :(得分:2)
Android inflate不了解mvvmcross绑定。
要使用基于XML的绑定,您必须使用BindingInflate
- 请参阅http://mvvmcross.blogspot.com中包含https://github.com/MvvmCross/NPlus1DaysOfMvvmCross中代码的示例 - 包括n = 26中的片段 - https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-26-Fraggle/Rock.Droid/Views/SubFrag.cs
http://motzcod.es/post/60427389481/effective-navigation-in-xamarin-android-part-1
中还有导航抽屉样本