C# 如何使用dependency属性替换UserControl构造函数中的参数?
我注意到以前也有人问过类似的问题,但我没有找到任何详细的例子 我有一个winform程序,它的构造函数有一个参数cn:C# 如何使用dependency属性替换UserControl构造函数中的参数?,c#,wpf,constructor,user-controls,C#,Wpf,Constructor,User Controls,我注意到以前也有人问过类似的问题,但我没有找到任何详细的例子 我有一个winform程序,它的构造函数有一个参数cn: public AddFailure(ProSimConnect cn)// constructor in winform { this.connection = cn; InitializeComponent(); ... } 现在我需要在WPF中的UserControl中模拟它,并将其放入MainWindow 在M
public AddFailure(ProSimConnect cn)// constructor in winform
{
this.connection = cn;
InitializeComponent();
...
}
现在我需要在WPF中的UserControl中模拟它,并将其放入MainWindow
在MainWindow.xaml中:
<Border ...>
<IOS:Core_System/>
</Border>
因此,我尝试使用dependency属性:
public partial class Core_System : UserControl
{
ProSimConnect connection;
//dependency property
public ProSimConnect cn
{
get { return (ProSimConnect) GetValue(connectionProperty); }
set { SetValue(connectionProperty, value); }
}
public static readonly DependencyProperty connectionProperty =
DependencyProperty.Register("cn", typeof(ProSimConnect),typeof(Core_System));
// constructor in UserControl
public Core_System()
{
this.connection = cn;
InitializeComponent();
...
}
...
}
它不工作-它报告“null”异常。我错在哪里?谢谢
这是需要在UserControl的构造函数中使用参数的函数:
Failure []getSelectedFailures()
{
return cn.getFailures().Where(failure => failure_name.Contains(failure.name)).ToArray();
}
我称之为的地点是:
public partial class Core_System : UserControl
{
...
private void button_Engine_1_On_Fire(object sender, RoutedEventArgs e)
{
...
ArmedFailure.create(getSelectedFailures());
}
}
在构造函数返回之前,无法设置依赖项属性
您可以将使用
ProSimConnect
的任何代码从UserControl
的构造函数移动到依赖项属性回调:
public partial class Core_System : UserControl
{
ProSimConnect connection;
//dependency property
public ProSimConnect cn
{
get { return (ProSimConnect)GetValue(connectionProperty); }
set { SetValue(connectionProperty, value); }
}
public static readonly DependencyProperty connectionProperty =
DependencyProperty.Register("cn", typeof(ProSimConnect), typeof(Core_System), new PropertyMetadata(new PropertyChangedCallback(OnPropertySet));
private static void OnPropertySet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Core_System ctrl = d as Core_System;
ctrl.connection = e.NewValue as ProSimConnect;
//...
}
// constructor in UserControl
public Core_System()
{
InitializeComponent();
}
}
只要将依赖项属性设置为值,就会调用回调。在构造函数返回之前无法设置依赖项属性…您好@mm8,您的意思是我需要将其放在构造函数之后吗?您可以将使用ProSimConnect的任何代码从UserControl的构造函数移动到依赖项属性回调。看看我的答案。谢谢。我已经编辑了我的问题。在您为我添加的OnPropertySet函数中,我应该将省略项放在“return cn.getFailures().Where(failure=>failure_name.Contains(failure.name)).ToArray()”,并将函数类型从“void”更改为“failure[]”?很抱歉,我以前从未使用过dependency属性。您在哪里调用getSelectedFailures()?您好,我在UserControl xaml的类core_system-对应的.xaml.cs中调用它。我已经在我的问题中添加了它。多谢各位@MM8那么这与ProSimConnect和您最初的问题有什么关系。。。?
public partial class Core_System : UserControl
{
ProSimConnect connection;
//dependency property
public ProSimConnect cn
{
get { return (ProSimConnect)GetValue(connectionProperty); }
set { SetValue(connectionProperty, value); }
}
public static readonly DependencyProperty connectionProperty =
DependencyProperty.Register("cn", typeof(ProSimConnect), typeof(Core_System), new PropertyMetadata(new PropertyChangedCallback(OnPropertySet));
private static void OnPropertySet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Core_System ctrl = d as Core_System;
ctrl.connection = e.NewValue as ProSimConnect;
//...
}
// constructor in UserControl
public Core_System()
{
InitializeComponent();
}
}