【问题标题】:Quit application from a user Control从用户控件退出应用程序
【发布时间】:2013-07-13 04:31:55
【问题描述】:

我的应用程序中有一个 MainWindow。 MainWindow 在其 ContentControl 中托管一个 UserControl(我称之为 MainPage)。 MainPage 进入另一个包含各种控件的 UserControl (KiviPage)。

我正在尝试连接到 MainPage 中的数据库并在 KiviPage 中加载文件。如果这两个操作中的任何一个失败(连接到数据库或文件加载),我必须退出应用程序。这意味着我必须从用户控件中退出应用程序。

最好的方法是什么?

【问题讨论】:

    标签: wpf xaml wpf-controls


    【解决方案1】:

    只需调用用户控件的"Shutdown" from code behind

    Application.Current.Shutdown();
    

    【讨论】:

    • 我无法在 CodeBehind 中编码。两者(数据库连接和文件加载)都是绑定到控件的属性。
    【解决方案2】:

    我认为,您可以通过附加的DependencyProperty 来实施此操作。类似的东西(这是一个简单的工作示例):

    XAML

    <Window x:Class="ShutdownAppHelp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ShutdownAppHelp"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.Resources>
        <Style TargetType="{x:Type CheckBox}">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="local:ProgramBehaviours.Shutdown" Value="True" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
        <Grid>
            <CheckBox Content=" Shutdown" IsChecked="False" />
        </Grid>
    </Window>
    

    Code behind

    namespace ShutdownAppHelp
    {    
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    
        public static class ProgramBehaviours
        {
            // Shutdown program
            public static void SetShutdown(DependencyObject target, bool value)
            {
                target.SetValue(ShutdownProperty, value);
            }
    
            public static readonly DependencyProperty ShutdownProperty =
                                                      DependencyProperty.RegisterAttached("Shutdown",
                                                      typeof(bool),
                                                      typeof(ProgramBehaviours),
                                                      new UIPropertyMetadata(false, OnShutdown));
    
            // Here call function in UIPropertyMetadata()
            private static void OnShutdown(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                if (e.NewValue is bool && ((bool)e.NewValue))
                {
                    Application.Current.Shutdown();             
                }
            }
        }
    }
    

    您可以在DependencyProperty 中放置任何类型的行为,这只能通过代码获得,并将其称为 XAML:

    <DataTrigger Binding="{Binding ElementName=SomeControl, Path=Tag}" Value="Shutdown">
        <Setter Property="local:ProgramBehaviours.Shutdown" Value="True" />
    </DataTrigger>
    

    另外,您可以通过行为代码直接访问它:

    ProgramBehaviours.SetShutdown(SomeControl, Value);
    

    或无条件来自 XAML:

    <SomeControl local:ProgramBehaviours.SetShutdown="True" ... />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-18
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 2017-01-13
      • 1970-01-01
      相关资源
      最近更新 更多