问题描述
您好,我是 WPF 和 XAML 的新手,我正在尝试利用 MVVMCross 的 MvxInteraction 与用户交互,以根据此 example 获得“是”或“否”确认。
我在让交互订阅事件处理程序时遇到了障碍,因为交互始终为空。我可以从引用中看到交互变量基于绑定看到彼此,所以我不确定发生了什么。我环顾四周,发现 this,这表明我稍后将绑定带入我的 UserControl 视图背后的代码,所以我使用了调度程序,但也没有奏效。
查看模型
public class StudentDetailsviewmodel : Mvxviewmodel
{
private InteractionRequest<YesNoQuestion> _interaction = new InteractionRequest<YesNoQuestion>();
public IInteractionRequest Interaction => _interaction;
}
VIEW.XAML.CS
public partial class StudentDetailsView : MvxWpfView
{
private InteractionRequest<YesNoQuestion> _interaction;
public StudentDetailsView()
{
InitializeComponent();
dispatcher.BeginInvoke(new Action(() => BindInteractions()),dispatcherPriority.ContextIdle,null);
}
public InteractionRequest<YesNoQuestion> Interaction
{
get => _interaction;
set
{
if(_interaction != null)
{
_interaction.Requested -= OnInteractionRequested;
}
_interaction = value;
_interaction.Requested += OnInteractionRequested; //***RUN TIME NULL EXCEPTION***
}
}
private void OnInteractionRequested(object sender,InteractionRequestedEventArgs eventArgs)
{
var yesNoQuestion = eventArgs.Callback;
}
private void BindInteractions()
{
var set = this.CreateBindingSet<StudentDetailsView,StudentDetailsviewmodel>();
set.Bind(this).For(view => view.Interaction).To(viewmodel => viewmodel.Interaction).OneWay();
set.Apply();
}
}
互动类
public class YesNoQuestion
{
public bool? Confirmation { get; set; }
public string Question { get; set; }
public YesNoQuestion(string message)
{
Question = message;
}
}
我的第二个问题是,我对他们在示例中使用“ShowDialog”和“DialogStatus”实现的内容有些困惑:
private async void OnInteractionRequested(object sender,MvxValueEventArgs<YesNoQuestion> eventArgs)
{
var yesNoQuestion = eventArgs.Value;
// show dialog
var status = await ShowDialog(yesNoQuestion.Question);
yesNoQuestion.YesNoCallback(status == DialogStatus.Yes);
}
他们是否只是通过 ShowDialog 方法调用另一个用户控件视图来显示自己?
解决方法
_interaction.Requested += OnInteractionRequested; //***RUN TIME NULL EXCEPTION***
不知何故,这在第一次启动时总是为空,然后它会分配适当的交互,因此添加一个空检查来解决这个问题。或许我们需要跟MVVMCross自己确认一下。
其次,您可以处理您想在交互请求中显示的任何内容,例如,显示带有 yes no 按钮类型的 MessageBox 或弹出另一个视图以显示自定义消息框。因为这在 WPF 层上运行。