Android 从服务访问 MvxDialogFragment 视图
Android access MvxDialogFragment view from service
我正在使用 MVVMCross,但 MvxDialogFragment 绑定有问题。
我有一个基础服务,它在核心 PCL 项目中得到解决,在 iOS 和 Android 项目中添加自定义服务实现从基础服务 class.
在 android 服务中,我构造并显示 MvxDialogFragment 实例:
var top = (MvxFragmentActivity)Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;
if (top == null)
{
throw new MvxException("Cannot get current top activity");
}
var dlg = new AlertDialog.Builder(top);
dlg.Create().Show();
dialog = new MyDialog
{
Cancelable = false
};
dialog.Show(top.SupportFragmentManager, "");
我有简单的对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/test_click_button"
android:text="Test"
app:MvxBind="Click TestClickCommand" />
</LinearLayout>
所以我的目标是从服务实例化的 dialogFragment 访问基本服务命令。我该怎么做?
作为替代方案,我想在服务中处理我的按钮点击,但找不到执行此操作的方法,因为我的 View、ViewModel 或 Dialog 属性为空。
如何处理服务中的点击,或者实现自绑定?
最后我通过 MvxDialogFragment 订阅和服务注入实现了预期:
public class MyDialog : MvxDialogFragment
{
private ISampleService _sampleService;
public MyDialog(ISampleService sampleService)
{
_sampleService = sampleService;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
EnsureBindingContextSet(savedInstanceState);
var dialog = new AlertDialog.Builder(Activity);
var view = this.BindingInflate(Resource.Layout.MyDialog, null);
view.FindViewById(Resource.Id.test_click_button).Click += (sender, e) =>
{
_sampleService.TestClick();
Dismiss();
};
dialog.SetView(view);
return dialog.Create();
}
}
我正在使用 MVVMCross,但 MvxDialogFragment 绑定有问题。 我有一个基础服务,它在核心 PCL 项目中得到解决,在 iOS 和 Android 项目中添加自定义服务实现从基础服务 class.
在 android 服务中,我构造并显示 MvxDialogFragment 实例:
var top = (MvxFragmentActivity)Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;
if (top == null)
{
throw new MvxException("Cannot get current top activity");
}
var dlg = new AlertDialog.Builder(top);
dlg.Create().Show();
dialog = new MyDialog
{
Cancelable = false
};
dialog.Show(top.SupportFragmentManager, "");
我有简单的对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/test_click_button"
android:text="Test"
app:MvxBind="Click TestClickCommand" />
</LinearLayout>
所以我的目标是从服务实例化的 dialogFragment 访问基本服务命令。我该怎么做?
作为替代方案,我想在服务中处理我的按钮点击,但找不到执行此操作的方法,因为我的 View、ViewModel 或 Dialog 属性为空。
如何处理服务中的点击,或者实现自绑定?
最后我通过 MvxDialogFragment 订阅和服务注入实现了预期:
public class MyDialog : MvxDialogFragment
{
private ISampleService _sampleService;
public MyDialog(ISampleService sampleService)
{
_sampleService = sampleService;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
EnsureBindingContextSet(savedInstanceState);
var dialog = new AlertDialog.Builder(Activity);
var view = this.BindingInflate(Resource.Layout.MyDialog, null);
view.FindViewById(Resource.Id.test_click_button).Click += (sender, e) =>
{
_sampleService.TestClick();
Dismiss();
};
dialog.SetView(view);
return dialog.Create();
}
}