Я очень много нового в дизайне архитектуры MVVM...
В последнее время я боролся за то, чтобы найти подходящий контроль, уже написанный для такой цели, но не повезло, поэтому я повторно использовал части XAML из другого подобного элемента управления и получил свой собственный.
Чего я хочу достичь:
У вас есть возможность повторного использования View (usercontrol) + viewmodel (для привязки), чтобы иметь возможность использовать внутри других представлений в качестве модального наложения, отображающего диалог, который отключает остальную часть представления, и показывает диалог над ним.
Как я хотел его реализовать:
- создать модель представления, которая принимает строку (сообщение) и действие + набор строк (кнопки)
- viewmodel создает коллекцию ICommands, которые вызывают эти действия. Диалоговое окно
- связывается с его моделью просмотра, которая будет отображаться как свойство другой модели представления (parent) Диалоговое окно
- помещается в xaml родителя следующим образом:
pseudoXAML:
<usercontrol /customerview/ ...>
<grid>
<grid x:Name="content">
<various form content />
</grid>
<ctrl:Dialog DataContext="{Binding DialogModel}" Message="{Binding Message}" Commands="{Binding Commands}" IsShown="{Binding IsShown}" BlockedUI="{Binding ElementName=content}" />
</grid>
</usercontrol>
Таким образом, модальный диалог получает datacontext из свойства DialogModel модели view Customer и связывает команды и сообщения. Он также будет связан с каким-либо другим элементом (здесь "контент" ), который должен быть отключен, когда отображается диалог (привязка к IsShown). Когда вы нажимаете какую-либо кнопку в диалоговом окне, вызывается соответствующая команда, которая просто вызывает связанное действие, которое было передано в конструкторе модели представления.
Таким образом, я мог бы вызвать Show() и Hide() диалогового окна в режиме просмотра диалога изнутри модели просмотра Customer и, при необходимости, изменить режим просмотра диалога.
Это дало бы мне только один диалог за раз, но это нормально. Я также думаю, что модель просмотра диалога будет оставаться unittestable, так как unittests будет охватывать вызов команд, которые должны быть созданы после того, как они были созданы с помощью Actions в конструкторе. Для диалогового окна было бы несколько строк кода, но очень мало и довольно тупые (сеттеры getters, практически без кода).
Что меня беспокоит:
Это нормально? Есть ли проблемы, в которые я мог бы попасть? Означает ли это нарушение некоторых принципов MVVM?
Спасибо большое!
EDIT: я отправил свое полное решение, чтобы вы могли лучше посмотреть. Любые архитектурные комментарии приветствуются. Если вы видите какой-то синтаксис, который можно исправить, сообщение помечено как вики сообщества.