我正在尝试使用Silverlight的ChildWindow对象进行确认对话.
理想情况下,我希望它像MessageBox.Show()一样工作,整个应用程序停止,直到从用户收到输入.
例如:
for (int i = 0; i < 5; i++) { if (i==3 && MessageBox.Show("Exit early?","Iterator",MessageBoxButton.OKCancel) == MessageBoxResult.OK) { break; } }
如果用户点击OK,将停止迭代3 …
但是,如果我要做一些事情:
ChildWindow confirm = new ChildWindow(); confirm.Title = "Iterator"; confirm.HasCloseButton = false; Grid container = new Grid(); Button closeBtn = new Button(); closeBtn.Content = "Exit early"; closeBtn.Click += delegate { confirm.DialogResult = true; confirm.Close(); }; container.Children.Add(closeBtn); Button continueBtn = new Button(); continueBtn.Content = "Continue!"; continueBtn.Click += delegate { confirm.DialogResult = false; confirm.Close(); }; container.Children.Add(continueBtn); confirm.Content = container; for(int i=0;i<5;i++) { if (i==3) { confirm.Show(); if (confirm.DialogResult.HasResult && (bool)confirm.DialogResult) { break; } } }
这显然不起作用,因为线程没有停止… confirm.DialogResult.HasResult将为false,并且循环将继续超过3.
我只是想知道,我怎么能正确地做到这一点. Silverlight是单线程的,所以我不能只是让线程进入睡眠状态,然后在我准备就绪时将其唤醒,所以我只是想知道是否还有其他人可以推荐的东西?
我考虑过颠倒逻辑 – 即将我想要发生的动作传递给Yes / No事件,但在我的具体情况下,这不会起作用.
提前致谢!
解决方法
我认为你不能像WinForms的ShowDialog一样在消息循环中阻止你的代码.
但是,您可以滥用迭代器来实现相同的效果:
interface IAction { void Execute(Action callback); } public static void ExecAction(IEnumerator<IAction> enumerator) { if (enumerator.MoveNext()) enumerator.Current.Execute(() => ExecAction(enumerator)); } class DialogAction : ChildWindow,IAction { void IAction.Execute(Action callback) { //Show the window,then call callback when it's closed } } IEnumerator<IAction> YourMethod() { ... var confirm = new DialogAction(); yield return confirm; if (confirm.DialogResult.HasResult && (bool)confirm.DialogResult) yield break; ... }
要使用此系统,您可以编写ExecAction(YourMethod());.请注意,这将是一个半阻塞调用,我根本没有测试过这个.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。