創建新的WPF 4.5 MVVM Light應用程序後,我想更改啓動URI,以便在應用程序啓動之前進行一些檢查。我做了如下修改的App.xaml:MVVM Light - 更改啓動URI
<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">
要:
<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">
,並增加了OnStartup
方法將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();
}
}
這樣做似乎改變的背景下,窗口運行。我嘗試設置數據上下文到MainViewModel,但這似乎沒有幫助。
你對'Application'定義做了什麼改變?我看不出有什麼區別......你還有'StartupUri =「MainWindow.xaml」'。你在哪裏設置'DataContext'?我也沒有看到。 – Sheridan
看起來很好,儘管這完全取決於我可憐的格式! –
就DataContext而言,改變它並沒有什麼區別,這就是爲什麼我沒有包含它。 –