如何使用依赖项属性替换UserControl的构造函数中的参数? [英] how to use dependency property to replace the parameter in the constructor of a UserControl?

查看:85
本文介绍了如何使用依赖项属性替换UserControl的构造函数中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到之前曾问过类似的问题,但我没有找到任何详细的示例。



我有一个winform程序,其构造函数有一个参数cn:

  public AddFailure(ProSimConnect cn)// winform中的构造函数
{
this.connection = cn;
InitializeComponent();
...
}

现在我需要在UserControl中对其进行模拟在WPF中将其放入MainWindow中。



在MainWindow.xaml中:

 <边界...> 
< IOS:Core_System />
< / Border>

我想这样做,但我知道不能,因为它不应该有任何参数:

  public Core_System(ProSimConnect cn)// UserControl中的构造函数
{
this.cn = connection;
InitializeComponent();
...
}

因此,我尝试使用依赖项属性:

 公共局部类Core_System:UserControl 
{
ProSimConnect连接;

//依赖属性
public ProSimConnect cn
{
get {return(ProSimConnect)GetValue(connectionProperty); }
set {SetValue(connectionProperty,value); }
}
公共静态只读DependencyProperty connectionProperty =
DependencyProperty.Register( cn,typeof(ProSimConnect),typeof(Core_System));

// UserControl中的构造函数
public Core_System()
{
this.connection = cn;
InitializeComponent();
...
}
...
}

它不起作用-报告空异常。我哪里错了?谢谢。



这是需要在UserControl的构造函数中使用参数的函数:

  Failure [] getSelectedFailures()
{
return cn.getFailures()。Where(failure => failure_name.Contains(failure.name))。ToArray();
}

我称之为的位置是:

 公共部分类Core_System:UserControl 
{
...
private void button_Engine_1_On_Fire(对象发送者,RoutedEventArgs e)
{
...
ArmedFailure.create(getSelectedFailures());
}
}


解决方案

A



您可以从构造函数中移动任何使用 ProSimConnect 的代码。 UserControl 到依赖项属性回调:

 公共部分类Core_System :UserControl 
{
ProSimConnect连接;

//依赖性属性
public ProSimConnect cn
{
get {return(ProSimConnect)GetValue(connectionProperty); }
set {SetValue(connectionProperty,value); }
}
公共静态只读DependencyProperty connectionProperty =
DependencyProperty.Register( cn,typeof(ProSimConnect),typeof(Core_System),new PropertyMetadata(new PropertyChangedCallback(OnPropertySet));

私有静态无效OnPropertySet(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
Core_System ctrl = d作为Core_System;
ctrl.connection = e.NewValue作为ProSimConnect;
// ...
}

// UserControl中的构造函数
public Core_System()
{
InitializeComponent();
}
}

只要将依赖项属性设置为一个值,就会调用该回调。 / p>

I noticed that similar questions have been asked before but I did not find any detailed examples.

I have a winform program, its constructor has a parameter cn:

    public AddFailure(ProSimConnect cn)// constructor in winform
    {
      this.connection = cn;
      InitializeComponent();
      ...
    }

Now I need to simulate it in a UserControl in WPF and put it into MainWindow.

In MainWindow.xaml:

   <Border ...>
   <IOS:Core_System/>
   </Border>

I want to do this but I know I can't because it shouldn't have any parameter:

    public Core_System(ProSimConnect cn)// constructor in UserControl
    {            
        this.cn = connection;
        InitializeComponent();
        ...
    }

Therefore I try to use dependency property:

 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();
      ...
  }
      ...
}

It doesn't work - it reports "null" exception. Where am I wrong? Thanks.

This is the function that need to use the parameter in the constructor of the UserControl:

    Failure []getSelectedFailures()
    {
        return cn.getFailures().Where(failure => failure_name.Contains(failure.name)).ToArray();
    }

The location I call it is:

public partial class Core_System : UserControl
{
  ...
    private void button_Engine_1_On_Fire(object sender, RoutedEventArgs e)
    {
       ...
       ArmedFailure.create(getSelectedFailures());
    }
}

解决方案

A dependency property cannot be set before the constructor has returned.

You could move any code that uses ProSimConnect from the constructor of the UserControl to a dependency property callback:

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

The callback will be invoked whenever the dependency property is set to a value.

这篇关于如何使用依赖项属性替换UserControl的构造函数中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆