CustomControl 在清理后可见,但在重建后不可见

CustomControl is visible after clean but not after rebuild

几周前我开始使用 WPF,所以我是新手。 我试图创建一个用户控件。

但它在 Window 中不可见。 如果我清理项目,UserControl 是可见的,并且有一个带有文本的警告标志:

"This document contains one or more controls which have been changed. Rebuild the project to show the changes in the design view."

当我重建或运行(包括重建)时,UserControl 消失。

那就是 XAML

    <Slider Name="sliderRed" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Minimum="0" Maximum="255" Value="{Binding ElementName=colorPicker, Path=Red}"/>
    <Slider Name="sliderGreen" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Minimum="0" Maximum="255" Value="{Binding ElementName=colorPicker, Path=Green}"/>
    <Slider Name="sliderBlue" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Top" Minimum="0" Maximum="255" Value="{Binding ElementName=colorPicker, Path=Blue}"/>

    <Rectangle Grid.Column="1" Grid.RowSpan="3">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding ElementName=colorPicker,Path=Color}"/>
        </Rectangle.Fill>
    </Rectangle>

而这个是XAML后面的Class:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CustomControls
{
public partial class ColorPicker : UserControl
{
    public static DependencyProperty ColorProperty;

    public static DependencyProperty RedProperty;
    public static DependencyProperty GreenProperty;
    public static DependencyProperty BlueProperty;

    public static readonly RoutedEvent ColorChangedEvent;

    public Color Color
    {
        get { return (Color)GetValue(ColorProperty); }
        set { SetValue(ColorProperty, value); }
    }

    public byte Red
    {
        get { return (byte)GetValue(RedProperty); }
        set { SetValue(RedProperty, value); }
    }

    public byte Green
    {
        get { return (byte)GetValue(GreenProperty); }
        set { SetValue(GreenProperty, value); }
    }

    public byte Blue
    {
        get { return (byte)GetValue(BlueProperty); }
        set { SetValue(BlueProperty, value); }
    }

    public event RoutedPropertyChangedEventHandler<Color> ColorChanged
    {
        add { AddHandler(ColorChangedEvent, value); }
        remove { RemoveHandler(ColorChangedEvent, value); }
    }

    static ColorPicker()
    {
        ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPicker), new FrameworkPropertyMetadata(Colors.Black, new PropertyChangedCallback(OnColorChanged)));

        RedProperty = DependencyProperty.Register("Red", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRGBChanged)));
        GreenProperty = DependencyProperty.Register("Green", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRGBChanged)));
        BlueProperty = DependencyProperty.Register("Blue", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRGBChanged)));

        ColorChangedEvent = EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<Color>), typeof(ColorPicker));
    }

    private static void OnColorRGBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        ColorPicker colorPicker = (ColorPicker)sender;
        Color color = colorPicker.Color;

        if (e.Property == RedProperty)
        {
            color.R = (byte)e.NewValue;
        }
        else if (e.Property == GreenProperty)
        {
            color.G = (byte)e.NewValue;
        }
        else if (e.Property == BlueProperty)
        {
            color.B = (byte)e.NewValue;
        }

        colorPicker.Color = color;
    }

    private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        Color newColor = (Color)e.NewValue;
        Color oldColor = (Color)e.OldValue;

        ColorPicker colorPicker = (ColorPicker)sender;
        colorPicker.Red = newColor.R;
        colorPicker.Green = newColor.G;
        colorPicker.Blue = newColor.B;

        RoutedPropertyChangedEventArgs<Color> args = new RoutedPropertyChangedEventArgs<Color>(oldColor, newColor);
        args.RoutedEvent = ColorPicker.ColorChangedEvent;

        colorPicker.RaiseEvent(args);

    }
}

}

这是我在 Main 中实现 UserControl 的方式Window:

<Window x:Class="CustomControls.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CustomControls"
    x:Name="mainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Background="ForestGreen">
<Grid>
    <local:ColorPicker Grid.Column="0" Grid.Row="0" Height="300" Width="300"/>
</Grid>

我已经尝试将 XAML 放入 MainWindow 以查看是否失败但并没有失败,我尝试创建一个产生相同结果的空自定义用户控件喜欢拾色器。

好的,问题来了:您将 ctor 设置为静态,以注册所有 DependencyProperties,因此您不再拥有通常的 class ctor呼叫 InitializeComponents()。所以在你的 class 中添加这个 ctor:

public ColorPicker()
{
    this.InitializeComponent();
}

要了解为什么要进行此调用,请访问 this page