我不能结合从布局视图模型的单击事件。
我想使用MVVM Cross 5.6在两个片段之间进行导航。 我正在使用本机Xamarin.Android。
首先,我创建了一个ViewModel:
{
public ICommand NavigateCommand => new MvxCommand(() => {
this.ShowViewModel<ProfileFragmentViewModel>(); });
}
使用的布局是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:background="#005000"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Navigate"
local:MvxBind="Click NavigateCommand"
android:background="#ffec4747"
android:clickable="true"/>
</LinearLayout>
然后,我创建了一个片段:
public class FirstFragmentView : MvxFragment<FirstFragmentViewModel>
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.FirstFragmentView, null);
}
}
FirstFragmentView在此处是不完整的:
[Activity(MainLauncher = true)]
public class TabView : MvxTabsFragmentActivity
{
public TabView()
: base(Resource.Layout.Home, Resource.Id.actualtabcontent)
{
}
public FirstFragmentViewModel FirstViewModel => (FirstFragmentViewModel)this.ViewModel;
public FirstViewModel SecondViewModel => (FirstViewModel)this.ViewModel;
protected override void AddTabs(Bundle args)
{
AddTab<FirstFragmentView>("1", "Tab 1", args, FirstViewModel);
AddTab<SecondFragmentView>("2", "Tab 2", args, SecondViewModel);
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
}
}
按钮已显示,但处于禁用状态,单击该按钮,则什么也没有发生。
答案 0 :(得分:1)
调用时好像传递了错误的ViewModel:
AddTab<FirstFragmentView>("1", "Tab 1", args, FirstViewModel);
“活动”中的以下几行看起来可疑:
public FirstFragmentViewModel FirstViewModel => (FirstFragmentViewModel)this.ViewModel;
public FirstViewModel SecondViewModel => (FirstViewModel)this.ViewModel
您正在将this.ViewModel
投射到FirstFragmentViewModel
和FirstViewModel
上,这没有意义。 Activity和每个Fragment都应具有各自独立的ViewModel。
尝试进行此更改,看看是否有效:
AddTab<FirstFragmentView>("1", "Tab 1", args, new FirstFragmentViewModel());