gpt4 book ai didi

c# - Mahapps 1.3 对话框和 Avalon.Wizard

转载 作者:行者123 更新时间:2023-11-30 21:45:45 27 4
gpt4 key购买 nike

我已经集成了流行的 UI 库 MahappsAvalon.Wizard控制。

它集成得很好,但我对 Mahapps 对话框有疑问。向导控件定义一个 InitializeCommand 来处理向导页面上的输入。

显然,在初始化附加到 View 的依赖属性 (DialogParticipation.Register) 之前会触发 InitializeCommand。

这会导致以下错误:

Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext.

重现该问题的示例项目可用 here .

关于如何解决这个问题有什么建议吗?

最佳答案

页面 Xaml 不是在初始化命令中创建的,因此此时您不能使用 DialogCoordinator。

这是一个带有 LoadedCommand 的自定义接口(interface),您可以在 ViewModel 中实现它并在后面的 Xaml 代码中调用它。

public interface IWizardPageLoadableViewModel
{
ICommand LoadedCommand { get; set; }
}

View 模型:

public class LastPageViewModel : WizardPageViewModelBase, IWizardPageLoadableViewModel
{
public LastPageViewModel()
{
Header = "Last Page";
Subtitle = "This is a test project for Mahapps and Avalon.Wizard";

InitializeCommand = new RelayCommand<object>(ExecuteInitialize);
LoadedCommand = new RelayCommand<object>(ExecuteLoaded);
}

public ICommand LoadedCommand { get; set; }

private async void ExecuteInitialize(object parameter)
{
// The Xaml is not created here! so you can't use the DialogCoordinator here.
}

private async void ExecuteLoaded(object parameter)
{
var dialog = DialogCoordinator.Instance;
var settings = new MetroDialogSettings()
{
ColorScheme = MetroDialogColorScheme.Accented
};
await dialog.ShowMessageAsync(this, "Hello World", "This dialog is triggered from Avalon.Wizard LoadedCommand", MessageDialogStyle.Affirmative, settings);
}
}

和 View :

public partial class LastPageView : UserControl
{
public LastPageView()
{
InitializeComponent();
this.Loaded += (sender, args) =>
{
DialogParticipation.SetRegister(this, this.DataContext);
((IWizardPageLoadableViewModel) this.DataContext).LoadedCommand.Execute(this);
};
// if using DialogParticipation on Windows which open / close frequently you will get a
// memory leak unless you unregister. The easiest way to do this is in your Closing/ Unloaded
// event, as so:
//
// DialogParticipation.SetRegister(this, null);
this.Unloaded += (sender, args) => { DialogParticipation.SetRegister(this, null); };
}
}

希望这对您有所帮助。

enter image description here

enter image description here

关于c# - Mahapps 1.3 对话框和 Avalon.Wizard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853326/

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