显示MvxDialogFragment的简单方法是什么?

Jim*_*cox 2 c# android xamarin.android mvvmcross

我正在尝试使用MvxDialogFragment从活动显示数据绑定对话框。我的对话框ViewModel如下:

public class ContainerDialogViewModel : MvxViewModel
{

    public string ShipperName;

    public void Init(string Name)
    {
        ShipperName = Name;
        LoadData();
    }

    public void LoadData()
    {
        Survey = SurveyDataSource.CurrSurvey;
    }

    private ShipmentSurvey _Survey;
    public ShipmentSurvey Survey
    {
        get
        {
            return _Survey;
        }
        set
        {
            _Survey = value;
            RaisePropertyChanged(() => Survey);
            RaisePropertyChanged(() => Containers);
        }
    }


    public List<ShipmentSurveyContainer> Containers
    {
        get
        {
            if (Survey == null)
                return new List<ShipmentSurveyContainer>();
            else
                return Survey.SurveyContainers.ToList();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

MvxDialogFragment的编码如下:

public class ContainerDialog : MvxDialogFragment<ContainerDialogViewModel>
{
    public override Dialog OnCreateDialog(Bundle savedState)
    {
        base.EnsureBindingContextSet(savedState);

        this.BindingInflate(Resource.Layout.ContainerDialog, null);

        return base.OnCreateDialog(savedState);
    }

}
Run Code Online (Sandbox Code Playgroud)

在我的活动中,我试图找出启动对话框的最简单方法。这是我尝试过的:

public class SurveyView : MvxActivity
{
    public void ShowContainerDialog()
    {
        ContainerDialogViewModel vm = new ViewModels.ContainerDialogViewModel();
        vm.Init("Test Name");
        var dialogFragment = new ContainerDialog()
        {
            DataContext = vm
        };
        dialogFragment.Show(FragmentManager, "Containers");
    }
}
Run Code Online (Sandbox Code Playgroud)

我很确定我创建视图模型的方法是非传统的,但是我不知道另一种方法。最大的问题是FragmentManager转换为错误的版本。节目正在寻找Android.Support.V4.App.FragmentManager,公开的FragmentManager是Android.App.FragmentManager。我尝试将MvxActivity更改为MvxFragmentActivity,但这似乎无济于事。有人可以指出我正确的方向吗?

Jim*_*cox 6

当我尝试执行此操作时,MvvmCross并没有真正支持此功能,但是遇到了另一个需要该功能的实例,可惜功能确实存在。感谢@ Martijn00为我指出解决方案。这将是非常基本的,但是我认为这可能会对某人有所帮助。

我的版式:

<?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="match_parent"
    android:layout_height="wrap_content">
  <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      style="@style/TableHeaderTextView"
      android:text="Work Date"/>
  <MvxDatePicker
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    local:MvxBind="Value WorkDate" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Close"
        local:MvxBind="Click CloseCommand" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的ViewModel:

public class HoursDateDialogViewModel : MvxViewModel<EstimateHours>
{
    private readonly IMvxNavigationService _navigationService;

    public HoursDateDialogViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;

        CloseCommand = new MvxAsyncCommand(async () => await _navigationService.Close(this));
    }

    public override System.Threading.Tasks.Task Initialize()
    {
        return base.Initialize();
    }

    public override void Prepare(EstimateHours parm)
    {
        base.Prepare();
        Hours = parm;
    }

    public IMvxAsyncCommand CloseCommand { get; private set; }

    private EstimateHours _Hours;
    public EstimateHours Hours
    {
        get
        {
            return _Hours;
                }
        set
        {
            _Hours = value;
            RaisePropertyChanged(() => Hours);
            RaisePropertyChanged(() => WorkDate);
        }
    }

    public DateTime WorkDate
    {
        get
        {
            return Hours.StartTime ?? DateTime.Today;
        }
        set
        {
            DateTime s = Hours.StartTime ?? DateTime.Today;
            DateTime d = new DateTime(value.Year, value.Month, value.Day, s.Hour, s.Minute, s.Second);
            Hours.StartTime = d;
            DateTime e = Hours.EndTime ?? DateTime.Today;
            d = new DateTime(value.Year, value.Month, value.Day, e.Hour, e.Minute, e.Second);
            Hours.EndTime = d;
            RaisePropertyChanged(() => WorkDate);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我的看法:

[MvxDialogFragmentPresentation]
[Register(nameof(HoursDateDialogView))]
public class HoursDateDialogView : MvxDialogFragment<HoursDateDialogViewModel>
{
    public HoursDateDialogView()
    {
    }

    protected HoursDateDialogView(IntPtr javaReference, JniHandleOwnership transfer)
        : base(javaReference, transfer)
    {
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var ignore = base.OnCreateView(inflater, container, savedInstanceState);

        var view = this.BindingInflate(Resource.Layout.HoursDateDialogView, null);

        return view;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的所有都是它的。我能够传递参数对象并将该对象的一部分绑定到MvxDatePicker。为了显示此对话框,首先在Setup.cs中,您需要:

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        return new MvxAppCompatViewPresenter(AndroidViewAssemblies);
    }
Run Code Online (Sandbox Code Playgroud)

在从中打开对话框的视图模型中,需要一个包含以下内容的构造函数:

    private readonly IMvxNavigationService _navigationService;

    public LocalHourlyViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;
    }
Run Code Online (Sandbox Code Playgroud)

这将注入导航服务,以便您可以使用它。最后,打开对话框所需要做的就是:

async () => await _navigationService.Navigate<HoursDateDialogViewModel, EstimateHours>(Item);
Run Code Online (Sandbox Code Playgroud)

我什至不确定您是否必须等待呼叫,但是我一直在关注该示例。您可以在提供的@ Martijn00链接中查看更多示例:

https://github.com/MvvmCross/MvvmCross/tree/develop/TestProjects/Playground

干杯!