将集合引用从视图传递到虚拟机时跟踪集合引用的最佳方法是什么

我的应用程序是翻译应用程序。它包含传递给不同视图模型的翻译列表。这些视图模型可能会修改这些列表,包括添加和删除操作。为此,我ObservableCollection在构造函数中将此列表转换为 an ,并且我的列表不再被修改。我知道转换为 anObservableCollection会创建一个新对象,并且引用不再相同。它对于相关视图来说工作得很好,但是一旦我想更改为另一个视图,列表就不会更新。我想知道解决这个问题的最佳方法是什么?


我认为我可以创建一个自定义 ObservableCollection,其中包含相应的列表,并在完成添加或删除操作时自动更新它。看起来与此类似的东西。


看法


public partial class MainWindow : Window

{

    private void ListViewItem_PreviewMouseDown(objectsender,MouseButtonEventArgs e)

    {

        // this is where I instanciate the viewModel, and the 

        // list<Translation> isn't modify once I close the view

        DataContext = new ModifyWordVM(translations);

    }

}

视图模型


public class ModifyWordVM: INotifyPropertyChanged

{

    private ObservableCollection<TranslationVM> translations;

    public ObservableCollection<TranslationVM> Translations

    {

        get { return translations; }

        set { translations = value; OnPropertyChanged("Translations"); }

    }

    public ModifyWordVM(List<Translation> translations)

    {

        // Converting list to ObservableCollection

        Translations = ConvertionHelper.ConvertTo(translations);

    }

}

我想知道恢复修改后的列表的更干净的方法是什么。


慕标5832272
浏览 107回答 1
1回答

炎炎设计

您应该封装翻译及其操作。为此,只需引入一个类,例如TranslationService在所有相关视图模型之间共享的类。为了省略臭单例,我将服务的实例添加到App.xaml资源中。这个想法是翻译列表的所有修改都发生在一个位置或类型中。与视图的绑定源相同的类型。添加新翻译时,视图应调用ICommand视图模型上的 a。该命令将AddTranslation调用TranslationService. 删除也一样。对翻译集合的任何更改现在都将反映在整个应用程序中。如果您还想捕获实际翻译的修改(例如重命名或编辑),则还TranslationService需要处理项目PropertyChanged的事件。 当项目属性更改时,必须通过引发该属性的事件来响应。这也需要实施这些项目。ObservableCollectionTranslationServicePropertyChangedObservableCollectionTranslationsINotifyPropertyChangedApp.xaml共享TranslationService实例<Application.Resources>&nbsp; &nbsp; <TranslationService x:Key="TranslationService">&nbsp; &nbsp; &nbsp; &nbsp; <TranslationService.DatabaseService>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DatabaseService />&nbsp; &nbsp; &nbsp; &nbsp; </TranslationService.DatabaseService>&nbsp; &nbsp; </TranslationService></Application.Resources>MainWindow.xaml.cspublic partial class MainWindow : Window{&nbsp; &nbsp; private void ListViewItem_PreviewMouseDown(objectsender,MouseButtonEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Instantiate the view model and initialize DataContext from XAML instead.&nbsp; &nbsp; &nbsp; &nbsp; // This method became redundant.&nbsp; &nbsp; }}主窗口.xaml<Window.DataContext>&nbsp; <ModifyWordVM>&nbsp; &nbsp; <ModifyWordVM.TranslationService>&nbsp; &nbsp; &nbsp; <!-- Reference the shared instance -->&nbsp; &nbsp; &nbsp; <StaticResource ResourceKey="TranslationService" />&nbsp; &nbsp; </ModifyWordVM.TranslationService>&nbsp; </ModifyWordVM></Window.DataContext>修改WordVM.cspublic class ModifyWordVM: INotifyPropertyChanged{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public ModifyWordVM()&nbsp; &nbsp; {}&nbsp; &nbsp; public AddTranslation(Translation translation) => this.translationService.AddTranslation(translation);&nbsp; &nbsp; public RemoveTranslation(Translation translation) => this.translationService.RemoveTranslation(translation);&nbsp; &nbsp; public TranslationService TranslationService {get; set;}&nbsp; &nbsp; public ObservableCollection<TranslationVM> Translations => this.translationService.Translations;&nbsp;}翻译服务.cspublic class TranslationService{&nbsp; &nbsp; public TranslationService()&nbsp; &nbsp; {}&nbsp; &nbsp; public AddTranslation(Translation translation)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Add translations&nbsp; &nbsp; }&nbsp; &nbsp; public RemoveTranslation(Translation translation)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Remove translations&nbsp; &nbsp; }&nbsp; &nbsp; private DatabaseService databaseService;&nbsp; &nbsp; public DatabaseService DatabaseService&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get => this.databaseService;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.databaseService = value;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Translations = databaseService.getTranslations;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; private ObservableCollection<TranslationVM> translations;&nbsp; &nbsp; public ObservableCollection<TranslationVM> Translations&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get => this.translations;&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.translations = value;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged("Translations");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP