我面临一个问题,因为几个星期/几个月,我希望有人可以帮助我;) 此WPF问题仅在通过触摸屏使用时才会出现,它可以与鼠标一起使用。 重现的概念非常简单: - 1个带有列表框或列表视图的窗口 - 使用简单项目填充列表(例如,仅文本) - 带有2个或更多简单按钮的第二个窗口
当您通过触摸屏选择窗口1中列表中的项目时,它将以模态模式(ShowDialog)打开第二个窗口。当第二个窗口打开时,您无法在第一次触摸时触摸任何按钮! 如果你用鼠标做同样的事情它工作正常!当你调用窗口2 ShowDialog时,看起来触摸事件没有完成(当它不是模态时它工作正常:使用Show)。
这里是XAML窗口1:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="661" WindowStartupLocation="CenterScreen">
<StackPanel>
<ListView x:Name="lstItems" Height="300" Margin="10,10,20,10" Width="Auto" ItemsSource="{Binding}" BorderBrush="{x:Null}"
SelectionMode="Single" Visibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Hidden">
</ListView>
</StackPanel>
</Window>
这里是窗口1背后的代码:
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
With lstItems
.Items.Clear()
For i = 0 To 50
.Items.Add("test #" & i.ToString)
Next
End With
End Sub
Private Sub lstItems_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles lstItems.SelectionChanged
Dim _frm As frm2 = New frm2
_frm.ClickedItem = lstItems.SelectedItem.ToString
_frm.ShowDialog()
_frm.Close()
_frm = Nothing
End Sub
End Class
这里窗口2 xaml:
<Window x:Class="frm2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="frm2" Height="393" Width="661" WindowStartupLocation="CenterScreen">
<Grid>
<UniformGrid Columns="2">
<Button x:Name="cmdbutton1" Height="128" Content="Button1"/>
<Button x:Name="cmdbutton2" Height="128" Content="Button2"/>
</UniformGrid>
</Grid>
</Window>
这里是窗口2背后的代码:
Public Class frm2
Property ClickedItem As String = ""
Private Sub frm2_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Me.Title = ClickedItem
End Sub
Private Sub cmdbutton1_Click(sender As Object, e As RoutedEventArgs) Handles cmdbutton1.Click
Me.Close()
End Sub
Private Sub cmdbutton2_Click(sender As Object, e As RoutedEventArgs) Handles cmdbutton2.Click
Me.Close()
End Sub
End Class
提前感谢您的帮助......
答案 0 :(得分:3)
解决方案就在这里,您只需在Async中声明您的sub并执行任务等待...
-1
答案 1 :(得分:0)
这个问题在Microsoft KB中被引用,它已经被标记为自.NET 4.5.1以来已经解决,无论如何它不是因为我可以轻松地重现它... 我刚刚在这里找到了一个解决方法:
只需替换原始代码:
Private Sub lstItems_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles lstItems.SelectionChanged
Dim _frm As frm2 = New frm2
_frm.ClickedItem = lstItems.SelectedItem.ToString
_frm.ShowDialog()
_frm.Close()
_frm = Nothing
End Sub
有了这个:
Private Sub lstItems_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles lstItems.SelectionChanged
Dispatcher.BeginInvoke(Sub()
Dim _frm As frm2 = New frm2
_frm.ClickedItem = lstItems.SelectedItem.ToString
_frm.ShowDialog()
_frm.Close()
_frm = Nothing
结束子) 万一 结束子
答案 2 :(得分:0)
ShowDialog
中的{p> Dispatcher.BeginInvoke
为我工作。它有点慢,但至少工作。如果您从TouchUp
事件打开对话框,则相同,但如果您从按钮的Click
事件中打开,则表示良好。奇怪的错误。