0

Having created a new WPF 4.5 MVVM Light Application, I wanted to change the startup URI so that I could do some checks before the app starts. I made the following change to App.xaml:

<Application x:Class="MvvmLight1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         StartupUri="MainWindow.xaml"
         mc:Ignorable="d">

To:

<Application x:Class="MvvmLight1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d">

And added an OnStartup method to the App.xaml.cs:

public partial class App : Application
{
    static App()
    {
        DispatcherHelper.Initialize();
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        //base.OnStartup(e);

        MainWindow win = new MainWindow();
        win.Show();
    }
}

Doing this seems to change the context that the window runs in. I did try setting the data context to the MainViewModel, but this didn't seem to help.

4

2 に答える 2

0

StartupハンドラーをApplication定義に追加していないようです...これを試してください:

<Application x:Class="MvvmLight1.App"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d" Startup="OnStartup">   <!--   <<< Here   -->

更新 >>>

これにはメソッドを使用できません...起動ハンドラーに正しいメソッド定義を使用していないようです...私のものは次のようになります:

public void App_Startup(object sender, StartupEventArgs e)
{

}

object senderパラメータをハンドラに追加してみてください。

于 2013-10-10T09:23:56.337 に答える