【问题标题】:How to ask to the user if project shutdown by using another WPF Window?如何使用另一个 WPF 窗口询问用户是否关闭项目?
【发布时间】:2018-07-30 17:10:00
【问题描述】:

这是 MainWindow xaml 代码。

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Name="MainWindow" 
    Height="300" 
    Width="500">
</Window> 

这是 Window1 xaml 代码。

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="Window1" 
Height="200" 
Width="500">

<Grid>
    <DockPanel LastChildFill="False">
        <TextBlock DockPanel.Dock="Top" HorizontalAlignment="Stretch" TextWrapping="Wrap" TextAlignment="Justify" Margin="10,20,10,0">
            <TextBlock.Inlines>
                <Run Name="Inline1" Text="Message"/>
            </TextBlock.Inlines>
        </TextBlock>

        <DockPanel DockPanel.Dock="Bottom" LastChildFill="False">
            <Button Name="Button1" Content="No" Width="70" DockPanel.Dock="Right"/>
            <Button Name="Button2" Content="Yes" Width="70" DockPanel.Dock="Right"/>
        </DockPanel>
    </DockPanel>
</Grid>
</Window>

这是 MainWindow vb 代码。

Class MainWindow

Private Sub MainWindow_Closing(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    Dim myWindow As New Window1()
    myWindow.Owner = Application.Current.MainWindow
    myWindow.Inline1.Text = "Do you really want to quit?"
    myWindow.ShowDialog()
    e.Cancel = True
End Sub

End Class

这是Window1 vb代码。

Public Class Window1

Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
    System.Windows.Application.Current.Shutdown()
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Me.Close()
End Sub

End Class

1- 运行这个项目。

2- 单击 X 以关闭此项目。

3- 当项目询问您是否真的要退出时,单击按钮?

4- 查看以下错误;

https://prnt.sc/ih9l5f

【问题讨论】:

  • 你的代码现在做什么?你想让它做什么呢?

标签: c# wpf vb.net winforms visual-studio


【解决方案1】:

我认为(如果我错了,请纠正我)您想从 [Window1] 接收输入,这有点像要求关闭的消息框?所以我猜有几个按钮 [Yes]/[No] [Confirm]/[Dont Shutdown] 用户点击?

如果是这样... 在 Window1 类中声明一个公共布尔值,然后在关闭或应用程序关闭之前根据需要将布尔值设置为 true / false。

Public Class Window1
Public Confirmed as boolean = false

'I have set to default as boolean as the user could theroretically close the
'form and not click on either button!?

Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
    Confirmed = true
    ''System.Windows.Application.Current.Shutdown()
    me.close
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Confirmed = false
    Me.Close()
End Sub

End Class



Class MainWindow

Private Sub MainWindow_Closing(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    Dim myWindow As New Window1()
    ''myWindow.Owner = Application.Current.MainWindow ''I dont know why you need this?
    myWindow.Inline1.Text = "Do you really want to quit?"
    myWindow.ShowDialog()
    if mywindow.confirmed then
         System.Windows.Application.Current.Shutdown()
         ''or simply..
         ''application.exit()
    else
         e.Cancel = True
    end if

End Sub

End Class

这绝不是唯一的方法,请不要认为这个答案是完全正确的,因为您的想法可以通过很多方式实现,其中一种是您已经完成的方式。老实说,我可以在 showdialog 上看到错误发生的原因/方式,但删除窗口并在 MainWindow 中处理会消除 Window1 的问题/错误。

希望这在某种程度上有所帮助 鸡

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2015-12-28
    • 1970-01-01
    相关资源
    最近更新 更多