gpt4 book ai didi

android - 当按钮位于 fragment 中时将按钮绑定(bind)到 ViewModel 命令

转载 作者:行者123 更新时间:2023-11-29 21:21:59 25 4
gpt4 key购买 nike

我使用的是内置在最新 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 中,我根据 Selected Item 设置了 Home_HomeView_ContentFrame fragment

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 布局中,我有一个绑定(bind)到 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 ." />

它绑定(bind)的 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);
}
}
}

当我在 HomeFragmentBindingContext 行放置断点时,我绝对可以看到 ViewModel 填充了 PlayCommand。

我遇到的问题是触摸按钮的 Action 不会触发 PlayCommand。我哪里错了?

最佳答案

Android inflate 不知道 mvvmcross 绑定(bind)。

要使用基于 XML 的绑定(bind),您必须使用 BindingInflate - 请参阅 http://mvvmcross.blogspot.com 中的示例代码在 https://github.com/MvvmCross/NPlus1DaysOfMvvmCross 中- 包括 n=26 中的 fragment - 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 中还有一个抽屉导航示例

关于android - 当按钮位于 fragment 中时将按钮绑定(bind)到 ViewModel 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20621644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com