c# - UWP 中的错误或者我错过了什么

标签 c# events win-universal-app uwp windows-10-universal

我正在启动我的第一个通用 Windows 应用程序。我想要的第一件事是子类化主要的“Page”类以进行导航。出于简单的目的,我只想添加一个 RightTapped 事件 Hook 来显示实际显示页面的消息...

无论如何,我创建了一个全新的项目。创建了单个类 MyPage

public class MyPage : Page
{
    public MyPage()
    {
        RightTapped += MyPage_RightTapped;
    }

    private async void MyPage_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {
        var dialog = new MessageDialog("This page is " + GetType(), "What is my form");
        await dialog.ShowAsync();
    }
}

然后在默认的主窗体上,我将 MainPage.xaml 更改为

<Page

<local:MyPage

在代码隐藏中,我改变了

public sealed partial class MainPage : Page

public sealed partial class MainPage

运行表单,它可以工作,右键单击键盘,就会出现消息。

现在问题来了。在主页中的 Grid 声明处,它是用背景定义的...

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

如果我删除此颜色(实际颜色默认为“#FFFFFFFF”)

<Grid>

RightTapped 甚至不再起作用,这是唯一的变化。我放入任何其他颜色的背景,RightTapped 就可以工作。

任何人都可以解释这一点以及为什么它在没有背景颜色的情况下失败,与使用背景相比,背景颜色不应该有任何轴承?

最佳答案

这听起来像是有记录的行为。 UIElement.RightTapped 的文档包含一些相关提示:

For touch actions and also for interaction-specific or manipulation events that are consequences of a touch action, an element must be hit-test visible in order to be the event source and fire the event that is associated with the action. UIElement.Visibility must be Visible. Other properties of derived types also affect hit-test visibility. For more info, see Events and routed events overview.

详细信息来自 Events and routed events overview: Hit testing and input events :

There are several factors that affect hit testing, but you can determine whether a given element can fire input events by checking its IsHitTestVisible property. This property returns true only if the element meets these criteria:

  • The element's Visibility property value is Visible.
  • The element's Background or Fill property value is not null. A nullBrush value results in transparency and hit test invisibility. (To make an element transparent but also hit testable, use a Transparent brush instead of null.)

GridBackground property (继承自 Panel )默认为 null,使没有 Background XAML 属性的网格对于 HitTest 不可见。

关于c# - UWP 中的错误或者我错过了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39024216/

相关文章:

c# - 请求期间使用 WCF 的 System.OutOfMemoryException

c# - RSAProvider.ImportParameters() => 某些特殊 rsa 私钥的错误数据

windows-phone-7 - Windows Phone - 用户控件中的 OnNavigatedTo 和 OnNavigatedFrom

excel - 通过宏的事件代码

javascript - 在 Windows 10 webView 中执行 js 脚本

c# - 如何从 Microsoft.Extensions.Http 包中配置 HttpClient?

c# - XPath:从非根节点选择失败

c# - 获取带有子项的菜单项的点击事件 (C#)

xaml - 自定义 Windows 10 通用应用程序主题

windows - 如何将点击事件添加到来自通知中心的 Toast 通知?