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