【发布时间】:2020-07-19 12:20:13
【问题描述】:
我一直在尝试拦截从我的 Xamarin.Forms UWP 应用中的页面返回的用户,以便阻止它或向他们显示“你确定吗?”对话框。
我已经能够在 ContentPage 的构造函数中使用它来移除导航栏后退按钮:
NavigationPage.SetHasBackButton(this, false);
但是,鼠标上的后退按钮 (XButton1) 仍会导致页面后退。 我尝试在页面上使用它来禁用它:
protected override bool OnBackButtonPressed()
{
return true;
}
这将禁用 Android 之类的硬件后退按钮,但在点击鼠标后退按钮时根本不会调用它。 我还尝试过在 UWP MainPage 上使用 PointerPressed 事件:
public MainPage()
{
this.InitializeComponent();
LoadApplication(new MyApp.App());
this.PointerPressed += MainPage_PointerPressed;
}
private void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e)
{
PointerPoint currentPoint = e.GetCurrentPoint(this);
if (currentPoint.PointerDevice.PointerDeviceType == PointerDeviceType.Mouse)
{
PointerPointProperties pointerProperties = currentPoint.Properties;
if (pointerProperties.IsXButton1Pressed)
{
// back button pressed
}
}
}
对于所有鼠标输入都正确调用此方法除了 XButton1 鼠标后退按钮如果应用程序的当前页面当前位于 NavigationPage 中 - 几乎就像 Xamarin.Forms 一样沿途某处拦截它。在导航页面之外,它可以很好地拾取 XButton1,并且它总是会拾取所有其他输入(包括 XButton2)。
有没有办法拦截或禁用 Xamarin.Forms UWP 应用的 XButton1 后退功能?
【问题讨论】:
-
你能分享你设置XButton1控件的代码吗?
-
我测试了它,它在鼠标后退按钮上正常工作,这里是运行 GIF。 imgur.com/a/aNqjSJ6,这是我的演示。github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/…
-
@LeonLu-MSFT 您的解决方案有效,但它在 Xamarin.Forms 3.4 上。将项目更新到 4.3 后出现问题,因此看起来这些版本之间引入了一项功能。我也尝试了最新的(4.5)并且有相同的。
-
@Saamer 不确定你的意思。我目前没有代码为此设置任何行为。
-
我尝试了 xamarin forms 4.5,我遇到了同样的问题,你可以在 github github.com/xamarin/Xamarin.Forms/issues 中打开一个线程并在那里更新一个重现演示。
标签: c# xamarin xamarin.forms uwp