【发布时间】:2020-02-06 10:38:44
【问题描述】:
对于我当前的项目,我需要在 Xamarin.Forms 中捕获按钮按下和释放事件。但我想使用 Prism 保持松散耦合。
一开始我使用了 的 Command 属性,如下所示:
<Button x:Name="ButtonForward" Command="{Binding MoveUpCommand}" />
但 Command 属性仅在释放按钮时触发。为了使按下和释放的动作分开,我使用了 XAML 中的事件:
<Button x:Name="ButtonForward" Pressed="ButtonForward_Pressed" Released="ButtonMove_Released"/>
并在后面的代码中的事件处理程序中手动调用命令:
private void ButtonMove_Released(object sender, System.EventArgs e)
{
var vm = BindingContext as DirectControlViewModel;
if (vm.MoveStopCommand.CanExecute(null))
vm.MoveStopCommand.Execute(null);
}
private void ButtonForward_Pressed(object sender, System.EventArgs e)
{
var vm = BindingContext as DirectControlViewModel;
if (vm.MoveUpCommand.CanExecute(null))
vm.MoveUpCommand.Execute(null);
}
问题在于它不再是松散耦合的,因为 View 现在必须知道它的 ViewModel。 有没有办法让按钮对按下和释放事件有单独的命令,保持 View 和 ViewModel 松散耦合?任何帮助将不胜感激。
【问题讨论】:
-
为什么不像正常的 (wpf-)xaml 那样使用行为?
标签: c# xaml xamarin.forms prism