WPF Prism 将参数从 idialog 发送回视图

Posted

技术标签:

【中文标题】WPF Prism 将参数从 idialog 发送回视图【英文标题】:WPF Prism send parameter back from idialog to view 【发布时间】:2021-09-20 10:30:42 【问题描述】:

我有一个视图,它打开一个包含数据网格的 idialog。我选择了某些行并将它们作为列表发送回视图,但我搜索的所有内容都发现 ResultButton 返回 idialog。如何发回自定义值,例如列表或其他任何内容?

我的 iDialog 视图模型:

public DelegateCommand<string> CloseCommand  get; set; 
    public DelegateCommand<string> AttachCommand  get; set; 

    private string _txtSearch;
    public string txtSearch
    
        get  return _txtSearch; 
        set  SetProperty(ref _txtSearch, value); 
    


    public List<GE_Drawing> SelectedDrawings  get; set; 

    private ObservableCollection<GE_Drawing> _DrawingList= new ObservableCollection<GE_Drawing>();
    public ObservableCollection<GE_Drawing> DrawingList
    
        get  return _DrawingList; 
        set  SetProperty(ref _DrawingList, value); 
    
    private bool _IsSelected;
    public bool IsSelected
    
        get  return _IsSelected; 
        set  SetProperty(ref _IsSelected, value); 
    
    public GE_DrawingAttachViewModel()
    
        LoadList();
        CloseCommand = new DelegateCommand<string>(onClose);
        AttachCommand = new DelegateCommand<string>(onAttach);
       


    



    private void onAttach(string parameter)
    
        ButtonResult result = new ButtonResult();

        var records = DrawingList.Where(x => x.IsSelected == true).ToList();

        if(records.Count>0)
        
            SelectedDrawings = records;
        

        

        //result = ButtonResult.OK;
        //RaiseRequestClose(new DialogResult(result));
    

    private void RaiseRequestClose(DialogResult dialogResult)
    
        
        RequestClose?.Invoke(dialogResult);
    

    private void onClose(string parameter)
    
        ButtonResult result = new ButtonResult();
        result = ButtonResult.Cancel;
        RaiseRequestClose(new DialogResult(result));
    

    private string _title="Drawing List";
    public string Title
    
        get => _title; 
        set => SetProperty(ref _title, value); 
    

    public event Action<IDialogResult> RequestClose;

    public bool CanCloseDialog()
    
        return true;
    

    public void OnDialogClosed()
    
       
    

    public void OnDialogOpened(IDialogParameters parameters)
    
       
    

    public async void LoadList()
    
        GenericDataService<GE_Drawing> generic = new GenericDataService<GE_Drawing>();
        
       
        DrawingList.AddRange(await generic.GetAll());
      

    

如上所述,我有一个 OnAttach,它应该将我的 SelectedDrawings 变量发送回视图,但我不知道该怎么做。

【问题讨论】:

显示对话框的代码在哪里?特别是 - 您用来解析结果的回调是什么? 【参考方案1】:

我找到了答案... IdialogResult 有一个带值的参数可以在对话框中设置,并使用一个键在回调中获取值。

private void openDialog()
    
        DialogParameters parameter = new DialogParameters();
        _dialogService.ShowDialog("GE_DrawingAttach", parameter, r =>
        
            if(r.Result==ButtonResult.OK)
            

                DrawingList = r.Parameters.GetValue<List<GE_Drawing>>("DrawingList");
                
            

        );
    

【讨论】:

以上是关于WPF Prism 将参数从 idialog 发送回视图的主要内容,如果未能解决你的问题,请参考以下文章

从PRISM开始学WPFPrism?

WPF PRISM开发入门一

从PRISM开始学WPFMVVMViewModel?

从PRISM开始学WPFMVVMCommand?

从PRISM开始学WPFPrism-Module?

从PRISM开始学WPFMVVM事件聚合器EventAggregator?