如何使用依赖属性替换UserControl的构造函数中的参数?

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

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

问题描述

我注意到以前有人问过类似的问题,但我没有找到任何详细的例子.

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

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

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

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

现在我需要在 WPF 中的 UserControl 中模拟它并放入 MainWindow.

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

在 MainWindow.xaml 中:

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

它不起作用 - 它报告null"异常.我哪里错了?谢谢.

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

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

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

我称它的位置是:

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.

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

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的构造函数中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用依赖属性替换UserControl的构造函数中的参数?