基函数的奇怪行为

本文关键字:基函数 | 更新日期: 2023-09-27 18:25:37

我有ObservableCollection开关,可以容纳两种不同类型的FirstDerivedTypeSecondDerivedType。二者在CCD_ 3上都有借鉴意义。主要的问题是为什么当我在构造函数FirstDerivedType() 中创建PlotModel的实例时

Data.PlotModel = new PlotModel();

它运行良好。但是,当我在base.Draw(Data.PlotModel);中创建PlotModel的实例时,我会在DataContext中获得null。为什么?

namespace WpfApplication3
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new CurrentViewModel();
    }
}
public class CurrentViewModel
{
    private ObservableCollection<BaseType> items;
    public ObservableCollection<BaseType> Items
    {
        get
        {
            return this.items;
        }
        set
        {
            this.items = value;
        }
    }
    public CurrentViewModel()
    {
        Items = new ObservableCollection<BaseType>();
        Items.Add(new FirstDerivedType (17));
        Items.Add(new FirstDerivedType (100));
        Items.Add(new FirstDerivedType (50));
        Items.Add(new FirstDerivedType (120));
    }
}
public class Data:INotifyPropertyChanged
{
    public int ID { get; set; }
    public PlotModel PlotModel { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string info)
    {
        if (PropertyChanged!=null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
public abstract class BaseType
{
    public virtual void Draw(PlotModel PlotModel)
    {
        PlotModel = new OxyPlot.PlotModel(); //do not Working
        LineSeries LS= new LineSeries();
        Random rnd = new Random();
        for (int i=0; i<10; i++)
        {
            LS.Points.Add(new DataPoint(i,rnd.Next(1,100)));
        }
        PlotModel.Series.Add(LS);
        PlotModel.InvalidatePlot(false);
    }
}
public class FirstDerivedType : BaseType
{
    public FirstDerivedType(int ID)
    {
        Data = new Data();
        //Data.PlotModel = new PlotModel(); //Working
        Data.ID = ID;
        base.Draw(Data.PlotModel);
    }
    public Data Data { get; set; }
}
public class SecondDerivedType : BaseType
{
    public Data Data { get; set; }
}
}

基函数的奇怪行为

在不起作用的情况下,您没有设置类型为Data的实例的属性PlotModel,您只是将方法参数PlotModel的值设置为PlotModel的新实例。请注意,Data.PlotModel仍然为空。

此行:

public virtual void Draw(PlotModel PlotModel)
{
    PlotModel = new OxyPlot.PlotModel(); //do not Working

将新实例分配给方法Draw的参数PlotModelNOT实例Data的属性PlotModel。它不影响持有属性PlotModel的实例Data。此行:

base.Draw(Data.PlotModel);

如果在CCD_ 22方法中重新分配方法参数,则不会导致属性CCD_。

尝试将Draw重命名为CreatePlotModel,然后执行以下操作:

public virtual PlotModel CreatePlotModel()
{
    var model = new OxyPlot.PlotModel();
    LineSeries LS= new LineSeries();
    Random rnd = new Random();
    for (int i=0; i<10; i++)
    {
        LS.Points.Add(new DataPoint(i,rnd.Next(1,100)));
    }
    model.Series.Add(LS);
    model.InvalidatePlot(false);
    return model;
}

然后分配数据的PlotModel属性:Data.PlotModel = base.CreatePlotModel()

从构造函数调用虚拟方法是危险的。请参阅中的更多信息https://msdn.microsoft.com/en-us/library/s8e39b8h(v=vs.120).aspx