应用程序启动检查,我应该在 MVVM 模式中的哪里执行它们?

发布于 2024-12-26 22:04:12 字数 179 浏览 2 评论 0原文

我想在检查本地文件的数据库连接设置后,使用主视图模型的实例启动 C# / WPF / MVVM 应用程序。

一旦我有了连接设置,我会将它们绑定到一个类并将它们包含在我的主视图模型中。

我想我会检查本地文件并在 App.xaml.cs 中创建主视图模型和主窗口的实例

有什么想法吗?有更好的方法吗?

I would like to start up a C# / WPF / MVVM Application with an instance of my Main View Model after checking a local file for database connection settings.

Once I have the connection settings I will bind them to a class and include them in my Main View Model.

I'm thinking that I would check the local file and create the instances of the Main View Model, and Main Window in App.xaml.cs

Any thoughts ? Is there a better way to do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短叹 2025-01-02 22:04:12

我通常按​​照你说的做:在 App.xaml.cs 中创建初始 View 和 ViewModel

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    var vm = new MainViewModel();
    // set vm properties

    var view = new MainView();
    view.DataContext = vm;

    view.Show();
}

I usually do just what you said: create the initial View and ViewModel in App.xaml.cs

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    var vm = new MainViewModel();
    // set vm properties

    var view = new MainView();
    view.DataContext = vm;

    view.Show();
}
旧街凉风 2025-01-02 22:04:12

您可以在负责导航(加载(ViewModel 和)View 并侦听导航消息)的单独类中实现此职责。

在 App 类中,您只需实例化并初始化此类。

You could implement this responsibility in a separate class that is responsible for navigation (loading (a ViewModel and) a View and listening for navigation messages).

In the App class you would only have to instantiate and initialize this class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文