4

アプリのウィンドウにはボーダーがないので、右側のコーナーに終了ボタンがありませんか?どうすれば正しく閉じることができますか?

これが私のやり方です。最初にコマンドをカスタム終了ボタンにバインドします。

<Button Content="Exit" HorizontalAlignment="Left" Margin="327,198,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ExitCommand}"/>

ボタンがクリックされたときに ViewModel で例外をスローするよりも。

class ViewModel:NotificationObject
{
    public ViewModel()
    {
        this.ExitCommand = new DelegateCommand(new Action(this.ExecuteExitCommand));
    }

    public DelegateCommand ExitCommand { get; set; }

    public void ExecuteExitCommand()
    {
        throw new ApplicationException("shutdown");
    }
}

Application クラスで例外をキャッチする

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Bootstrapper bootstrapper = new Bootstrapper();
        AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
        try
        {
            bootstrapper.Run();
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
    }

    private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        HandleException(e.ExceptionObject as Exception);
    }

    private static void HandleException(Exception ex)
    {
        if (ex == null)
            return;
        Environment.Exit(1);
    }
}
4

2 に答える 2

7

たぶん使用しますApplication.Current.Shutdown()か??

public void ExecuteExitCommand()
{
    Application.Current.Shutdown();
}

例外を通信メカニズムとして使用するのは奇妙に思えます。

なんらかの理由で VM で ShutDown() を呼び出したくない場合は、Messenger(PrismではEventAggregator) を使用してカスタム メッセージを送信します。このメッセージは、アプリケーション クラスまたは MainWindow のコード ビハインドからサブスクライブして同じものを呼び出すことができます。Application.Current.Shutdown()

于 2013-05-06T10:53:24.517 に答える
0

私は個人的にやりたい:

private DelegateCommand terminateApplication;
public ICommand TerminateApplication => terminateApplication ??= new 
DelegateCommand(PerformTerminateApplication);

private void PerformTerminateApplication()
{
    Environment.Exit(0);
}
于 2021-12-29T07:20:11.920 に答える