diff --git a/Source/Demos/Wif.Demo.Common/Interfaces/IActivatable.cs b/Source/Demos/Wif.Demo.Common/Interfaces/IActivatable.cs
new file mode 100644
index 0000000..0c98343
--- /dev/null
+++ b/Source/Demos/Wif.Demo.Common/Interfaces/IActivatable.cs
@@ -0,0 +1,10 @@
+namespace Wif.Demo.Common.Interfaces
+{
+ ///
+ /// Use this Interface when you want to mark a control as recieving View
+ /// Activation when it doesn't have a backing ViewModel.
+ ///
+ public interface IActivatable
+ {
+ }
+}
\ No newline at end of file
diff --git a/Source/Demos/Wif.Demo.Common/Interfaces/IActivatableView.cs b/Source/Demos/Wif.Demo.Common/Interfaces/IActivatableView.cs
new file mode 100644
index 0000000..3eca411
--- /dev/null
+++ b/Source/Demos/Wif.Demo.Common/Interfaces/IActivatableView.cs
@@ -0,0 +1,10 @@
+namespace Wif.Demo.Common.Interfaces
+{
+ ///
+ /// Use this Interface when you want to mark a control as receiving View
+ /// Activation when it doesn't have a backing ViewModel.
+ ///
+ public interface IActivatableView
+ {
+ }
+}
\ No newline at end of file
diff --git a/Source/Demos/Wif.Demo.Common/Interfaces/IViewFor.cs b/Source/Demos/Wif.Demo.Common/Interfaces/IViewFor.cs
new file mode 100644
index 0000000..f192988
--- /dev/null
+++ b/Source/Demos/Wif.Demo.Common/Interfaces/IViewFor.cs
@@ -0,0 +1,30 @@
+namespace Wif.Demo.Common.Interfaces
+{
+ ///
+ /// This base class is mostly used by the Framework. Implement
+ /// instead.
+ ///
+ public interface IViewFor : IActivatable
+ {
+ ///
+ /// Gets or sets the View Model associated with the View.
+ ///
+ object ViewModel { get; set; }
+ }
+
+#pragma warning disable SA1402 // File may only contain a single type
+ ///
+ /// Implement this interface on your Views to support Routing and Binding.
+ ///
+ /// The type of ViewModel.
+ public interface IViewFor : IViewFor
+#pragma warning restore SA1402 // File may only contain a single type
+ where T : class
+ {
+ ///
+ /// Gets or sets the ViewModel corresponding to this specific View. This should be
+ /// a DependencyProperty if you're using XAML.
+ ///
+ new T ViewModel { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Source/Demos/Wif.Demo.Common/UserControlBase.cs b/Source/Demos/Wif.Demo.Common/UserControlBase.cs
new file mode 100644
index 0000000..1e8a6c7
--- /dev/null
+++ b/Source/Demos/Wif.Demo.Common/UserControlBase.cs
@@ -0,0 +1,42 @@
+using System.Windows;
+using System.Windows.Controls;
+using Wif.Demo.Common.Interfaces;
+
+namespace Wif.Demo.Common
+{
+ public class UserControlBase : UserControl, IViewFor where TViewModel : class
+ {
+ ///
+ /// The view model dependency property.
+ ///
+ public static readonly DependencyProperty ViewModelProperty =
+ DependencyProperty.Register(nameof(ViewModel), typeof(TViewModel), typeof(UserControlBase), new PropertyMetadata(null));
+
+ ///
+ /// Gets the binding root view model.
+ ///
+ public TViewModel BindingRoot => ViewModel;
+
+ #region IViewFor Members
+
+ ///
+ ///
+ ///
+ public TViewModel ViewModel
+ {
+ get => (TViewModel)GetValue(ViewModelProperty);
+ set => SetValue(ViewModelProperty, value);
+ }
+
+ ///
+ ///
+ ///
+ object IViewFor.ViewModel
+ {
+ get => ViewModel;
+ set => ViewModel = (TViewModel)value;
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
diff --git a/Source/Demos/Wif.Demo/App.xaml b/Source/Demos/Wif.Demo/App.xaml
index 5fd32a5..e7be608 100644
--- a/Source/Demos/Wif.Demo/App.xaml
+++ b/Source/Demos/Wif.Demo/App.xaml
@@ -1,9 +1,7 @@
-
-
-
+ Exit="App_OnExit"
+ Startup="App_OnStartup">
+
diff --git a/Source/Demos/Wif.Demo/App.xaml.cs b/Source/Demos/Wif.Demo/App.xaml.cs
index bf6bbcd..6e95084 100644
--- a/Source/Demos/Wif.Demo/App.xaml.cs
+++ b/Source/Demos/Wif.Demo/App.xaml.cs
@@ -1,10 +1,17 @@
-using System;
+using Microsoft.Extensions.Hosting;
+using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
+using System.Diagnostics;
using System.Linq;
+using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using System.Windows;
+using System.Windows.Threading;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using Wif.Demo.IocExtensions;
namespace Wif.Demo
{
@@ -13,5 +20,128 @@ namespace Wif.Demo
///
public partial class App : Application
{
+ #region Private Fields
+
+ ///
+ /// 定义程序当前的UI调度器。
+ ///
+ public Dispatcher CurrentUIDispatcher { get; private set; }
+
+ #endregion Private Fields
+
+ #region Constructors
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public App()
+ {
+ InitHostBuilder();
+ CurrentUIDispatcher = Current.Dispatcher;
+ }
+
+ #endregion Constructors
+
+ #region Fields
+
+ ///
+ /// 定义通用主机字段。
+ ///
+ private IHost _host;
+
+ ///
+ /// 获取或设置依赖注入的容器。
+ ///
+ public IServiceProvider Container { get; private set; }
+
+ #endregion Fields
+
+ #region Methods
+
+ ///
+ /// UI线程抛出全局异常事件处理
+ ///
+ ///
+ ///
+ [HandleProcessCorruptedStateExceptions]
+ private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
+ {
+ try
+ {
+ MessageBox.Show($"UI线程全局异常\r\n{e.Exception}", "警告");
+ e.Handled = true;
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("应用程序发生不可恢复的异常,将要退出!", "警告");
+ }
+ }
+
+ ///
+ /// The App_OnExit
+ ///
+ /// The sender
+ /// The e
+ private async void App_OnExit(object sender, ExitEventArgs e)
+ {
+ using (_host)
+ {
+ await _host.StopAsync(TimeSpan.FromSeconds(1));
+ }
+ }
+
+ ///
+ /// The App_OnStartup
+ ///
+ /// The sender
+ /// The e
+ private async void App_OnStartup(object sender, StartupEventArgs e)
+ {
+ // 注册异常事件。
+ Current.DispatcherUnhandledException += App_OnDispatcherUnhandledException;
+ AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
+
+ await _host.StartAsync();
+
+ var mainWindow = _host.Services.GetRequiredService();
+ mainWindow.Show();
+ }
+
+ ///
+ /// 非UI线程抛出全局异常事件处理
+ ///
+ ///
+ ///
+ [HandleProcessCorruptedStateExceptions]
+ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
+ {
+ try
+ {
+ if (e.ExceptionObject is not Exception ex)
+ return;
+ CurrentUIDispatcher?.InvokeAsync(() => { MessageBox.Show($"非UI线程全局异常\r\n{ex.Message}"); });
+ }
+ catch (Exception ex)
+ {
+ CurrentUIDispatcher?.InvokeAsync(() => { MessageBox.Show("应用程序发生不可恢复的异常,将要退出!"); });
+ }
+ }
+
+ ///
+ /// 创建和配置构建器对象。
+ ///
+ private void InitHostBuilder()
+ {
+ // 初始化依赖注入
+ _host = new HostBuilder()
+ .ConfigureView()
+ .UseEnvironment(Environments.Development)
+ .Build();
+
+ Container = _host.Services;
+ Locator.SetLocator(Container);
+ }
+
+ #endregion Methods
}
}
diff --git a/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoView.xaml b/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoView.xaml
new file mode 100644
index 0000000..8a47af7
--- /dev/null
+++ b/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoView.xaml
@@ -0,0 +1,70 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoView.xaml.cs b/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoView.xaml.cs
new file mode 100644
index 0000000..c88f52b
--- /dev/null
+++ b/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoView.xaml.cs
@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace Wif.Demo.Examples.BindingDemo
+{
+ ///
+ /// BindingDemoView.xaml 的交互逻辑
+ ///
+ public partial class BindingDemoView
+ {
+ public BindingDemoView()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoViewModel.cs b/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoViewModel.cs
new file mode 100644
index 0000000..61747ad
--- /dev/null
+++ b/Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoViewModel.cs
@@ -0,0 +1,61 @@
+using System.Collections.ObjectModel;
+using Frontier.Wif.Core.ComponentModel;
+using Wif.Demo.Common;
+
+namespace Wif.Demo.Examples.BindingDemo
+{
+ public class BindingDemoViewModel: ViewModelBase
+ {
+ #region Fields
+
+ ///
+ /// Defines the _mobilePhoneCollection
+ ///
+ private ObservableCollection _mobilePhoneCollection = new ObservableCollection();
+
+ #endregion
+
+ #region Properties
+
+ ///
+ /// Gets or sets the MobilePhoneCollection
+ ///
+ public ObservableCollection MobilePhoneCollection
+ {
+ get => _mobilePhoneCollection;
+ set => _mobilePhoneCollection = value;
+ }
+
+ #endregion
+
+ public BindingDemoViewModel()
+ {
+ var model = MobilePhoneSingletonModel.Instance;
+ model.Brand = Brand.Apple;
+ model.NumberOfCPUCore = NumberOfCPUCore.Quad;
+ model.RAM = RAM._4GB;
+ model.ROM = ROM._64GB;
+ model.ScreenResolution = ScreenResolution.FHD;
+ model.ScreenSize = 5.2f;
+ MobilePhoneCollection.Add(model);
+
+ model = MobilePhoneSingletonModel.Instance;
+ model.Brand = Brand.Apple;
+ model.NumberOfCPUCore = NumberOfCPUCore.Quad;
+ model.RAM = RAM._4GB;
+ model.ROM = ROM._64GB;
+ model.ScreenResolution = ScreenResolution.FHD;
+ model.ScreenSize = 5.2f;
+ MobilePhoneCollection.Add(model);
+
+ model = MobilePhoneSingletonModel.Instance;
+ model.Brand = Brand.Apple;
+ model.NumberOfCPUCore = NumberOfCPUCore.Quad;
+ model.RAM = RAM._4GB;
+ model.ROM = ROM._64GB;
+ model.ScreenResolution = ScreenResolution.FHD;
+ model.ScreenSize = 5.2f;
+ MobilePhoneCollection.Add(model);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Source/Demos/Wif.Demo/Examples/FileHelperDemo/FileHelperDemoView.xaml b/Source/Demos/Wif.Demo/Examples/FileHelperDemo/FileHelperDemoView.xaml
new file mode 100644
index 0000000..8e36276
--- /dev/null
+++ b/Source/Demos/Wif.Demo/Examples/FileHelperDemo/FileHelperDemoView.xaml
@@ -0,0 +1,12 @@
+
+
+
+
+
diff --git a/Source/Demos/Wif.Demo/Examples/FileHelperDemo/FileHelperDemoView.xaml.cs b/Source/Demos/Wif.Demo/Examples/FileHelperDemo/FileHelperDemoView.xaml.cs
new file mode 100644
index 0000000..ae40c18
--- /dev/null
+++ b/Source/Demos/Wif.Demo/Examples/FileHelperDemo/FileHelperDemoView.xaml.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+using Frontier.Wif.Utilities.Helpers;
+
+namespace Wif.Demo.Examples.FileHelperDemo
+{
+ ///
+ /// FileHelperDemoView.xaml 的交互逻辑
+ ///
+ public partial class FileHelperDemoView : UserControl
+ {
+ public FileHelperDemoView()
+ {
+ InitializeComponent();
+ }
+
+ private void CreateDirectory_OnClick(object sender, RoutedEventArgs e)
+ {
+ var file = @"D:\Users\leoli\Documents\MPSExport\test_file_dir\test_file.txt";
+ var dir = @"D:\Users\leoli\Documents\MPSExport\test_dir";
+ FileHelper.CreateDirectory(file);
+ FileHelper.CreateDirectory(dir);
+ }
+ }
+}
diff --git a/Source/Demos/Wif.Demo/IocExtensions/HostBuilderViewExtensions.cs b/Source/Demos/Wif.Demo/IocExtensions/HostBuilderViewExtensions.cs
new file mode 100644
index 0000000..5229eb1
--- /dev/null
+++ b/Source/Demos/Wif.Demo/IocExtensions/HostBuilderViewExtensions.cs
@@ -0,0 +1,38 @@
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Wif.Demo.Examples.BindingDemo;
+using Wif.Demo.Examples.FileHelperDemo;
+
+namespace Wif.Demo.IocExtensions
+{
+ public static class HostBuilderViewExtensions
+ {
+ ///
+ /// 配置视图依赖注入。
+ ///
+ /// IHostBuilder
+ /// IHostBuilder
+ public static IHostBuilder ConfigureView(this IHostBuilder hostBuilder)
+ {
+ hostBuilder.ConfigureServices((hostBuilderContext, serviceCollection) =>
+ {
+ #region 注入View
+
+ serviceCollection.AddTransient();
+ serviceCollection.AddTransient();
+ serviceCollection.AddTransient();
+
+ #endregion 注入View
+
+ #region 注入ViewModel
+
+ serviceCollection.AddTransient();
+ serviceCollection.AddTransient();
+ serviceCollection.AddTransient();
+
+ #endregion 注入ViewModel
+ });
+ return hostBuilder;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Source/Demos/Wif.Demo/Locator.cs b/Source/Demos/Wif.Demo/Locator.cs
new file mode 100644
index 0000000..e654c2c
--- /dev/null
+++ b/Source/Demos/Wif.Demo/Locator.cs
@@ -0,0 +1,22 @@
+using System;
+
+namespace Wif.Demo
+{
+ ///
+ /// A Locator which will host the container for dependency injection based operations.
+ ///
+ public static class Locator
+ {
+ ///
+ /// Gets the read only dependency resolver.
+ ///
+ public static IServiceProvider Container { get; private set; }
+
+ /// Allows setting the dependency resolver.
+ /// The dependency resolver to set.
+ public static void SetLocator(IServiceProvider serviceProvider)
+ {
+ Container = serviceProvider;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Source/Demos/Wif.Demo/MainViewModel.cs b/Source/Demos/Wif.Demo/MainViewModel.cs
index c70e508..6a386fe 100644
--- a/Source/Demos/Wif.Demo/MainViewModel.cs
+++ b/Source/Demos/Wif.Demo/MainViewModel.cs
@@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using System.Windows.Controls;
using Frontier.Wif.Core.ComponentModel;
using Wif.Demo.Common;
@@ -14,56 +15,14 @@ namespace Wif.Demo
///
public class MainViewModel : ViewModelBase
{
- #region Fields
-
- ///
- /// Defines the _mobilePhoneCollection
- ///
- private ObservableCollection _mobilePhoneCollection = new ObservableCollection();
-
- #endregion
-
- #region Properties
-
+ private UserControl _demoView;
///
- /// Gets or sets the MobilePhoneCollection
+ /// 获取或设置Demo视图。
///
- public ObservableCollection MobilePhoneCollection
+ public UserControl DemoView
{
- get => _mobilePhoneCollection;
- set => _mobilePhoneCollection = value;
- }
-
- #endregion
-
- public MainViewModel()
- {
- var model = MobilePhoneSingletonModel.Instance;
- model.Brand = Brand.Apple;
- model.NumberOfCPUCore = NumberOfCPUCore.Quad;
- model.RAM = RAM._4GB;
- model.ROM = ROM._64GB;
- model.ScreenResolution = ScreenResolution.FHD;
- model.ScreenSize = 5.2f;
- MobilePhoneCollection.Add(model);
-
- model = MobilePhoneSingletonModel.Instance;
- model.Brand = Brand.Apple;
- model.NumberOfCPUCore = NumberOfCPUCore.Quad;
- model.RAM = RAM._4GB;
- model.ROM = ROM._64GB;
- model.ScreenResolution = ScreenResolution.FHD;
- model.ScreenSize = 5.2f;
- MobilePhoneCollection.Add(model);
-
- model = MobilePhoneSingletonModel.Instance;
- model.Brand = Brand.Apple;
- model.NumberOfCPUCore = NumberOfCPUCore.Quad;
- model.RAM = RAM._4GB;
- model.ROM = ROM._64GB;
- model.ScreenResolution = ScreenResolution.FHD;
- model.ScreenSize = 5.2f;
- MobilePhoneCollection.Add(model);
+ get => _demoView;
+ set => this.RaiseAndSetIfChanged(ref _demoView, value);
}
}
}
diff --git a/Source/Demos/Wif.Demo/MainWindow.xaml b/Source/Demos/Wif.Demo/MainWindow.xaml
index 2b6525f..69bbbef 100644
--- a/Source/Demos/Wif.Demo/MainWindow.xaml
+++ b/Source/Demos/Wif.Demo/MainWindow.xaml
@@ -2,77 +2,25 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:demo="clr-namespace:Wif.Demo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:common="clr-namespace:Wif.Demo.Common;assembly=Wif.Demo.Common"
- mc:Ignorable="d"
- Title="MainWindow"
- Height="450"
+ Title="Wif Demo"
Width="800"
- Loaded="MainWindow_OnLoaded">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+ Height="450"
+ d:DataContext="{d:DesignInstance demo:MainViewModel}"
+ mc:Ignorable="d">
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
+
diff --git a/Source/Demos/Wif.Demo/MainWindow.xaml.cs b/Source/Demos/Wif.Demo/MainWindow.xaml.cs
index 54e2938..7142c47 100644
--- a/Source/Demos/Wif.Demo/MainWindow.xaml.cs
+++ b/Source/Demos/Wif.Demo/MainWindow.xaml.cs
@@ -13,6 +13,7 @@
using System.Windows.Navigation;
using System.Windows.Shapes;
using Frontier.Wif.Utilities.Helpers;
+using Microsoft.Extensions.DependencyInjection;
namespace Wif.Demo
{
@@ -21,22 +22,12 @@ namespace Wif.Demo
///
public partial class MainWindow : Window
{
- public MainWindow()
- {
- InitializeComponent();
- }
+ private MainViewModel ViewModel { get; set; }
- private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
+ public MainWindow(MainViewModel viewModel)
{
- DataContext = new MainViewModel();
- }
-
- private void CreateDirectory_OnClick(object sender, RoutedEventArgs e)
- {
- var file = @"D:\Users\leoli\Documents\MPSExport\test_file_dir\test_file.txt";
- var dir = @"D:\Users\leoli\Documents\MPSExport\test_dir";
- FileHelper.CreateDirectory(file);
- FileHelper.CreateDirectory(dir);
+ InitializeComponent();
+ DataContext = ViewModel = viewModel;
}
}
}
diff --git a/Source/Demos/Wif.Demo/AssemblyInfo.cs b/Source/Demos/Wif.Demo/Properties/AssemblyInfo.cs
similarity index 100%
rename from Source/Demos/Wif.Demo/AssemblyInfo.cs
rename to Source/Demos/Wif.Demo/Properties/AssemblyInfo.cs
diff --git a/Source/Demos/Wif.Demo/Wif.Demo.csproj b/Source/Demos/Wif.Demo/Wif.Demo.csproj
index 70ad0f2..cfa44dc 100644
--- a/Source/Demos/Wif.Demo/Wif.Demo.csproj
+++ b/Source/Demos/Wif.Demo/Wif.Demo.csproj
@@ -29,6 +29,10 @@
+
+
+
+
diff --git a/Source/Wif.sln.licenseheader b/Source/Wif.sln.licenseheader
new file mode 100644
index 0000000..aae6a2b
--- /dev/null
+++ b/Source/Wif.sln.licenseheader
@@ -0,0 +1,22 @@
+extensions: designer.cs generated.cs
+extensions: .cs .cpp .h
+/**************************************************************************
+* File Name:%FileName%
+* Description:To be added...
+* Copyright:Copyright © %CreationYear% LeoYang-Chuese. All rights reserved.
+* Creator:%UserDisplayName%
+* Create Time:%CreationYear%/%CreationMonth%/%CreationDay%
+*Project Address:https://github.com/LeoYang-Chuese/wif
+**************************************************************************/
+
+
+extensions: .aspx .ascx
+<%--
+Copyright (c) 2011 rubicon IT GmbH
+--%>
+extensions: .vb
+'Sample license text.
+extensions: .xml .config .xsd
+
\ No newline at end of file