gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:11:51 25 4
gpt4 key购买 nike

我正在尝试使用 MvxDialogFragment 来显示 Activity 中的数据绑定(bind)对话框。我的 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);
}

}

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

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");
}
}

我很确定我创建 View 模型的方法是非正统的,但我不知道其他方法。最大的问题是 FragmentManager 转换为错误的版本。 Show 正在寻找 Android.Support.V4.App.FragmentManager,而公开的 FragmentManager 是 Android.App.FragmentManager。我尝试将 MvxActivity 更改为 MvxFragmentActivity,但这似乎没有帮助。有人可以指出我正确的方向吗?

最佳答案

当我尝试这样做时,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>

我的 View 模型:

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;
}
}

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

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
return new MvxAppCompatViewPresenter(AndroidViewAssemblies);
}

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

    private readonly IMvxNavigationService _navigationService;

public LocalHourlyViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
}

这会注入(inject)导航服务,因此您可以使用它。最后,打开对话框所需要做的就是:

async () => await _navigationService.Navigate<HoursDateDialogViewModel, EstimateHours>(Item);

我什至不确定您是否必须等待调用,但我是在按照示例进行操作。您可以在提供的@Martijn00 链接中查看更多示例:

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

干杯!

关于c# - 显示 MvxDialogFragment 的简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44935143/

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