弹出消息框以确认日期选择器问题

发布于 2024-08-24 06:57:12 字数 819 浏览 7 评论 0原文

我在 Silverlight 2 中使用普通日期选择器。我将所选日期绑定到一个值,当该值更改时,我会弹出一个消息框以确认他们想要更改该值。

然而,当我在日期选择器的值更改后立即使用消息框时,会发生奇怪的行为。日期选择器的弹出窗口不会关闭,如果您将鼠标悬停在日历上,它将选择一个日期,而无需单击鼠标。

此外,发生这种情况后,它似乎会影响绑定,并且在重新加载页面之前无法再次设置视图模型的属性。

这个问题相当具体,所以我附上了一个精简的示例。选择一个日期并按“确定”,然后将鼠标移到日历上以重现该日期。

我的 XAML -

<Grid x:Name="LayoutRoot">
    <controls:DatePicker x:Name="dpTest" 
                         Height="25" 
                         Width="75" 
                         SelectedDateChanged="DatePicker_SelectedDateChanged" />
</Grid>

我的代码背后 -

  Private Sub DatePicker_SelectedDateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
    MessageBox.Show("Test Popup")
End Sub

有什么想法或解决方法吗?

I am using the vanilla datepicker in Silverlight 2. I bind the selected date to a value, and when that value changes I pop a messagebox to confirm that they would like to change the value.

However strange behaviour ensues when I use a messagebox straight after the datepicker's value is changed. The datepicker's popup will not close, and if you mouse over the calendar it will choose a date without having to click the mouse.

Also, after this occurs it seems to affect bindings and it cannot set the view model's property again until the page is reloaded.

This problem is rather specific so I have attached a stripped down example. Choose a date and press OK then move your mouse over the calendar to reproduce this.

My XAML -

<Grid x:Name="LayoutRoot">
    <controls:DatePicker x:Name="dpTest" 
                         Height="25" 
                         Width="75" 
                         SelectedDateChanged="DatePicker_SelectedDateChanged" />
</Grid>

My code behind -

  Private Sub DatePicker_SelectedDateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
    MessageBox.Show("Test Popup")
End Sub

Any ideas or workarounds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-08-31 06:57:12

嗯,这实际上并不少见。我的一位同事最近在 Windows 窗体应用程序中遇到了非常奇怪的问题,因为他正在使用 MessageBox 来响应第三方菜单控件的单击事件(在菜单被关闭之前)。

一个建议对他不起作用,但可能会起作用对您来说,很好的工作是将呼叫“推送”给调度员。这样,您的 SelectedDateChanged 处理程序将在消息框实际显示之前返回。

Private Sub DatePicker_SelectedDateChanged( ... )

    ' Unfortunately my VB is rusty '
    ' I believe this is the correct syntax. '
    Dispatcher.BeginInvoke(AddressOf ShowDateMessage)

    ' At this point, the message box has *not* been shown '
    ' It will be shown once control returns to the dispatcher '

End Sub

Private Sub ShowDateMessage()

    ' By this point, the DatePicker popup should be closed '
    ' so hopefully the issues you are seeing would be avoided '
    MessageBox.Show("Test Popup")

End Sub

但需要记住以下几点:

  • MessageBox.Show 在 Silverlight 中是独一无二的,因为它是创建模式对话框的唯一方法之一。与 Windows 窗体中的消息循环仍在运行不同,Silverlight 的 UI 线程在返回之前会停止。
  • 该事件已经在日期更改后发生,因此这不是确认更改的好方法。粗略地浏览一下文档表明没有相应的“更改”事件。
  • 根据具体情况,使用 ChildWindow 而不是 MessageBox 可能会更好。这不会出现您所描述的问题,因为虽然它看起来是一个模式对话框,但事实并非如此。

Hmm this is not all that uncommon actually. A coworker of mine recently ran into very strange issues in a Windows Forms application because he was using MessageBox in response to a third party menu control's click event (before the menu had been dismissed.)

One suggestion that didn't work for him but may very well work for you is to "push" the call onto the dispatcher. That way your SelectedDateChanged handler will return before the message box actually gets shown.

Private Sub DatePicker_SelectedDateChanged( ... )

    ' Unfortunately my VB is rusty '
    ' I believe this is the correct syntax. '
    Dispatcher.BeginInvoke(AddressOf ShowDateMessage)

    ' At this point, the message box has *not* been shown '
    ' It will be shown once control returns to the dispatcher '

End Sub

Private Sub ShowDateMessage()

    ' By this point, the DatePicker popup should be closed '
    ' so hopefully the issues you are seeing would be avoided '
    MessageBox.Show("Test Popup")

End Sub

A couple of things to keep in mind though:

  • MessageBox.Show is unique in Silverlight in that it is one of the only ways to create a modal dialog. And unlike in Windows Forms where the message loop is still running, Silverlight's UI thread is stalled until it returns.
  • The event already takes place after the date has changed so this is not a good way to confirm the change. A cursory glance at the docs suggests there isn't a corresponding "Changing" event.
  • Depending on the circumstances, you might just be better off using a ChildWindow instead of MessageBox. This wouldn't have the issues you described because while it appears to be a modal dialog, it's not.
池木 2024-08-31 06:57:12

我在博客中发布了一个解决方法此处通过改变工作流程,不再需要消息框。

I blogged a workaround HERE that make the message box unnecessary by changing the work flow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文