我们的mvvm爱好者都知道Josh Smith mvvm示例,以及如何通过将存储库对象注入(inject)到customerViewModel的构造函数中来在详细的客户 View 中保存客户。

但是, View 模型不应该知道存储库。它只是一个 View 模型,没有什么必须知道持久性等。

如果在代码后面设置了我的Action委托(delegate)SaveDocumentDelegate,该如何在DocumentViewModel上注册?实际上,我应该在DocumentController中订阅委托(delegate),但是如何在DocumentController中实例化DocumentView并将其设置为Datacontext而不在代码隐藏中这样做。我唯一想到的是在窗口中使用contentcontrol并将其绑定(bind)到viewModel的类型,并使用Document UserControl将其绑定(bind)为datatemplate,如下所示:

<UserControl.Resources>

        <DataTemplate DataType="{x:Type ViewModel:DocumentViewModel}">
            <View:DocumentDetailView/>
        </DataTemplate>

    </UserControl.Resources>

<ContentControl Content="{Binding MyDocumentViewModel}" />

但是我不想使用控件来解决我的体系结构问题...

xaml :(查看第一种方法)
public partial class DocumentDetailView : UserControl
    {
        public DocumentDetailView()
        {
            InitializeComponent();

            this.DataContext = new DocumentViewModel(new Document());
        }
    }

DocumentViewModel:
 public class DocumentViewModel : ViewModelBase
    {
        private Document _document;
        private RelayCommand _saveDocumentCommand;
        private Action<Document> SaveDocumentDelegate;

        public DocumentViewModel(Document document)
        {
            _document = document;
        }

        public RelayCommand SaveDocumentCommand
        {
            get { return _saveDocumentCommand ?? (_saveDocumentCommand = new RelayCommand(() => SaveDocument())); }
        }

        private void SaveDocument()
        {
            SaveDocumentDelegate(_document);
        }

        public int Id
        {
            get { return _document.Id; }
            set
            {
                if (_document.Id == value)
                    return;

                _document.Id = value;
                this.RaisePropertyChanged("Id");
            }
        }

        public string Name
        {
            get { return _document.Name; }
            set
            {
                if (_document.Name == value)
                    return;

                _document.Name = value;
                this.RaisePropertyChanged("Name");
            }
        }

        public string Tags
        {
            get { return _document.Tags; }
            set
            {
                if (_document.Tags == value)
                    return;

                _document.Tags = value;
                this.RaisePropertyChanged("Tags");
            }
        }
    }

更新:
public class DocumentController
    {
        public DocumentController()
        {
            var win2 = new Window2();
            var doc =  new DocumentViewModel(new DocumentPage());
            doc.AddDocumentDelegate += new Action<Document>(OnAddDocument);
            win2.DataContext = doc;
            wind2.ShowDialog();
        }

        private void OnAddDocument(Document doc)
        {
            _repository.AddDocument(doc);
        }
    }

您如何看待这个想法?

最佳答案



viewmodel将模型和 View 连接在一起;尽管它不能处理持久性,但它正是控制持久性的东西。

我们通过使用服务将其与其他关注点分离。

避免将持久性关注点添加到 View 模型的一种方法是将这些关注点抽象到存储库接口(interface)中,以便我们可以将其作为依赖项注入(inject)。这样,我们通常可以响应用户与 View 的交互,而在 View 模型中委派持久性工作。

关于wpf - 如果存储库在 View 模型中无法通过,如何从绑定(bind)到ViewModel的DetailView中保存数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3730692/

10-11 00:58
查看更多