开发者

A View Model for App.Xaml

Can we have a viewMo开发者_高级运维del for App.Xaml so that we can do some logical deductions on startUp and also form a starting point of app...


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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜