Quit application from a user Control

2019-02-23 17:11发布

问题:

I have a MainWindow in my application. The MainWindow hosts a UserControl in its ContentControl(I call this MainPage). MainPage goes onto hosts another UserControl which contains all sorts of controls on it (KiviPage).

I am trying to connect to a database in MainPage and load a file in the KiviPage. If any of the two operations fail (connection to database or file loading) I have to quit the application. Which means I have to quit the application from the user controls.

Whats the best way to do this?

回答1:

I think, you may implement this action through a attached DependencyProperty. Something like that (it's a simple work example):

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();             
            }
        }
    }
}

You can put any kind of behavior in DependencyProperty, which is available only through the code and call it a XAML:

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

Also, you can access it directly through the code of behavior:

ProgramBehaviours.SetShutdown(SomeControl, Value);

Or from XAML without condition:

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


回答2:

Simply call "Shutdown" from code behind of the user control:

Application.Current.Shutdown();