【问题标题】:WPF. if pop up window appear, main window brightness decrease //code-behindWPF。如果弹出窗口出现,主窗口亮度降低 //code-behind
【发布时间】:2015-08-03 11:47:30
【问题描述】:

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

示例:

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

代码:

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();
        }
...

【问题讨论】:

    标签: wpf popup brightness mainwindow


    【解决方案1】:

    我在我所有的 WPF 应用程序中都使用黑色背景和不透明度的画布来执行此操作

    例子:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我更改了我的代码,但此代码不再起作用,我为弹出窗口创建了新的 xaml.cs,它出现了,但它不会降低亮度。我编辑了我的帖子(编辑 2),也许你知道如何解决它?
      猜你喜欢
      • 2013-01-11
      • 2012-07-15
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      相关资源
      最近更新 更多