应用程序屏幕UWP上的半透明层

时间:2018-09-20 07:02:16

标签: c# uwp uwp-xaml

是否可以为UWP中的应用程序添加半透明层? 在普通的.NET中有可能,但是在UWP中我找不到它。

我需要这种行为,以使除了一个控件之外的整个应用程序处于非活动状态,并在控件旁边显示工具提示。应用中需要教程

谢谢大家!

1 个答案:

答案 0 :(得分:1)

我在想您需要某种BlockUI / UnBlockUI行为,不确定是否有第三方,但是您可以发挥创造力来实现这一目标。

利用 ZIndex 不透明度 DependencyProperty 和某些事件处理,您可以实现这一目标。

xaml

<Page x:Name="root"
     x:Class="App1.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
      d:DesignHeight="450" d:DesignWidth="800">
    <Canvas Background="AliceBlue">
        <Rectangle  Canvas.Left="30" Canvas.ZIndex="{Binding BlockerZIndex, ElementName=root}"  Width="180" Height="200" Fill="White" Opacity="0.5"/>
        <Button Canvas.ZIndex="1" Canvas.Left="30" Canvas.Top="70" Content="Send Email" Width="150" Height="50" Background="Blue" Foreground="Black" />
        <Button Canvas.ZIndex="1"  Canvas.Left="30" Canvas.Top="140" Content="Print Receipt" Width="150" Height="50" Background="Blue" Foreground="Black" />
        <Button x:Name="BlockUnBlockUIButton" Canvas.Left="30" Canvas.ZIndex="1" Canvas.Top="210" Width="150" Height="50" Background="Blue" Foreground="Black" Click="BlockUnBlockUIButton_OnClick" >
            <TextBlock Text="Block/UnBlock UI" ToolTipService.ToolTip="Block/Unblock UI"/>
        </Button>
    </Canvas>
</Page>

隐藏代码

using Windows.UI.Xaml;

namespace App1
{
    public sealed partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty BlockerZIndexProperty =
            DependencyProperty.Register(
                nameof(BlockerZIndex),
                typeof(int),
                typeof(MainPage),
                new PropertyMetadata(2));

        public int BlockerZIndex
        {
            get => (int)GetValue(BlockerZIndexProperty);
            set => SetValue(BlockerZIndexProperty, value);
        }

        private void BlockUnBlockUIButton_OnClick(object sender, RoutedEventArgs e)
        {
            BlockerZIndex = BlockerZIndex == 1 ? 2 : 1;
        }
    }
}

enter image description here


enter image description here