Xaml 背景或填充中的空笔刷和透明笔刷有什么区别

Xaml 背景或填充中的空笔刷和透明笔刷有什么区别,xaml,windows-8,windows-store-apps,winrt-xaml,Xaml,Windows 8,Windows Store Apps,Winrt Xaml,例如,我们有一个边界。这些Xaml之间有什么区别 1) Background=“Transparent” 2) Background=“{x:Null}” 这两条边界看起来完全相同。但是有什么区别呢?区别在于,如果我们设置null背景,边框将不支持命中测试,这就是为什么路由事件如PonterPressed不会被引发的原因 相反,如果我们将设置为透明则会引发背景事件 为了说明这一点,让我们编写代码 using Windows.UI.Xaml.Media; namespace App

例如,我们有一个边界。这些Xaml之间有什么区别

1) Background=“Transparent”



2) Background=“{x:Null}”



这两条边界看起来完全相同。但是有什么区别呢?

区别在于,如果我们设置null背景,边框将不支持命中测试,这就是为什么路由事件如PonterPressed不会被引发的原因

相反,如果我们将设置为透明则会引发背景事件

为了说明这一点,让我们编写代码

using Windows.UI.Xaml.Media;

namespace App1 {
    public sealed partial class MainPage : Page {
        public MainPage() {
            this.InitializeComponent();
        }

        void Border_PointerPressed(object sender, PointerRoutedEventArgs e) {
            Border border = sender as Border;
            if (border != null)
                border.Background = new SolidColorBrush(Colors.Red);
        }
        void Border_PointerReleased(object sender, PointerRoutedEventArgs e) {
            Border border = sender as Border;
            if (border != null)
                border.Background = new SolidColorBrush(Colors.Transparent);
        }
    }
}
1) 让我们使用第一个XAML,编译我们的应用程序并运行它。试着在广场内部敲击。方形变为红色,因为事件被提升,处理程序调用


2) 现在让我们使用第二个XAML,编译应用程序,运行它,点击方块内部。没有发生任何事情,因为事件没有发生。处理程序不是调用。

不同之处在于,如果我们设置null后台,边框将不支持命中测试,这就是为什么像PonterPressed这样的路由事件不会被引发的原因

相反,如果我们将设置为透明则会引发背景事件

为了说明这一点,让我们编写代码

using Windows.UI.Xaml.Media;

namespace App1 {
    public sealed partial class MainPage : Page {
        public MainPage() {
            this.InitializeComponent();
        }

        void Border_PointerPressed(object sender, PointerRoutedEventArgs e) {
            Border border = sender as Border;
            if (border != null)
                border.Background = new SolidColorBrush(Colors.Red);
        }
        void Border_PointerReleased(object sender, PointerRoutedEventArgs e) {
            Border border = sender as Border;
            if (border != null)
                border.Background = new SolidColorBrush(Colors.Transparent);
        }
    }
}
1) 让我们使用第一个XAML,编译我们的应用程序并运行它。试着在广场内部敲击。方形变为红色,因为事件被提升,处理程序调用


2) 现在让我们使用第二个XAML,编译应用程序,运行它,点击方块内部。没有发生任何事情,因为事件没有发生。处理程序不是调用。

为了完整起见,我发现这个链接很好地解释了这一点-请特别参阅第二个要点:

点击测试和输入事件

确定元素在UI中是否以及在何处对鼠标可见, 触摸和触笔输入称为点击测试。触摸动作和 也适用于特定于交互或操作的事件 触碰动作的后果,一个元件必须在触碰测试中可见 命令作为事件源并激发关联的事件 随着行动。否则,操作将通过元素传递到 可视化树中的任何基础元素或父元素 可以与该输入交互。有几个因素会影响 命中测试,但您可以确定给定元素是否可以触发 通过检查其IsitTestVisible属性来输入事件。此属性 仅当元素满足以下条件时才返回true:

  • 元素的可见性属性值可见
  • 元素的背景或填充属性值不为null。空笔刷值会导致透明度和命中测试不可见性(至) 使元素透明但也可命中测试,请使用透明 画笔而不是null。)注意背景和填充不是由 UIElement,并由不同的派生类定义,例如 作为控制和形状。但是你使用的画笔的含义是什么 前台和后台属性对于命中测试和 输入事件,无论哪个子类实现属性
  • 如果元素是控件,则其IsEnabled属性值必须为true
  • 元素在布局中必须具有实际尺寸。ActualHeight和ActualWidth都为0的元素不会触发输入事件

为了完整起见,我发现这个链接很好地解释了这一点——特别是第二个要点:

点击测试和输入事件

确定元素在UI中是否以及在何处对鼠标可见, 触摸和触笔输入称为点击测试。触摸动作和 也适用于特定于交互或操作的事件 触碰动作的后果,一个元件必须在触碰测试中可见 命令作为事件源并激发关联的事件 随着行动。否则,操作将通过元素传递到 可视化树中的任何基础元素或父元素 可以与该输入交互。有几个因素会影响 命中测试,但您可以确定给定元素是否可以触发 通过检查其IsitTestVisible属性来输入事件。此属性 仅当元素满足以下条件时才返回true:

  • 元素的可见性属性值可见
  • 元素的背景或填充属性值不为null。空笔刷值会导致透明度和命中测试不可见性(至) 使元素透明但也可命中测试,请使用透明 画笔而不是null。)注意背景和填充不是由 UIElement,并由不同的派生类定义,例如 作为控制和形状。但是你使用的画笔的含义是什么 前台和后台属性对于命中测试和 输入事件,无论哪个子类实现属性
  • 如果元素是控件,则其IsEnabled属性值必须为true
  • 元素在布局中必须具有实际尺寸。ActualHeight和ActualWidth都为0的元素不会触发输入事件

差别非常重要。如果您不使用透明,那么元素可能不会像您预期的那样与用户交互。区别非常重要。如果不使用透明,则元素可能不会像预期的那样与用户交互。
using Windows.UI.Xaml.Media;

namespace App1 {
    public sealed partial class MainPage : Page {
        public MainPage() {
            this.InitializeComponent();
        }

        void Border_PointerPressed(object sender, PointerRoutedEventArgs e) {
            Border border = sender as Border;
            if (border != null)
                border.Background = new SolidColorBrush(Colors.Red);
        }
        void Border_PointerReleased(object sender, PointerRoutedEventArgs e) {
            Border border = sender as Border;
            if (border != null)
                border.Background = new SolidColorBrush(Colors.Transparent);
        }
    }
}