WPF。如果出现弹出窗口,主窗口亮度会降低//代码隐藏

时间:2015-08-03 11:47:31

标签: wpf popup brightness mainwindow

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

实施例: enter image description here

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

代码:

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: 我有同样的问题,只是我的代码是变化的。现在我为弹出窗口创建了新的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;
        }
    } 
}

我的Mainwindow.xaml.cs:

namespace uploader
{

    public partial class MainWindow : Window
    {

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

2 个答案:

答案 0 :(得分:1)

我通过使用具有黑色背景和不透明度的画布

在我的所有WPF应用程序中执行此操作

示例:

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

答案 1 :(得分:0)

使用当前代码,您需要处理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事件中取消订阅(有助于防止内存泄漏)