Can we have a viewModel for App.Xaml so that we can do some logical deductions on startUp and also form a starting point of app...
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
No, App.xaml is not a Window class, it is your Application class.
You can still overwrite the OnStartup()
method of it to handle your own custom logic and to startup specific Views/ViewModels.
For example,
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var login = new LoginDialog();
var loginVm = new LoginViewModel();
login.DataContext = loginVm;
login.ShowDialog();
if (!login.DialogResult.GetValueOrDefault())
{
Environment.Exit(0);
}
// Providing we have a successful login, startup application
var app = new ShellView();
var context = new ShellViewModel(loginVm.CurrentUser);
app.DataContext = context;
app.Show();
}
回答2:
No we cannot have view models at App level. As @BoltClock suggested, It isnt something that has a data context to which we bind an instance of any class. MVVM does not work with App
.