none
关于自定义用户空间的问题 RRS feed

  • 问题

  • 拖拽得时候爆错误代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Media;

    namespace WpfApplication1.Study
    {
        public partial class ColorPicker : System.Windows.Controls.UserControl
        {
            public static DependencyProperty ColorProperty;

            public static DependencyProperty GreenProperty;

            public static DependencyProperty RedProperty;

            public static DependencyProperty BlueProperty;

            public static readonly RoutedEvent ColorChangedEvent;

            static ColorPicker()
            {
                #region 注册依赖属性
                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(Colors.Red, new PropertyChangedCallback(OnColorRGBChanged)));

                RedProperty = DependencyProperty.Register("Blue", typeof(byte), typeof(ColorPicker),
                    new FrameworkPropertyMetadata(Colors.Blue, new PropertyChangedCallback(OnColorRGBChanged)));
                RedProperty = DependencyProperty.Register("Green", typeof(byte), typeof(ColorPicker),
                    new FrameworkPropertyMetadata(Colors.Green, new PropertyChangedCallback(OnColorRGBChanged)));
                #endregion

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

            #region 属性
            public Color Color
            {
                get { return (Color)GetValue(ColorProperty); }
                set { this.SetValue(ColorProperty, value); }
            }

            public byte Read
            {
                get { return (byte)GetValue(RedProperty); }
                set { this.SetValue(RedProperty, value); }
            }

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

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

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

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

            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;
            }

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

    不知道为什么,会这样..实在找不出来原因...请教了..

    2012年2月27日 21:31

答案

  • 首先,你的代码里面有两个明显错误 :

    1. 你对RedProperty赋了三遍值

    2. 不应该是  public byte Read 而应该是  public byte Red

    然后,你VS显示这个错误是因为你对byte的属性设置一个Colors.*的默认值,这里除了问题, 应该这么写:

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

    Sincerely,

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年2月28日 3:50
    版主

全部回复

  • 首先,你的代码里面有两个明显错误 :

    1. 你对RedProperty赋了三遍值

    2. 不应该是  public byte Read 而应该是  public byte Red

    然后,你VS显示这个错误是因为你对byte的属性设置一个Colors.*的默认值,这里除了问题, 应该这么写:

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

    Sincerely,

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年2月28日 3:50
    版主
  • 哦..明白了.谢谢了..
    2012年2月29日 11:16