WPF。如果弹出 window 出现,主要 window 亮度降低 //code-behind

WPF. if pop up window appear, main window brightness decrease //code-behind

我需要如果我的弹出 window 出现(点击后),主要 window 亮度必须降低,也许有人知道怎么做?

示例:

编辑:我创建了canvas,但不知道如何使用它,亮度需要降低然后弹出出现。

代码:

private void sample_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            string path1 = System.AppDomain.CurrentDomain.BaseDirectory + "../../loader_bg.png";
            string path2 = System.AppDomain.CurrentDomain.BaseDirectory + "../../loader.gif";

            ImageBrush myBrush = new ImageBrush();
            Image image = new Image();
            image.Source = new BitmapImage(
                new Uri(path1));
            myBrush.ImageSource = image.Source;

            Image ima = new Image();
            MediaElement gif = new MediaElement();

            ima.Source = new BitmapImage(new Uri(path1));
            gif.Source=new Uri(path2);

            gif.Height = 72;
            gif.Width = 72;

            var pop = new Popup
            {
                IsOpen = true,
                StaysOpen = false,
                AllowsTransparency = true,
                VerticalOffset = 350,
                HorizontalOffset = 700,
                Height = 128,
                Width = 128,

            };
            Canvas c=new Canvas();
            c.Background=Brushes.Black;
            c.Opacity = 0.6;


            Grid p = new Grid();
            p.Background = myBrush; 

            //p.Children.Add(ima);
            //p.Children.Add(c);
            p.Children.Add(gif);
            pop.Child = p;


        }
    }

编辑 2: 我有同样的问题,只是我的代码发生了变化。现在我为弹出 window 创建了新的 xaml.cs,并尝试达到相同的目的,但我没有得到相同的结果(我说的是亮度降低)。 这是我的新 xaml.cs :

namespace uploader
{
    /// <summary>
    /// Interaction logic for PopupPanel.xaml
    /// </summary>
    public partial class PopupPanel : UserControl
    {
        private Popup _currentPopup;
        public PopupPanel()
        {
            InitializeComponent();

            string path1 = System.AppDomain.CurrentDomain.BaseDirectory + "../../loader_bg.png";
            string path2 = System.AppDomain.CurrentDomain.BaseDirectory + "../../loader.gif";

            ImageBrush myBrush = new ImageBrush();
            Image image = new Image();
            image.Source = new BitmapImage(new Uri(path1));
            myBrush.ImageSource = image.Source;


            MediaElement gif = new MediaElement();

            gif.Source=new Uri(path2);

            gif.Height = 72;
            gif.Width = 72;

            _currentPopup = new Popup
            {

                StaysOpen = false,
                AllowsTransparency = true,
                VerticalOffset = 350,
                HorizontalOffset = 700,
                Height = 128,
                Width = 128,

            };
            Overlay.Visibility = Visibility.Visible;

            _currentPopup.Closed += PopupClosing;
            _currentPopup.IsOpen = true;

            Grid p = new Grid();
            p.Background = myBrush; 
            p.Children.Add(gif);

            _currentPopup.Child = p;

        }
        private void PopupClosing(object sender, EventArgs e)
        {
            _currentPopup.Closed -= PopupClosing;
            _currentPopup = null;

            Overlay.Visibility = Visibility.Collapsed;
        }
    } 
}

我的主要window.xaml.cs:

namespace uploader
{

    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }
        private void sample_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PopupPanel pop = new PopupPanel();
        }
...

我在我的所有 WPF 应用程序中都使用 Canvas 黑色背景和不透明度

示例:

<Window>
    <Grid>
        <!--Main content-->
        <UserControl/>
        <Grid>
            <Canvas Background="Black" Opacity="0.6"/>
            <!--Overlay content-->
            <UserControl VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>         
    </Grid>
</Window>

使用您当前的代码,您需要处理 Canvas 叠加层的可见性。

在您的 XAML 中定义它更容易,如下所示:

<Window>
    <Grid>
        <!--Main content-->
        <UserControl/>
        <Grid>
            <Canvas x:Name="Overlay" 
                    Background="Black" 
                    Opacity="0.6"
                    Visibility="Collapsed"/>
            <!--Overlay content-->
            <UserControl VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>         
    </Grid>
</Window>

然后,在您的代码隐藏中,您可以设置弹出窗口打开前和关闭时的可见性:

Popup _currentPopup;

private void sample_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ...

    _currentPopup = new Popup
    {
        StaysOpen = false,
        AllowsTransparency = true,
        VerticalOffset = 350,
        HorizontalOffset = 700,
        Height = 128,
        Width = 128
    };

    Overlay.Visibility = Visibility.Visible;

    _currentPopup.Closed += PopupClosing;
    _currentPopup.IsOpen = true;

}

private void PopupClosing(object sender, EventArgs e)
{
    _currentPopup.Closed -= PopupClosing;
    _currentPopup = null;

    Overlay.Visibility = Visibility.Collapsed;
}

请注意,我正在使用局部变量来保留对弹出窗口的引用。这样我就可以取消订阅 Closing 事件(有助于防止内存泄漏)