从System.windows.FormsMessageBox转换为Xceed.Wpf.Toolkit.MessageBox时如何处理system.InvalidOperationException

问题描述 投票:0回答:1

我有一个非常平淡的消息框,向我的用户询问一个简单的问题(不是或不是)。为了进行快速开发,我使用了一个简单的System.Windows.Forms.MessageBox并用词表达了问题(“如果要选择'A',请单击'是',如果要选择'B',请单击'否'”)。现在,我要回过头来改善wpf应用程序的外观,并坚持尝试将此MessageBox转换为看起来不错的东西。

我的初步搜索告诉我使用Xceed.Wpf.Toolkit.MessageBox能够创建自定义消息框,但是在尝试使用它时出现了异常。

旧代码

DialogResult dialogResultForDataDisplay = System.Windows.Forms.MessageBox.Show("Yes: Display by properties \n \t Each row will contain data for a specific asset class in a specific submarket during a specific quarter. \n \n No: Display by quarters \n \t Each row will will show the change over time for a specific property of an asset class in a specific submarket.", "Data Grouping Format", MessageBoxButtons.YesNo);

新代码

Style style = new Style();
style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.YesButtonContentProperty, "By Property"));
style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.NoButtonContentProperty, "By Quarter"));
MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show("How do you want your information displayed?", "My caption", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes, style);
        Console.WriteLine(result);

新代码正在生成此异常:System.InvalidOperationException: 'The calling thread must be STA, because many UI components require this.'

您将如何处理此异常?

messagebox xceed
1个回答
0
投票
MessageBoxResult result = MessageBoxResult.None; System.Windows.Application.Current.Dispatcher.Invoke((Action)delegate { Style style = new Style(); style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.YesButtonContentProperty, "Yes, FTW!")); style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.NoButtonContentProperty, "Omg, no")); result = Xceed.Wpf.Toolkit.MessageBox.Show("How do you want your information displayed?", "My caption", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes, style); } Console.WriteLine(result);
© www.soinside.com 2019 - 2024. All rights reserved.