MVVMCross中的片段

时间:2016-05-17 22:41:46

标签: c# xamarin mvvmcross

我有一个导航抽屉,它有项目列表,还有一个叫CustomPreference。当用户点击它时,它会打开相应的视图,但会显示activity,但我尝试将该页面设为fragment。我想知道我在实施中缺少什么。

CustomPreferenceView.axml

<?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:orientation="vertical"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="#CECECE">
</LinearLayout>

CustomPreferenceView.cs

[MvxFragment(typeof(MainViewModel), Resource.Id.MainViewContainer)]
[Register("myapp.android.views.CustomPreferenceView")]
public class CustomPreferenceView: MvxFragment<CustomPreferenceViewModel>
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var ignored = base.OnCreateView(inflater, container, savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.CustomPreferenceView, null);
        return view;
    }
}

CustomPreferenceViewModel.cs

public class CustomPreferenceViewModel: MvxViewModel
{

}

我可以简单地说明我想要实现的目标如下:

enter image description here

MenuView.cs

public bool OnNavigationItemSelected(IMenuItem item)
{
  Navigate(item.ItemId);
  return true;
}

private async Task Navigate(int selectedId)
{
   ((MainView)Activity).DrawerLayout.CloseDrawers();
   await Task.Delay(TimeSpan.FromMilliseconds(320));

   switch (selectedId)
   {
     case Resource.Id.logout:
         ViewModel.ShowLogoutCommand.Execute();
         break;
     case Resource.Id.custompreference:
         ViewModel.ShowCustomPreferenceCommand.Execute();
         break;
   }
}

MainViewModel.cs

public IMvxCommand ShowCustomPreferenceCommand
{
  get { return new MvxCommand(ShowCustomPreference); }
}

private void ShowCustomPreference()
{
   ShowViewModel<CustomPreferenceViewModel>();
}

MainView.axml

<?xml version="1.0" encoding="utf-8"?>
<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/MainViewDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/MainViewContainer"
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1" />
    <FrameLayout
        android:id="@+id/NavigationViewContainer"
        android:layout_height="match_parent"
        android:layout_width="320dp"/>
     </android.support.v4.widget.DrawerLayout>

MainView.cs

public class MainView : MvxCachingFragmentCompatActivity<MainViewModel>
 {
    public DrawerLayout DrawerLayout;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.MainView);

        DrawerLayout = FindViewById<DrawerLayout>(Resource.Id.MainViewDrawer);
        ViewModel.ShowMenu();

    }

0 个答案:

没有答案
相关问题