0

メインウィンドウを起動する前に、EFを使用してデータベースに接続できるかどうかを確認したいWPFデスクトップアプリケーションがあります。そのため、データベースに接続しようとしましたが、例外が発生した場合は、ユーザーに正しいデータベース接続文字列を入力するように求めるウィンドウを表示するだけです。

ユーザーが接続文字列を入力した後、データベースへの接続を再試行し、接続された場合はメインウィンドウを起動してアプリケーションを起動します。

編集:コードを少し変更しましたが、それでも機能しません。1つのウィンドウを閉じてから、Applicationクラスで別のウィンドウを表示しようとすることだと思います。ServerConfigurationWinが表示されていない場合に機能します。

ただし、データベース構成ウィンドウをユーザーに表示した後、アプリケーションはApp.xamlファイルで宣言されたStartupUriウィンドウのコンストラクターを呼び出しますが、画面には何も表示されません。

ありがとう。

これが私のアプリコードです:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        bool success = false;
        while (!success)
        {
            try
            {
                DemirbasContext context = new DemirbasContext();
                DbInitializer initializer = new DbInitializer();
                Database.SetInitializer<DemirbasContext>(initializer);
                context.Database.Initialize(false);
                success = true;
            }
            catch (ProviderIncompatibleException)
            {
                ServerConfigurationWin configurationWin = new ServerConfigurationWin();
                if (!configurationWin.ShowThemAll())
                {
                    // ShowThemAll() returns that operation is canceled.
                    App.Current.Shutdown();
                    return;
                }
                success = false;
            }
        }
    }

    public App()
    {

    }

    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        // Process unhandled exception do stuff below
        Console.WriteLine("***Message***");
        Console.WriteLine(e.Exception.Message);
        Console.WriteLine("***Stack Trace***");
        Console.WriteLine(e.Exception.StackTrace);
        Console.WriteLine("***Target Site***");
        Console.WriteLine(e.Exception.TargetSite);
        // Prevent default unhandled exception processing
        e.Handled = true;
    }
}

App.xaml:

<Application x:Class="Demirbas.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         DispatcherUnhandledException="App_DispatcherUnhandledException"
         StartupUri="Enterance.xaml">
<Application.Resources>
    <Style TargetType="Button">
        <Setter Property="Height" Value="25" />
        <Setter Property="Width" Value="75" />
        <Setter Property="MinWidth" Value="25" />
        <Setter Property="Margin" Value="5,0" />
        <Setter Property="Padding" Value="0" />
    </Style>
    <Style TargetType="Label">
        <Setter Property="Margin" Value="5,0" />
    </Style>

</Application.Resources>

4

2 に答える 2

0

WPFアプリケーションがメインウィンドウを呼び出すためのエントリポイントを探し始めたら、これを試してみてください。App.XamlのStartupUriを使用して、デフォルトのメインウィンドウを変更できます。または、このように起動呼び出しをオーバーライドできます。

App.Xaml.csクラス

 protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        bool condition = false;

        //you logic. / you code.

        if (condition)
            (App.Current.MainWindow = new MyMainWindow ()).Show();
        else
            App.Current.Shutdown();
    }
于 2012-10-19T13:56:20.510 に答える
0

Applicationクラスから2番目のウィンドウを表示できないようです。そのため、アプリケーションをシャットダウンして再起動する必要がありました。

于 2012-11-02T14:02:14.873 に答える