【问题标题】:What is the difference between null and transparent brush in the Background or Fill背景或填充中的空笔刷和透明笔刷有什么区别
【发布时间】:2013-12-19 08:42:11
【问题描述】:

例如我们有一个边框。这些 XAML 之间有什么区别?

1) Background="透明"

<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Grid
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Border
        BorderBrush="White"
        BorderThickness="2"
        Width="400"
        Height="400"
        Background="Transparent"
        PointerPressed="Border_PointerPressed"
        PointerReleased="Border_PointerReleased" />
</Grid>


2) Background="{x:Null}"

<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Grid
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Border
        BorderBrush="White"
        BorderThickness="2"
        Width="400"
        Height="400"
        Background="{x:Null}"
        PointerPressed="Border_PointerPressed"
        PointerReleased="Border_PointerReleased" />
</Grid>

这两个边框看起来相同。但是有什么区别呢?

【问题讨论】:

    标签: xaml windows-8 windows-store-apps winrt-xaml


    【解决方案1】:

    不同之处在于,如果我们设置 null 背景,Border 将不支持命中测试,这就是为什么 路由事件 像 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,编译应用程序,运行它,在方块内点击。什么都没有发生,因为事件没有上升。处理程序不是调用。

    【讨论】:

    • 区别很重要。如果您不使用透明,则该元素可能不会像您期望的那样与用户交互。
    【解决方案2】:

    为了完整起见,我发现这个链接 http://msdn.microsoft.com/en-us/library/hh758286.aspx#hit_testing 很好地解释了这一点 - 尤其是第二个要点:

    命中测试和输入事件

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

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

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 2018-01-28
      • 1970-01-01
      • 2014-04-24
      • 2017-06-17
      相关资源
      最近更新 更多