XAML 编辑器的黑色背景 [英] Black Background for XAML Editor

查看:49
本文介绍了XAML 编辑器的黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究具有白色文本和透明背景的用户控件.不幸的是,因为 VS2010 中的 XAML 设计视图具有白色背景,所以我看不到我正在设计的任何东西!

I am currently working on a user control that has white text and a transparent background. Unfortunately because the XAML design view within VS2010 has a white background I cannot see anything that I am designing!

我已经浏览了我能想到的所有设置对话框,但一直无法找到更改 XAML 设计器背景颜色的设置.

I have been through all the settings dialogs I can think of, but have been unable to find a setting that changes the background colour of the XAML designer.

有谁知道如何做到这一点?

Does anyone know how this can be done?

推荐答案

在 XAML 中,将背景设置为黑色.然后在您的用户控件中,使用 DesignerProperties 在运行时设置背景:

In your XAML, set your background to black. Then in your user control, use the DesignerProperties to set the background at runtime:

XAML

<UserControl .... Background="Black" .... >

背后的代码

public YourUserControl()
{
  InitializeComponent();

  if( !System.ComponentModel.DesignerProperties.GetIsInDesignMode( this ) )
  {
    this.Background = Brushes.Transparent;
  }

}


用户控制:

在您的用户控件中,不要声明背景颜色:

In your user control, do not declare a background color:

<UserControl ... namespaces ...>

背后的用户控制代码:

在您的用户控件的构造函数中,使用上面的 DesignTime 方法,但要检查它是否为设计模式(与其他方法相反):

In your user control's constructor, use the DesignTime method as above, but check to see if it is Design Mode (opposite check from other method):

public YourUserControl()
{
  InitializeComponent();

  if( System.ComponentModel.DesignerProperties.GetIsInDesignMode( this ) )
  {
    this.Background = Brushes.Black;
  }

}

App.xaml:

最后,在你的 App.xaml 中,添加一个样式来为 UserControls 设置背景颜色:

Finally, in your App.xaml, add a style to set a background color for UserControls:

<Application.Resources>
  <Style TargetType="{x:Type UserControl}">
    <Setter Property="Background" Value="Black" />
  </Style>
</Application.Resources>

这是正在发生的事情:

  1. App.xaml 将在设计时影响 UserControl,因为类型化样式会自动应用于对象,但它应用于派生对象(在本例中为 UserControl).所以,在设计时,VS 认为它应该应用该样式,但在运行时,它会被忽略.
  2. 在使用 UserControl 的窗口中查看控件时,GetIsInDesignMode 检查将影响 UserControl,因为 VS 在设计时编译 UserControl 以便在可视化设计器中呈现它.
  1. The App.xaml will effect the UserControl at design time because a typed style is applied on an object automatically, but it is not applied to a derived object (UserControl in this case). So, at design time, VS thinks it should apply the style, but at runtime, it will be ignored.
  2. The GetIsInDesignMode check will effect the UserControl when viewing the control in a Window that is using the UserControl because VS is compiling the UserControl at design time in order to render it in the Visual Designer.

HTH

这篇关于XAML 编辑器的黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆