我正在尝试使用MvxDialogFragment来显示活动的数据绑定对话框。我的Dialog 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();
}
}
}
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);
}
}
在我的活动中,我试图找出启动对话框的最简单方法。这是我尝试过的:
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");
}
}
我很确定我创建视图模型的方法是非正统的,但我不知道另一种方法。最大的问题是FragmentManager被转换为错误的版本。 Show正在寻找Android.Support.V4.App.FragmentManager,并且公开的FragmentManager是Android.App.FragmentManager。我尝试将MvxActivity更改为MvxFragmentActivity,但这似乎没有帮助。有人能指出我正确的方向吗?
答案 0 :(得分:5)
我的布局:
<?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>
我的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);
}
}
}
我的观点:
[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;
}
}
这就是它的全部。我能够传递参数对象并将对象的一部分绑定到MvxDatePicker。要显示此对话框,首先在Setup.cs中,您需要:
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
return new MvxAppCompatViewPresenter(AndroidViewAssemblies);
}
在您将从中打开对话框的viewmodel中,您需要一个包含以下内容的构造函数:
private readonly IMvxNavigationService _navigationService;
public LocalHourlyViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
}
这将注入导航服务,以便您可以使用它。最后,打开对话框所需要做的就是:
async () => await _navigationService.Navigate<HoursDateDialogViewModel, EstimateHours>(Item);
我甚至不确定你是否必须等待电话,但我正在效仿这个例子。您可以在@ Martijn00提供的链接中看到更多示例:
https://github.com/MvvmCross/MvvmCross/tree/develop/TestProjects/Playground
干杯!
答案 1 :(得分:0)
我尝试将MvxActivity更改为MvxFragmentActivity
这是正确的第一步。然后传入FragmentManager
。
SupportFragmentManager
如果您不熟悉支持库,您可以阅读更多关于它们的内容以及如何使用Xamarin here