私のプログラムは、昼間のための1つの配色と夜間の時間のためのより暗い配色を持たなければなりません。派生クラスの "DependencyProperty already defined"エラー?
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) {
}
}
これは罰金コンパイル:
public enum TimesOfDay { DayTime, NightTime }
は私がCarSystemDialogと呼ばれるウィンドウから派生したカスタムコントロールを持っている:私はTimesOfDay列挙型を定義しています。
私はCarSystemDialogから下降ユーザーコントロールクラスを持っている:
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();
}
}
}
このクラスのコードのすべてがうまくコンパイルが、ユーザーコントロールがインスタンス化された実行時間、で、私が手次のエラー:
The type initializer for "MyProject.MyDialog" threw an exception: "TimeOfDayMode" property was already registered by "MyDialog".
このエラーの原因は何ですか?どうすれば修正できますか?
おかげ
トニー
同じ問題がありました。これは、同じタイプのプロパティが定義されているかどうかを確認することを思い出しました。ありがとう! +1 –