gpt4 book ai didi

c# - Prism 。关闭使用 IDialogService 创建的对话框

转载 作者:行者123 更新时间:2023-11-30 22:52:16 25 4
gpt4 key购买 nike

我正在尝试使用在 github 问题 1666. A New IDialogService for WPF 中讨论过的新 IDialogService .我喜欢这个新功能,但与 InteractionRequest 相比,我找不到针对使用 IDialogService 的一种情况的解决方案。

有一个按钮,按下它打开非模态对话框。如果用户再次按下同一个按钮,而对话框仍然打开,则对话框关闭。应该如何以正确的方式实现这种行为?

MainWindowViewModel

public class MainWindowViewModel : BindableBase
{
private readonly IDialogService _dialogService;
public DelegateCommand CustomPopupCommand { get; }

public MainWindowViewModel(IDialogService dialogService)
{
_dialogService = dialogService;
CustomPopupCommand = new DelegateCommand(OpenClosePopup);
}

private void OpenClosePopup()
{
// It looks like some additional logic should be implemented here.
// How to save previously opened IDialogAware instance and close it if needed?
_dialogService.Show("CustomPopupView", new DialogParameters("Title=Good Title"), result => { });
}
}

CustomPopupViewModel

public class CustomPopupViewModel : BindableBase, IDialogAware
{
private string _title;
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
public DelegateCommand<object> CloseCommand { get; }

public CustomPopupViewModel()
{
CloseCommand = new DelegateCommand<object>(CloseDialog);
}

public event Action<IDialogResult> RequestClose;

public void OnDialogOpened(IDialogParameters parameters)
{
Title = parameters.GetValue<string>(nameof(Title));
}

public void OnDialogClosed()
{
}

public bool CanCloseDialog()
{
return true;
}

public void RaiseRequestClose(IDialogResult dialogResult)
{
RequestClose?.Invoke(dialogResult);
}

private void CloseDialog(object button)
{
RaiseRequestClose(
new DialogResult(button is ButtonResult buttonResult ? buttonResult : ButtonResult.Cancel));
}
}

我不知道如何以正确的方式实现它,因为方法 IDialogService.Show() 完全脱离了对 ViewModel 和 View 的了解。当然View的名字除外。

最佳答案

您始终可以通过事件聚合器发送事件,如果一次打开多个对话框,您可能必须在对话框参数中传递一些 id 以关闭正确的对话框。

但这感觉真的很笨拙,我更愿意从 Show/ShowDialog 获取一个 IDisposable 来关闭 上的对话框处置

public CustomPopupViewModel(IEventAggregator eventAggregator)
{
eventAggregator.GetEvent<CloseDialogEvent>().Subscribe( id => { if (id == _id) CloseMe(); } );
}

public void OnDialogOpened(IDialogParameters parameters)
{
_id = parameters.GetValue<string>("id");
}

_dialogService.Show("CustomPopupView", new DialogParameters("id=12345"), result => { });

_eventAggregator.GetEvent<CloseDialogEvent>().Publish("12345");

关于c# - Prism 。关闭使用 IDialogService 创建的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58291547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com