WPF MVVM dialog example

后端 未结 6 1798
梦毁少年i
梦毁少年i 2020-12-04 07:48

Does anyone have any examples of showing a window dialog using MVVM (Prism)? - for example a configuration settings window when a command is executed.

All of the exa

6条回答
  •  死守一世寂寞
    2020-12-04 08:04

    I'm agreed, that using service to display dialog according to MVVM pattern is the most simple solution. But, I also asked myself, if there are 3 assemblies in my project Model, ViewModel, View and according to MVVM pattern assembly ViewModel has a reference to Model, and View to both Model and ViewModel where should I place DialogService class? If I will place one in the ViewModel assembly - I have no chances to create DialogView instance; on the other hand, if I will place DialogService in the View assembly, how I should inject it in my ViewModel class?

    So, I would recoment look at Advanced MVVM scenarios with Prism Part: Using Interaction Request Objects

    As example of this approach:

    DialogViewModelBase

    public abstract class DialogViewModelBase : ViewModelBase
    {
        private ICommand _ok;
    
        public ICommand Ok
        {
            get { return _ok ?? (_ok = new DelegateCommand(OkExecute, CanOkExecute)); }
        }
    
        protected virtual bool CanOkExecute()
        {
            return true;
        }
    
        protected virtual void OkExecute()
        {
            _isSaved = true;
            Close = true;
        }
    
        private ICommand _cancel;
    
        public ICommand Cancel
        {
            get 
            {
               return _cancel ?? (_cancel = new DelegateCommand(CancelExecute, CanCancelExecute));
            }
        }
    
        protected virtual bool CanCancelExecute()
        {
            return true;
        }
    
        protected virtual void CancelExecute()
        {
            Close = true;
        }
    
        private bool _isSaved = false;
        public bool IsSaved
        {
            get { return _isSaved; }
        }
    
        private bool _close = false;
    
        public bool Close
        {
            get { return _close; }
            set
            {
                _close = value;
                RaisePropertyChanged(() => Close);
            }
        }
    }
    

    CreateUserStoryViewModel:

    public class CreateUserStoryViewModel : DialogViewModelBase
    {
        private string _name = String.Empty;
    
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                RaisePropertyChanged(() => Name);
            }
        }
    }
    

    CreateUserStoryRequest

    private InteractionRequest _createUserStoryRequest;
    public InteractionRequest CreateUserStoryRequest
    {
        get
        {
            return _createUserStoryRequest ?? (_createUserStoryRequest = new InteractionRequest());
        }
    }
    

    CreateUserStory Command

    private void CreateUserStoryExecute()
    {
        CreateUserStoryRequest.Raise(new Notification()
        {
            Content = new CreateUserStoryViewModel(),
            Title = "Create User Story"
        }, 
        notification =>
                     {
                          CreateUserStoryViewModel createUserStoryViewModel =
                                   (CreateUserStoryViewModel)notification.Content;
                          if (createUserStoryViewModel.IsSaved)
                          {
                             _domainContext.CreateUserStory(
    new UserStory(){ Name = createUserStoryViewModel.Name, });
                          }
                     });
    }
    

    XAML:

    
    
    
      
        
          
            
          
        
      
    
    

提交回复
热议问题