【发布时间】:2017-09-05 22:26:31
【问题描述】:
我正在尝试遵循 MVVM 的最佳做法(据我了解)。我有一个解决方案,在我的主项目中使用 Views,在另一个项目中使用 ViewModels。我的主项目中有一个静态类,它根据命令行参数(例如 /env=Production 或 /env=Development)返回当前环境的连接字符串。
我想通过 XAML 将连接字符串注入 ViewModel。以下工作,但它需要我在 XAML 中硬编码连接字符串:
<Window.DataContext>
<ObjectDataProvider xmlns:sys="clr-namespace:System;assembly=mscorlib" ObjectType="vm:SchedulerViewModel">
<ObjectDataProvider.ConstructorParameters>
<sys:String>Data Source =.; Initial Catalog = MyDb_Dev; Integrated Security = true;</sys:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
</Window.DataContext>
有没有办法将硬编码字符串Data Source =.; Initial Catalog = MyDb_Dev; Integrated Security = true; 替换为调用我的静态类(名为全局)中的公共属性:Global.CnString?还是我从根本上“做错了”?
【问题讨论】: