对图表中的系列撤消操作

我想在图表中多次更改一些 Yvalues[0] 后恢复以前的系列。所以我使用备忘录模式实现了撤销操作。


但是它不起作用并且运行时没有错误。下面是发起人、纪念品和看守人。


public class Originator

{


    private Series _series = new Series();


    public Originator(Series series)

    {

        _series = series;

    }

    public Series OSeries

    {

        get

        {

            return _series;

        }

        set

        {

            _series = value;

        }

    }


    public Memento SaveSeries()

    {

        return new Memento(_series);

    }


    public void RestoreSeries(Memento m)

    {

        this._series = m.MMseries;

    }

 }


public class Memento

{

    private Series _series =new Series();


    public Memento(Originator org)

    {

        this._series = org.OSeries;

    }

    public Memento(Series mseries)

    {

        _series = mseries;

    }


    public Series MMseries

    {

        get

        {

            return _series;

        }

        set

        {

            _series = value;

        }

    }

}

public class Caretaker

{

    //  private static Caretaker _instance;



    //  public Caretaker() { }


    private Memento chartMemento;

    public Memento Memento

    {

        get

        { return chartMemento; }


        set

        {  chartMemento = value; }


    }

}

在公共部分类 Form1 中创建对象:Form


Originator _org;

Caretaker taker = new Caretaker();

从文件加载图表时,open_file 函数包含以下代码:


_org = new Originator(global_series); //For undo/redo operation

while (chart1.Series.Count > 0) { chart1.Series.RemoveAt(0); }


chart1.Series.Add(global_series);

taker.Memento = _org.SaveSeries();

对于撤消按钮:


_org.RestoreSeries(taker.Memento);

while (chart1.Series.Count > 0) { chart1.Series.RemoveAt(0); }

global_series = _org.OSeries;

chart1.Series.Add(global_series);


蛊毒传说
浏览 135回答 2
2回答

MYYA

使用备忘录模式进行多次撤销操作看护人代码:public class Caretaker{&nbsp; &nbsp; private Memento chartMemento;&nbsp; &nbsp; public Memento Memento&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; { return chartMemento; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; chartMemento = value; }&nbsp; &nbsp; }}纪念品代码:public class Memento{&nbsp; &nbsp; private Series _series =new Series();&nbsp; &nbsp; public Memento(Originator org)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this._series.Points.Clear();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var dp in org.OSeries.Points) this._series.Points.Add(dp.Clone());&nbsp; &nbsp; }&nbsp; &nbsp; public Memento(Series mseries)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this._series.Points.Clear();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var dp in mseries.Points) this._series.Points.Add(dp.Clone());&nbsp; &nbsp; }&nbsp; &nbsp; public Series MMseries&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _series;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _series = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}发起人代码public class Originator{&nbsp; &nbsp; private Series _series = new Series();&nbsp; &nbsp; public Originator() { }&nbsp; &nbsp; public Originator(Series series)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // _series = series;&nbsp; &nbsp; &nbsp; &nbsp; _series.Points.Clear();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var dp in series.Points) _series.Points.Add(dp.Clone());&nbsp; &nbsp; }&nbsp; &nbsp; public Series OSeries&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _series;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _series = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public Memento SaveSeries()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return new Memento(_series);&nbsp; &nbsp; }&nbsp; &nbsp; public void RestoreSeries(Memento m)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //this._series = m.MMseries;&nbsp; &nbsp; &nbsp; &nbsp; this._series.Points.Clear();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var dp in m.MMseries.Points) this._series.Points.Add(dp);&nbsp; &nbsp; &nbsp; &nbsp; this._series.ChartType = SeriesChartType.Line;&nbsp; &nbsp; }}在 Form1 中声明对象列表及其计数器&nbsp;Orginator _org;&nbsp;List<Originator> list_org = new List<Originator>();&nbsp;List<Caretaker> list_taker = new List<Caretaker>();&nbsp;int obj_counter = 0;Do 操作的代码&nbsp;list_org.Add(_org = new Originator(global_series));&nbsp;Caretaker ct = new Caretaker();&nbsp;ct.Memento = list_org[obj_counter++].SaveSeries();&nbsp;list_taker.Add(ct);撤销操作的代码:&nbsp; if (obj_counter > 0)&nbsp; {&nbsp; &nbsp; &nbsp;list_org[--obj_counter].RestoreSeries(list_taker[obj_counter].Memento);&nbsp; &nbsp; &nbsp;global_series.Points.Clear();&nbsp; &nbsp; &nbsp;foreach (var dp in list_org[obj_counter].OSeries.Points)&nbsp;&nbsp; &nbsp; &nbsp;global_series.Points.Add(dp);&nbsp; }

catspeake

Series是参考变量。因此,您所做的所有更改都将针对该系列,并且您拥有的所有参考资料都将指向同一(原始)系列。花哨的图案,因为他们有用的可能是不能代替的基础知识。要允许撤销/重做你需要复制的的数据的不是,参考!注意Series没有Clone方法。所以你需要自己做;在您的情况下,这归结为复制 Points ..但是对于更一般的情况,您可能想要复制其他系列属性,最值得注意的是ChartType您为准备Series. 也许以工厂模式创建它..?但也要注意DataPoint 确实有一个Clone方法。所以你可以Originator像这样改变你的构造函数:public Originator(Series series){&nbsp; &nbsp; //_series = series;&nbsp; &nbsp; foreach (var dp in series.Points) _series.Points.Add(dp.Clone());}事情将开始按预期工作。但是您也需要像这样进行恢复,否则您将指向保存的系列,接下来的更改将更改它而不是图表系列..:public void RestoreSeries(Memento m){&nbsp; &nbsp; // this._series = m.MMseries;&nbsp; &nbsp; this._series.Points.Clear();&nbsp; &nbsp; foreach (var dp in m.MMseries.Points) this._series.Points.Add(dp);}顺便说一句:要清除所有系列,请使用chart.Series.Clear()!
打开App,查看更多内容
随时随地看视频慕课网APP