C# wpf窗口在重新打开后保存旧数据

C# wpf窗口在重新打开后保存旧数据,c#,wpf,memory,mvvm,C#,Wpf,Memory,Mvvm,我有一个窗口,显示绑定到视图中richtextbox的flowdocument。为了制作此文档,我使用来自不同ViewModel的集合 第一次打开窗口时,一切正常,但第二次或之后的任何时候,它仍然保存着第一次打开窗口时的数据,并且看起来GenerateReport方法不会再次启动 如何摆脱旧数据并使GenerateReport方法获取更新的数据 这就是我的ViewModel的外观 public class ReportViewModel : ViewModelBase { private

我有一个窗口,显示绑定到视图中richtextbox的flowdocument。为了制作此文档,我使用来自不同ViewModel的集合

第一次打开窗口时,一切正常,但第二次或之后的任何时候,它仍然保存着第一次打开窗口时的数据,并且看起来GenerateReport方法不会再次启动

如何摆脱旧数据并使GenerateReport方法获取更新的数据

这就是我的ViewModel的外观

public class ReportViewModel : ViewModelBase
{
    private string _shotListReport;
    public string ShotListReport   // the Name property
    {
        get { return _shotListReport; }
        set { _shotListReport = value; RaisePropertyChanged(""); }
    }  

    public ReportViewModel (ShotListViewModel ShotListViewModel)
    {
        ShotList = ShotListViewModel.AllShots;
        SceneList = ShotListViewModel.SceneCollectionView;

        GenerateReport();
    }

    public ListCollectionView SceneList { get; set; }
    public ObservableCollection<Shot> ShotList { get; set; }

    private void GenerateReport()
    {
        FlowDocument doc = new FlowDocument();

        //DO STUFF

        ShotListReport = new TextRange(doc.ContentStart, doc.ContentEnd).Text;
     }   
}
公共类ReportViewModel:ViewModelBase { 私有字符串\u shotListReport; 公共字符串ShotListReport//Name属性 { 获取{return\u shotListReport;} set{u shotListReport=value;RaisePropertyChanged(“”;} } 公共报表视图模型(ShotListViewModel ShotListViewModel) { ShotList=ShotListViewModel.AllShots; SceneList=ShotListViewModel.SceneCollectionView; GenerateReport(); } 公共ListCollectionView场景列表{get;set;} 公共ObservableCollection快照列表{get;set;} 私有void生成器报告() { FlowDocument文档=新的FlowDocument(); //做事 ShotListReport=新文本范围(doc.ContentStart,doc.ContentEnd).Text; } }
当您调用此func:ShotListReportViewModel时,您可能需要在窗口关闭事件上实现一些代码,以消除绑定等,并将所有使用的数据源设置为null或空,并在打开时重新填充所有数据。@Kram。ShotListReportViewModel是一个打字错误,我将其更新为ReportViewModel。该函数在窗口打开时被调用OK听起来您的问题是发送的ShotListViewModel属性已经注册了数据。在将ShotListViewModel发送到新VM之前,请尝试重新生成它。似乎您在未正确清除数据的情况下重用了ReportViewModel或ShotListViewModel。正如Kram和Impin所说的,你需要要么把它们报废,要么每次都重建它们,如果你真的想重用它们,也可以实现适当的清理逻辑。当你调用这个func:ShotListReportViewModel时,你可能需要在窗口关闭事件上实现一些代码,以消除绑定等,并将所有使用的数据源设置为null或空,并在打开时重新填充所有数据。@Kram。ShotListReportViewModel是一个打字错误,我将其更新为ReportViewModel。该函数在窗口打开时被调用OK听起来您的问题是发送的ShotListViewModel属性已经注册了数据。在将ShotListViewModel发送到新VM之前,请尝试重新生成它。似乎您在未正确清除数据的情况下重用了ReportViewModel或ShotListViewModel。正如Kram和Impin所说,如果你真的想重新使用它们,你需要要么报废它们,然后每次都重建它们,要么实施正确的清理逻辑。