问题
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
.
来源:https://stackoverflow.com/questions/7647247/a-view-model-for-app-xaml