这是App.xaml中的 XAML代码:
<Application x:Class="Company.Product.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Company.Windows.Themes.Theme1;component/Themes/System.Windows.xaml"/>
<ResourceDictionary Source="/Company.Windows.Themes.Theme1;component/Themes/Company.Windows.Controls.xaml"/>
<ResourceDictionary Source="/Company.Windows.Themes.Theme1;component/Themes/Company.Windows.Controls.Data.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
相当于:
App.xaml:
<Application x:Class="Company.Product.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
</Application>
Run Code Online (Sandbox Code Playgroud)
在App.xaml.cs中:
public partial class App : Application
{
public App()
{
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("/Company.Windows.Themes.Theme1;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("/Company.Windows.Themes.Theme1;component/Themes/Company.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("/Company.Windows.Themes.Theme1;component/Themes/Company.Windows.Controls.Data.xaml", UriKind.RelativeOrAbsolute)
});
}
}
Run Code Online (Sandbox Code Playgroud)
一面注意:我是否需要调用Application.Current.Resources.MergedDictionaries.Clear(); 在这种情况下,我开始添加合并的词典?我想目前默认的MergedDictionaries集合最初是空的.
我正在编写我的第一个WPF应用程序,我想请求您帮助解决我遇到的问题.
我试图遵循MVVM模式,我来到了需要实现模态对话框的地步.我用谷歌搜索/阅读了一段时间,我能够找到解决方案.然而,当重构时,我遇到了一个困境,即使用DI(构造函数注入)作为服务定位器的替代.
我将参考这些:http://pastebin.com/S6xNjtWW.
我非常喜欢Roboblob的方法:
第一:他创建了一个模态对话框(界面)的抽象.我命名接口IModalDialog,它是这样的:
public interface IModalDialog
{
bool? DialogResult { get; set; }
object DataContext { get; set; }
void Show();
bool? ShowDialog();
void Close();
event EventHandler Closed;
}
Run Code Online (Sandbox Code Playgroud)
第二:模态对话服务的抽象:
public interface IModalDialogService
{
void ShowDialog<TDialogViewModel>(IModalDialog view, TDialogViewModel viewModel, Action<TDialogViewModel> onDialogClose) where TDialogViewModel : class;
void ShowDialog<TDialogViewModel>(IModalDialog view, TDialogViewModel viewModel) where TDialogViewModel : class;
}
Run Code Online (Sandbox Code Playgroud)
第三: IModalDialogService的具体实现:
public class ModalDialogService : IModalDialogService
{
public void ShowDialog<TDialogViewModel>(IModalDialog view, TDialogViewModel viewModel, Action<TDialogViewModel> onDialogClose) where …Run Code Online (Sandbox Code Playgroud)