【问题标题】:Configuring overlay background colour for ContentDialog为 ContentDialog 配置覆盖背景颜色
【发布时间】:2015-11-16 12:57:44
【问题描述】:

我正在使用深色主题为通用 Windows 平台编写应用程序,我注意到虽然我在使用 ContentDialog 类显示模式对话框时正确地将请求的主题设置为深色,但覆盖变亮了整个页面,而不是使其变暗。

显示对话框之前:

显示对话框:

由于ContentDialog 上没有属性来控制覆盖,我该如何覆盖正在使用的颜色?

【问题讨论】:

    标签: xaml windows-runtime winrt-xaml windows-10 uwp


    【解决方案1】:

    经过一些实验,我发现用于控制上面显示ContentDialog 的叠加层颜色的画笔是SystemControlPageBackgroundBaseMediumBrush,而不是看起来更可能的ContentDialogDimmingThemeBrush

    通过检查默认主题定义,可以发现浅色和深色主题都将此画笔设置为颜色资源SystemBaseMediumColor,浅色主题为#99000000,深色主题为#99FFFFFF。这会导致叠加层使浅色主题变暗而使深色主题变亮。

    由于SystemBaseMediumColor 是其他画笔定义的引用,例如用于非活动枢轴标题的那些定义,因此有必要覆盖SystemControlPageBackgroundBaseMediumBrush,而不是它仅针对深色主题引用的颜色。

    为此,我们需要在 App.xaml 的资源主题字典中或在合并到 App.xaml 的资源 XAML 文件中重新定义画笔,如下所示:

    <Application>
    
        <Application.Resources>
    
            <ResourceDictionary>
    
                <ResourceDictionary.ThemeDictionaries>
    
                    <ResourceDictionary x:Key="Dark">
    
                        <SolidColorBrush 
                            x:Key="SystemControlPageBackgroundBaseMediumBrush" 
                            Color="#99000000"
                            />
    
                    </ResourceDictionary>
    
               </ResourceDictionary.ThemeDictionaries>
    
           </ResourceDictionary>
    
        </Application.Resources>
    
    </Application>
    

    【讨论】:

    • 做得很好,现在我们详细介绍了,您是否看到应用程序的窗口调整大小时具有空白的白色背景并且位于根框架下方(因为我在根中设置了一个图像框架)。有什么想法可以改变这种颜色吗?我认为是内部的。除此之外,我必须看看如何在触摸边框时关闭内容对话框,我知道我将不得不编辑模板。
    【解决方案2】:

    我使用了已接受答案的代码,但是更改此画笔的颜色对我有用...“SystemControlPageBackgroundMediumAltMediumBrush”也许是因为我在阅读here时使用的是周年纪念版

    还要确保您的资源字典键与您使用的主题相匹配。我使用的是“Light”主题,所以我将资源字典 x:key 更改为这个...

    &lt;ResourceDictionary x:Key="Light"&gt;

    【讨论】:

      【解决方案3】:

      试试下面的代码。

          /// <summary>
          /// Set the Overlay background for content Dialog
          /// </summary>
          /// <param name="subTree">Content Dialog reference</param>
          public static void SetContentDialogOverlay(UIElement subTree)
          {
      
              var hostparent = VisualTreeHelper.GetParent(subTree);
              var rect = FindVisualChild<Rectangle>(hostparent);
              rect.Fill = new SolidColorBrush(Colors.Black);
              rect.Opacity = 0.7;
      
          }
      
          /// <summary>
          /// Find the child element from UIContainer
          /// </summary>
          /// <typeparam name="T"> Type</typeparam>
          /// <param name="depObj"> Dependency Reference </param>
          /// <returns></returns>
          public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
          {
              if (depObj != null)
              {
                  for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                  {
                      DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                      if (child != null && child is T)
                      {
                          return (T)child;
                      }
      
                      T childItem = FindVisualChild<T>(child);
                      if (childItem != null) return childItem;
                  }
              }
              return null;
          }
      

      现在,像这样在你的代码后面调用上面的方法----

         // Based upon your access modifier i.e. public/private or protected
          SetContentDialogOverlay(this); 
      

      这里,“this”表示内容对话框引用,或者您可以传递 ContectDialog 的对象引用。

      希望这将帮助您更改叠加层的颜色。 快乐编码.. :)

      【讨论】:

        【解决方案4】:

        我在 App.xaml 中使用了以下代码。它对我很有效。

        <Application.Resources> <SolidColorBrush x:Key="SystemControlPageBackgroundMediumAltMediumBrush" Color="#00000000" /> </Application.Resources>

        【讨论】:

          猜你喜欢
          • 2013-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多