本文介绍了背景或填充中的空画笔和透明画笔有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For example we have a Border. What the difference beetween these XAMLs?

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>

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

Both of these borders looks identical. But what the difference?

推荐答案

区别在于如果我们设置 null 背景,Border 将不支持命中测试,这就是为什么路由事件strong> 像 PonterPressed 不会被提升.

The difference is if we set null background the Border will not support hit-testing, that's why routed events like PonterPressed will not be raised.

相反,如果我们设置透明背景事件将被引发.

Conversely though, if we set Transparent background events will be raised.

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

To illustrate this let's write code-behind.

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,编译我们的应用程序并运行它.尝试在正方形内部轻敲.方块变成红色是因为事件上升并且处理程序调用.

1) Let's use the first XAML, compile our app and run it. Try to tap inside the square. The square becomes red because the events are rised and the handlers calls.

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

2) Now let's use the second XAML, compile the app, run it, tap inside the square. Nothing happens because the events are not rised. The handlers are not calls.

这篇关于背景或填充中的空画笔和透明画笔有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 09:05