我的程序必须有一个白天的配色方案和一个夜间时间的深色配色方案。我已经定义了TimesOfDay枚举:
public enum TimesOfDay { DayTime, NightTime }
我有一个从名为CarSystemDialog的窗口派生的自定义控件:
public class CarSystemDialog : Window {
public static readonly DependencyProperty TimeOfDayModeProperty =
DependencyProperty.Register( "TimeOfDayMode", typeof( TimesOfDay ), typeof( CarSystemDialog ),
new FrameworkPropertyMetadata( TimesOfDay.DayTime,
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback( OnTimeOfDayInvalidated ) ) );
public TimesOfDay TimeOfDayMode {
get { return (TimesOfDay) GetValue( TimeOfDayModeProperty ); }
set { SetValue( TimeOfDayModeProperty, value ); }
}
static CarSystemDialog() {
DefaultStyleKeyProperty.OverrideMetadata( typeof( CarSystemDialog ), new FrameworkPropertyMetadata( typeof( CarSystemDialog ) ) );
}
private void Grid_MouseLeftButtonDown( object sender, MouseButtonEventArgs e ) {
DragMove();
}
private static void OnTimeOfDayInvalidated( DependencyObject d, DependencyPropertyChangedEventArgs e ) {
// Convert the DependencyObject into an AlarmDisplayer instance
CarSystemDialog dialog = (CarSystemDialog) d;
// Let the instance handle the event.
dialog.OnTimeOfDayModeChanged( d, e );
}
public override void OnApplyTemplate() {
base.OnApplyTemplate();
Grid grid = (Grid) Template.FindName( "PART_TitleBar", this );
grid.MouseLeftButtonDown += Grid_MouseLeftButtonDown;
}
public virtual void OnTimeOfDayModeChanged( object sender, DependencyPropertyChangedEventArgs e ) {
}
}
编译好。
我有一个来自CarSystemDialog的UserControl类:
public partial class SettingsDialog : CarSystemDialog {
public static readonly DependencyProperty VolumeProperty =
DependencyProperty.Register( "Volume", typeof( double ), typeof( SettingsDialog ),
new PropertyMetadata( 0.5, new PropertyChangedCallback( OnVolumeInvalidated ) ) );
protected App Application { get; set; }
public double Volume {
get { return (double) GetValue( VolumeProperty ); }
set { SetValue( VolumeProperty, value ); }
}
public SettingsDialog()
: base() {
InitializeComponent();
// Initialize the Application property
Application = (App) App.Current;
// Get the current value of the Volume property
Volume = (double) Application.CurrentUserProfile.Volume;
}
private void AdvancedButton_Click( object sender, RoutedEventArgs e ) {
Application.CurrentUserProfile.Save();
Close();
MainWindow mainWindow = (MainWindow) Application.MainWindow;
mainWindow.ReadsDisplay.Visibility = Visibility.Collapsed;
mainWindow.AdvancedSettingsEditor.Visibility = Visibility.Visible;
e.Handled = true;
}
private void CloseButton_Click( object sender, RoutedEventArgs e ) {
MainWindow window = (MainWindow) Application.MainWindow;
window.Volume = Volume;
Application.ConfigurationFile.Save();
Close();
e.Handled = true;
}
private void DayButton_Click( object sender, RoutedEventArgs e ) {
MainWindow window = (MainWindow) Application.MainWindow;
window.DayButton_Click( sender, e );
e.Handled = true;
}
private void Grid_MouseLeftButtonDown( object sender, MouseButtonEventArgs e ) {
DragMove();
e.Handled = true;
}
public override void OnTimeOfDayModeChanged( object sender, DependencyPropertyChangedEventArgs e ) {
throw new NotImplementedException();
}
private void OnVolumeChanged( object sender, DependencyPropertyChangedEventArgs e ) {
Application.CurrentUserProfile.Volume = (double) e.NewValue;
}
private static void OnVolumeInvalidated( DependencyObject d, DependencyPropertyChangedEventArgs e ) {
SettingsDialog window = (SettingsDialog) d;
window.OnVolumeChanged( window, e );
}
private void NightButton_Click( object sender, RoutedEventArgs e ) {
MainWindow window = (MainWindow) Application.MainWindow;
window.NightButton_Click( sender, e );
e.Handled = true;
}
private void SettingsDialog_Closed( object sender, EventArgs e ) {
Application.CurrentUserProfile.Save();
}
}
}
此类中的所有代码编译都很好,但在运行时,当实例化用户控件时,我收到以下错误:
“MyProject.MyDialog”的类型初始值设定项引发了异常: “TimeOfDayMode”属性已由“MyDialog”注册。
此错误的原因是什么?我该如何解决?
由于
贝
答案 0 :(得分:4)
我发现了问题。在另一个名为CarSystemWindow的类中,我通过复制&来定义相同的属性。从CarSystemDialog粘贴属性的定义。我忘了将此属性副本中的类型名称从CarSystemDialog更改为CarSystemWindow。
我更正了名字,现在没事了。
非常感谢。