A View Model for App.Xaml

萝らか妹 提交于 2019-12-08 06:17:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!