diff --git a/AOPConsole/AOPConsole.csproj b/AOPConsole/AOPConsole.csproj new file mode 100644 index 0000000..e991b7c --- /dev/null +++ b/AOPConsole/AOPConsole.csproj @@ -0,0 +1,67 @@ + + + + + Debug + AnyCPU + {FF268F86-C30C-4FD8-8925-62C3AD8B8823} + Exe + AOPConsole + AOPConsole + v4.5 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll + + + ..\packages\Autofac.Aop.1.0.1\lib\Autofac.Aop.dll + + + ..\packages\Autofac.Extras.DynamicProxy2.3.0.2\lib\net40\Autofac.Extras.DynamicProxy2.dll + + + ..\packages\Castle.Core.4.4.0\lib\net45\Castle.Core.dll + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AOPConsole/App.config b/AOPConsole/App.config new file mode 100644 index 0000000..5ddb8cc --- /dev/null +++ b/AOPConsole/App.config @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AOPConsole/LogInterceptor.cs b/AOPConsole/LogInterceptor.cs new file mode 100644 index 0000000..0aacd05 --- /dev/null +++ b/AOPConsole/LogInterceptor.cs @@ -0,0 +1,103 @@ +/************************************************************************** +* +* ================================= +* CLR版本 :4.0.30319.42000 +* 命名空间 :AOPConsole +* 文件名称 :LoggerAspect.cs +* ================================= +* 创 建 者 :LQZ +* 创建日期 :2019-10-14 8:57:19 +* 功能描述 : +* ================================= +* 修 改 者 : +* 修改日期 : +* 修改内容 : +* ================================= +* +***************************************************************************/ +using Autofac.Extras.DynamicProxy2; +using Castle.DynamicProxy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AOPConsole +{ + class LogInterceptor : IInterceptor + { + /// + /// 拦截方法 打印被拦截的方法执行前的名称、参数和方法执行后的 返回结果 + /// + /// 包含被拦截方法的信息 + public void Intercept(IInvocation invocation) + { + Console.WriteLine("方法执行前:拦截{0}类下的方法{1}的参数是{2}", + invocation.InvocationTarget.GetType(), + invocation.Method.Name, string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())); + + //在被拦截的方法执行完毕后 继续执行 + invocation.Proceed(); + + Console.WriteLine("方法执行完毕,返回结果:{0}", invocation.ReturnValue); + Console.WriteLine(); + } + } + + /// + /// 定义一个接口 + /// + public interface IPerson + { + void Say(string Name); + } + + /// + /// 继承接口,并实现方法,给类型加上特性Attribute + /// + [Intercept(typeof(LogInterceptor))] + public class Man : IPerson + { + public string Age; + + public void Say(string Name) + { + Console.WriteLine("男人调用Say方法!姓名:" + Name + ",年龄:" + Age); + } + } + + /// + /// 继承接口,并实现方法,给类型加上特性Attribute + /// + [Intercept(typeof(LogInterceptor))] + public class Woman : IPerson + { + public void Say(string Name) + { + Console.WriteLine("女人调用Say方法!姓名:" + Name); + } + } + + /// + /// 管理类 + /// + public class PersonManager + { + IPerson _Person; + + /// + /// 根据传入的类型动态创建对象 + /// + /// + public PersonManager(IPerson Person) + { + _Person = Person; + } + + public void Say(string Name) + { + _Person.Say(Name); + } + } +} diff --git a/AOPConsole/Program.cs b/AOPConsole/Program.cs new file mode 100644 index 0000000..1e57284 --- /dev/null +++ b/AOPConsole/Program.cs @@ -0,0 +1,49 @@ +using Autofac; +using Autofac.Extras.DynamicProxy2; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AOPConsole +{ + [System.Security.SecuritySafeCritical] + class Program + { + static void Main(string[] args) + { + //启用拦截器主要有两个方法:EnableInterfaceInterceptors(),EnableClassInterceptors() + //EnableInterfaceInterceptors方法会动态创建一个接口代理 + //EnableClassInterceptors方法会创建一个目标类的子类代理类,这里需要注意的是只会拦截虚方法,重写方法 + //注意:需要引用Autofac.Extras.DynamicProxy2才能使用上面两个方法 + #region 启用接口代理拦截(推荐用这种方式) + //创建拦截容器 + var builder2 = new ContainerBuilder(); + //注册拦截器到容器 + builder2.RegisterType(); + //构造函数注入(只要调用者传入实现该接口的对象,就实现了对象创建,下面两种方式) + builder2.RegisterType(); + //方式一:给类型上加特性Attribute + //属性注入 + builder2.Register(c => new Man { Age = "20" }).As().EnableInterfaceInterceptors(); + //builder2.RegisterType().As().EnableInterfaceInterceptors(); + builder2.RegisterType().Named("Woman").EnableInterfaceInterceptors(); + //方式二:在注册类型到容器的时候动态注入拦截器(去掉类型上的特性Attribute) + //builder2.RegisterType().As().InterceptedBy(typeof(LogInterceptor)).EnableInterfaceInterceptors(); + //builder2.RegisterType().Named("Woman").InterceptedBy(typeof(LogInterceptor)).EnableInterfaceInterceptors(); + using (var container = builder2.Build()) + { + //从容器获取对象 + var Manager = container.Resolve(); + Manager.Say("管理员"); + var Person = container.Resolve(); + Person.Say("张三"); + var Woman = container.ResolveNamed("Woman"); + Woman.Say("王萌"); + } + Console.ReadLine(); + #endregion + } + } +} diff --git a/AOPConsole/Properties/AssemblyInfo.cs b/AOPConsole/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4996de3 --- /dev/null +++ b/AOPConsole/Properties/AssemblyInfo.cs @@ -0,0 +1,37 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Security; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("AOPConsole")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("AOPConsole")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("ff268f86-c30c-4fd8-8925-62c3ad8b8823")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/AOPConsole/packages.config b/AOPConsole/packages.config new file mode 100644 index 0000000..0ee079d --- /dev/null +++ b/AOPConsole/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/ToDoStylet.Model/App.config b/ToDoStylet.Model/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/ToDoStylet.Model/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ToDoStylet.Model/App.xaml b/ToDoStylet.Model/App.xaml new file mode 100644 index 0000000..2d27657 --- /dev/null +++ b/ToDoStylet.Model/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/ToDoStylet.Model/App.xaml.cs b/ToDoStylet.Model/App.xaml.cs new file mode 100644 index 0000000..4d283da --- /dev/null +++ b/ToDoStylet.Model/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace ToDoStylet.Model +{ + /// + /// App.xaml 的交互逻辑 + /// + public partial class App : Application + { + } +} diff --git a/ToDoStylet.Model/Properties/AssemblyInfo.cs b/ToDoStylet.Model/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f2b56ff --- /dev/null +++ b/ToDoStylet.Model/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("ToDoStylet.Model")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ToDoStylet.Model")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +//若要开始生成可本地化的应用程序,请设置 +//.csproj 文件中的 CultureYouAreCodingWith +//例如,如果您在源文件中使用的是美国英语, +//使用的是美国英语,请将 设置为 en-US。 然后取消 +//对以下 NeutralResourceLanguage 特性的注释。 更新 +//以下行中的“en-US”以匹配项目文件中的 UICulture 设置。 + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //主题特定资源词典所处位置 + //(未在页面中找到资源时使用, + //或应用程序资源字典中找到时使用) + ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置 + //(未在页面中找到资源时使用, + //、应用程序或任何主题专用资源字典中找到时使用) +)] + + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ToDoStylet.Model/Properties/Resources.Designer.cs b/ToDoStylet.Model/Properties/Resources.Designer.cs new file mode 100644 index 0000000..2a520aa --- /dev/null +++ b/ToDoStylet.Model/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace ToDoStylet.Model.Properties +{ + + + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ToDoStylet.Model.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 覆盖当前线程的 CurrentUICulture 属性 + /// 使用此强类型的资源类的资源查找。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/ToDoStylet.Model/Properties/Resources.resx b/ToDoStylet.Model/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/ToDoStylet.Model/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ToDoStylet.Model/Properties/Settings.Designer.cs b/ToDoStylet.Model/Properties/Settings.Designer.cs new file mode 100644 index 0000000..973538a --- /dev/null +++ b/ToDoStylet.Model/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ToDoStylet.Model.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/ToDoStylet.Model/Properties/Settings.settings b/ToDoStylet.Model/Properties/Settings.settings new file mode 100644 index 0000000..033d7a5 --- /dev/null +++ b/ToDoStylet.Model/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/ToDoStylet.Model/StudentModel.cs b/ToDoStylet.Model/StudentModel.cs new file mode 100644 index 0000000..364ed4d --- /dev/null +++ b/ToDoStylet.Model/StudentModel.cs @@ -0,0 +1,57 @@ +/************************************************************************** +* +* ================================= +* CLR版本 :4.0.30319.42000 +* 命名空间 :ToDoStylet.Model +* 文件名称 :StudentModel.cs +* ================================= +* 创 建 者 :LQZ +* 创建日期 :2019-8-14 8:42:33 +* 功能描述 : +* ================================= +* 修 改 者 : +* 修改日期 : +* 修改内容 : +* ================================= +* +***************************************************************************/ +using Stylet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ToDoStylet.Model +{ + /// + /// 后台属性 + /// + public class StudentModel: PropertyChangedBase + { + private string st_Name; + //姓名 + public string ST_Name + { + get { return this.st_Name; } + set { this.SetAndNotify(ref this.st_Name, value); } + } + + private string gender; + //性别 + public string Gender + { + get { return gender; } + set { this.SetAndNotify(ref this.gender, value); } + } + + private int age; + //年龄 + public int Age + { + get { return age; } + set { this.SetAndNotify(ref this.age,value); } + } + + } +} diff --git a/ToDoStylet.Model/ToDoStylet.Model.csproj b/ToDoStylet.Model/ToDoStylet.Model.csproj new file mode 100644 index 0000000..cde089e --- /dev/null +++ b/ToDoStylet.Model/ToDoStylet.Model.csproj @@ -0,0 +1,94 @@ + + + + + Debug + AnyCPU + {D5EE4BA2-5DE3-40ED-BB05-C918CFF34069} + WinExe + ToDoStylet.Model + ToDoStylet.Model + v4.5 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Stylet.1.2.0\lib\net45\Stylet.dll + + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + \ No newline at end of file diff --git a/ToDoStylet.Model/packages.config b/ToDoStylet.Model/packages.config new file mode 100644 index 0000000..b1373a0 --- /dev/null +++ b/ToDoStylet.Model/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/ToDoStylet.ViewModel/App.config b/ToDoStylet.ViewModel/App.config new file mode 100644 index 0000000..55ec32e --- /dev/null +++ b/ToDoStylet.ViewModel/App.config @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ToDoStylet.ViewModel/App.xaml b/ToDoStylet.ViewModel/App.xaml new file mode 100644 index 0000000..53da4db --- /dev/null +++ b/ToDoStylet.ViewModel/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/ToDoStylet.ViewModel/App.xaml.cs b/ToDoStylet.ViewModel/App.xaml.cs new file mode 100644 index 0000000..2a76421 --- /dev/null +++ b/ToDoStylet.ViewModel/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace ToDoStylet.ViewModel +{ + /// + /// App.xaml 的交互逻辑 + /// + public partial class App : Application + { + } +} diff --git a/ToDoStylet.ViewModel/FodyWeavers.xml b/ToDoStylet.ViewModel/FodyWeavers.xml new file mode 100644 index 0000000..4e68ed1 --- /dev/null +++ b/ToDoStylet.ViewModel/FodyWeavers.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ToDoStylet.ViewModel/FodyWeavers.xsd b/ToDoStylet.ViewModel/FodyWeavers.xsd new file mode 100644 index 0000000..2f1b8aa --- /dev/null +++ b/ToDoStylet.ViewModel/FodyWeavers.xsd @@ -0,0 +1,54 @@ + + + + + + + + + + + Used to control if the On_PropertyName_Changed feature is enabled. + + + + + Used to change the name of the method that fires the notify event. This is a string that accepts multiple values in a comma separated form. + + + + + Used to control if equality checks should be inserted. If false, equality checking will be disabled for the project. + + + + + Used to control if equality checks should use the Equals method resolved from the base class. + + + + + Used to control if equality checks should use the static Equals method resolved from the base class. + + + + + + + + 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. + + + + + A comma-separated list of error codes that can be safely ignored in assembly verification. + + + + + 'false' to turn off automatic generation of the XML Schema file. + + + + + \ No newline at end of file diff --git a/ToDoStylet.ViewModel/Functions.cs b/ToDoStylet.ViewModel/Functions.cs new file mode 100644 index 0000000..465e0f2 --- /dev/null +++ b/ToDoStylet.ViewModel/Functions.cs @@ -0,0 +1,82 @@ +/************************************************************************** +* +* ================================= +* CLR版本 :4.0.30319.42000 +* 命名空间 :ToDoStylet.ViewModel +* 文件名称 :GetMessages.cs +* ================================= +* 创 建 者 :LQZ +* 创建日期 :2019-10-9 9:10:19 +* 功能描述 : +* ================================= +* 修 改 者 : +* 修改日期 : +* 修改内容 : +* ================================= +* +***************************************************************************/ +using Castle.DynamicProxy; +using Stylet; +using System; +using System.Collections.Generic; +using System.Dynamic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace ToDoStylet.ViewModel +{ + //订阅事件 + public class MyEvent + { + public MyEvent() + { + MessageBox.Show("Init Event"); + } + public bool Messages() + { + MessageBox.Show("Get Message: "+DateTime.Now.ToString("HH:mm:ss.fff")); + return true; + } + } + //订阅 + public class Subscriber : IHandle + { + public Subscriber(IEventAggregator eventAggregator) + { + eventAggregator.Subscribe(this); + } + public void Handle(MyEvent message) + { + message.Messages(); + } + } + //广播 + class Publisher + { + private IEventAggregator eventAggregator; + public Publisher(IEventAggregator eventAggregator) + { + this.eventAggregator = eventAggregator; + } + + public void PublishEvent() + { + this.eventAggregator.Publish(new MyEvent().Messages()); + } + + public void PublishEventWithChannels() + { + this.eventAggregator.Publish(new MyEvent(),"ChannelA","ChannelB"); + } + } + + + public interface TestInterface + { + void Test(); + } + +} diff --git a/ToDoStylet.ViewModel/MyLogger.cs b/ToDoStylet.ViewModel/MyLogger.cs new file mode 100644 index 0000000..aa605fa --- /dev/null +++ b/ToDoStylet.ViewModel/MyLogger.cs @@ -0,0 +1,285 @@ +/************************************************************************** +* +* ================================= +* CLR版本 :4.0.30319.42000 +* 命名空间 :ToDoStylet.ViewModel +* 文件名称 :LogHelper.cs +* ================================= +* 创 建 者 :LQZ +* 创建日期 :2019-10-9 16:31:19 +* 功能描述 :日志操作类-使用KingAOP框架进行切面编程 +* ================================= +* 修 改 者 : +* 修改日期 : +* 修改内容 : +* ================================= +* +***************************************************************************/ +using Castle.DynamicProxy; +using KingAOP.Aspects; +using System; +using System.IO; +using System.Linq; +using System.Text; + +namespace ToDoStylet.ViewModel +{ + public class LoggerAspect : OnMethodBoundaryAspect, Stylet.Logging.ILogger + { + // 日志文件存放目录 + private string _logDir = AppDomain.CurrentDomain.BaseDirectory + "\\Log"; + //流程日志文件存放目录 + private string _commLogDir = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\CommLog"; + //异常日志文件存放目录 + private string _errLogDir = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\ErrLog"; + //传入的操作源名称 + private readonly string name; + public LoggerAspect(string loggerName) + { + this.DelOldFile(); + name = loggerName; + } + public LoggerAspect(){} + + //日志级别为明细 + public void Info(string format, params object[] args) + { + if (this.name == "Stylet.ViewManager" || this.name == "Stylet.WindowManager") return; + var logStr = new StringBuilder(); + logStr.AppendLine(); + logStr.Append("日志时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); + logStr.AppendLine(); + logStr.Append("日志级别:[INFO]"); + logStr.AppendLine(); + logStr.Append(string.Format("操作对象:[{0}]", this.name)); + logStr.AppendLine(); + logStr.Append(String.Format("日志内容:{0}", String.Format(format, args))); + //写到本地 + WriteLog(logStr.ToString(), _commLogDir); + } + //日志级别为警告,同明细 + public void Warn(string format, params object[] args) + { + if (this.name == "Stylet.ViewManager") return; + var logStr = new StringBuilder(); + logStr.AppendLine(); + logStr.Append("日志时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); + logStr.AppendLine(); + logStr.Append("日志级别:[WARN]"); + logStr.AppendLine(); + logStr.Append(string.Format("操作对象:[{0}]", this.name)); + logStr.AppendLine(); + logStr.Append(String.Format("日志内容: {0}", String.Format(format, args))); + //写到本地 + WriteLog(logStr.ToString(), _commLogDir); + } + //日志级别为异常 + public void Error(Exception exception, string message = null) + { + var logStr = new StringBuilder(); + logStr.AppendLine(); + logStr.Append("日志时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); + logStr.AppendLine(); + logStr.Append("日志级别:[ERROR]"); + logStr.AppendLine(); + logStr.Append(string.Format("操作对象:[{0}]", this.name)); + logStr.AppendLine(); + if (message == null) + logStr.Append(String.Format("日志内容:{0}", exception)); + else + logStr.Append(String.Format("日志内容:{0} {1}", message, exception)); + //写到本地 + WriteLog(logStr.ToString(), _errLogDir); + } + + /// + /// 删除过期文件 + /// + private void DelOldFile() + { + // 遍历指定文件夹下所有子文件,将一定期限前的日志文件删除。 + if (!Directory.Exists(this._logDir)) + { + // 如果文件夹目录不存在 + Directory.CreateDirectory(this._logDir); + } + if (!Directory.Exists(this._commLogDir)) + { + Directory.CreateDirectory(this._commLogDir); + } + if (!Directory.Exists(this._errLogDir)) + { + Directory.CreateDirectory(this._errLogDir); + } + + var vFiles = (new DirectoryInfo(this._logDir)).GetFiles(); + for (int i = vFiles.Length - 1; i >= 0; i--) + { + // 指定条件,然后删除 + if (vFiles[i].Name.Contains("Log")) + { + if ((DateTime.Now - vFiles[i].LastWriteTime).Days > 14) + { + vFiles[i].Delete(); + } + } + } + + } + #region + public override void OnEntry(MethodExecutionArgs args) + { + string logData = CreateInfoLogData("Entering", args); + //写入本地 + WriteLog(logData,_commLogDir); + } + + public override void OnException(MethodExecutionArgs args) + { + string logData = CreateErrLogData("Exception", args); + WriteLog(logData, _errLogDir); + } + + public override void OnSuccess(MethodExecutionArgs args) + { + string logData = CreateInfoLogData("Success", args); + //写入本地 + WriteLog(logData, _commLogDir); + } + + public override void OnExit(MethodExecutionArgs args) + { + string logData = CreateInfoLogData("Exiting", args); + //写入本地 + WriteLog(logData, _commLogDir); + } + + /// + /// AOP处理逻辑,日志等级为INFO + /// + /// 方法状态 + /// 方法参数 + /// + private string CreateInfoLogData(string methodStage, MethodExecutionArgs args) + { + var str = new StringBuilder(); + str.AppendLine(); + str.Append("日志时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); + str.AppendLine(); + str.Append("日志级别:[INFO]"); + str.AppendLine(); + str.Append(string.Format("操作对象:[{0}] -->> {1} ", args.Method, methodStage)); + str.AppendLine(); + str.Append("日志内容:"); + foreach (var argument in args.Arguments) + { + //下面利用反射机制获取对象名称和对象属性和属性值 + var argType = argument.GetType(); + + str.Append("对象类型:"+argType.Name+" - 对象属性:"); + + if (argType == typeof(string) || argType.IsPrimitive) + { + str.Append(argument); + } + else + { + foreach (var property in argType.GetProperties()) + { + str.AppendFormat("{0} = {1} ; ", + property.Name, property.GetValue(argument, null)); + } + } + } + return str.ToString(); + } + /// + /// AOP处理逻辑,日志等级为Error + /// + /// 方法状态 + /// 方法参数 + /// + private string CreateErrLogData(string methodStage,MethodExecutionArgs args) + { + var str = new StringBuilder(); + str.AppendLine(); + str.Append("日志时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); + str.AppendLine(); + str.Append("日志级别:[ERROR]"); + str.AppendLine(); + str.Append(string.Format("对象类型:[{0}] -->> {1} ", args.Method, methodStage)); + str.AppendLine(); + str.Append("日志内容:"); + foreach (var argument in args.Arguments) + { + //下面利用反射机制获取对象名称和对象属性和属性值 + var argType = argument.GetType(); + + str.Append("操作对象:" + argType.Name + " - 对象属性:"); + + if (argType == typeof(string) || argType.IsPrimitive) + { + str.Append(argument); + } + else + { + foreach (var property in argType.GetProperties()) + { + str.AppendFormat("{0} = {1} ;", + property.Name, property.GetValue(argument, null)); + } + } + } + return str.ToString(); + } + #endregion + + /// + /// 写入一条日志记录 + /// + /// 日志记录内容 + private void WriteLog(string pLog, string path) + { + lock (path) //排它锁:防止主程序中出现多线程同时访问同一个文件出错 + { + // 根据时间创建一个日志文件 + var vDT = DateTime.Now; + string vLogDir = string.Format("{0}\\{1}{2}{3}", path, vDT.Year, vDT.Month, vDT.Day); + //创建子目录 + if (!Directory.Exists(vLogDir)) Directory.CreateDirectory(vLogDir); + string vLogFile = string.Format("{0}\\{1}.log", vLogDir, vDT.ToString("yyyyMMddHH")); + // 创建文件流,用于写入 + using (FileStream fs = new FileStream(vLogFile, FileMode.Append)) + { + StreamWriter sw = new StreamWriter(fs); + sw.WriteLine(pLog); + sw.Flush(); + sw.Close(); + fs.Close(); + } + } + } + } + + public class LoggerHelper : IInterceptor + { + /// + /// 拦截方法 打印被拦截的方法执行前的名称、参数和方法执行后的 返回结果 + /// + /// 包含被拦截方法的信息 + public void Intercept(IInvocation invocation) + { + Console.WriteLine("方法执行前:拦截{0}类下的方法{1}的参数是{2}", + invocation.InvocationTarget.GetType(), + invocation.Method.Name, string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())); + + //在被拦截的方法执行完毕后 继续执行 + invocation.Proceed(); + + Console.WriteLine("方法执行完毕,返回结果:{0}", invocation.ReturnValue); + Console.WriteLine(); + } + } + + +} diff --git a/ToDoStylet.ViewModel/Properties/AssemblyInfo.cs b/ToDoStylet.ViewModel/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..67eef96 --- /dev/null +++ b/ToDoStylet.ViewModel/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("ToDoStylet.ViewModel")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ToDoStylet.ViewModel")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +//若要开始生成可本地化的应用程序,请设置 +//.csproj 文件中的 CultureYouAreCodingWith +//例如,如果您在源文件中使用的是美国英语, +//使用的是美国英语,请将 设置为 en-US。 然后取消 +//对以下 NeutralResourceLanguage 特性的注释。 更新 +//以下行中的“en-US”以匹配项目文件中的 UICulture 设置。 + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //主题特定资源词典所处位置 + //(未在页面中找到资源时使用, + //或应用程序资源字典中找到时使用) + ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置 + //(未在页面中找到资源时使用, + //、应用程序或任何主题专用资源字典中找到时使用) +)] + + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ToDoStylet.ViewModel/Properties/Resources.Designer.cs b/ToDoStylet.ViewModel/Properties/Resources.Designer.cs new file mode 100644 index 0000000..76cd322 --- /dev/null +++ b/ToDoStylet.ViewModel/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace ToDoStylet.ViewModel.Properties +{ + + + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ToDoStylet.ViewModel.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 覆盖当前线程的 CurrentUICulture 属性 + /// 使用此强类型的资源类的资源查找。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/ToDoStylet.ViewModel/Properties/Resources.resx b/ToDoStylet.ViewModel/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/ToDoStylet.ViewModel/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ToDoStylet.ViewModel/Properties/Settings.Designer.cs b/ToDoStylet.ViewModel/Properties/Settings.Designer.cs new file mode 100644 index 0000000..12f5885 --- /dev/null +++ b/ToDoStylet.ViewModel/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ToDoStylet.ViewModel.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/ToDoStylet.ViewModel/Properties/Settings.settings b/ToDoStylet.ViewModel/Properties/Settings.settings new file mode 100644 index 0000000..033d7a5 --- /dev/null +++ b/ToDoStylet.ViewModel/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/ToDoStylet.ViewModel/ShellViewModel.cs b/ToDoStylet.ViewModel/ShellViewModel.cs new file mode 100644 index 0000000..2c63b65 --- /dev/null +++ b/ToDoStylet.ViewModel/ShellViewModel.cs @@ -0,0 +1,98 @@ +/************************************************************************** +* +* ================================= +* CLR版本 :4.0.30319.42000 +* 命名空间 :ToDoStylet.ViewModel +* 文件名称 :ShellViewModel.cs +* ================================= +* 创 建 者 :LQZ +* 创建日期 :2019-8-14 13:10:13 +* 功能描述 : +* ================================= +* 修 改 者 : +* 修改日期 : +* 修改内容 : +* ================================= +* +***************************************************************************/ +using Stylet; +using StyletIoC; +using System; +using System.Dynamic; +using System.Threading; +using System.Windows; + +namespace ToDoStylet.ViewModel +{ + public class ShellViewModel:Screen + { + + public static IWindowManager GlobalWindowManager; + IEventAggregator eventAggregator; + public ShellViewModel(IWindowManager window,IEventAggregator aggregator) + { + GlobalWindowManager = window; + //广播 + eventAggregator = aggregator; + //窗口关闭事件 + this.Closed += ShellViewModel_Closed; + } + + private string state="Loading"; + //绑定前台显示内容 + public string NowState + { + get { return this.state; } + set { SetAndNotify(ref this.state, value); } + } + + public void OpenStudentWindow() + { + //弹出窗口 + StudentViewModel studentView = new StudentViewModel(eventAggregator); + GlobalWindowManager.ShowWindow(studentView); + //当做对话框弹出调用关闭方法 + //this.RequestClose(true); + } + //广播方法 + public void Publish() + { + //事件广播 + Publisher publisher = new Publisher(eventAggregator); + publisher.PublishEvent(); + Thread thread = new Thread(Test); + thread.IsBackground = true; + thread.Start(); + + } + + private void Test() + { + //在后台线程中调用UI线程 + Execute.OnUIThread(new Action(() => { + NowState = "OnUIThread"; + })); + Thread.Sleep(1000); + Execute.PostToUIThread(new Action(() => { + NowState = "PostToUIThread"; + })); + } + + /// + /// 绑定WPF自带的窗体事件 + /// + /// + /// + private void ShellViewModel_Closed(object sender, CloseEventArgs e) + { + MessageBox.Show("View Closed"); + + } + + public void Open(object sender,EventArgs e) + { + Console.WriteLine("View Open"); + } + + } +} diff --git a/ToDoStylet.ViewModel/StudentViewModel.cs b/ToDoStylet.ViewModel/StudentViewModel.cs new file mode 100644 index 0000000..1b56d55 --- /dev/null +++ b/ToDoStylet.ViewModel/StudentViewModel.cs @@ -0,0 +1,186 @@ +/************************************************************************** +* +* ================================= +* CLR版本 :4.0.30319.42000 +* 命名空间 :ToDoStylet.ViewModel +* 文件名称 :StudentViewModel.cs +* ================================= +* 创 建 者 :LQZ +* 创建日期 :2019-8-14 9:37:53 +* 功能描述 : +* ================================= +* 修 改 者 : +* 修改日期 : +* 修改内容 : +* ================================= +* +***************************************************************************/ +using Autofac; +using Autofac.Extras.DynamicProxy2; +using KingAOP; +using KingAOP.Aspects; +using Stylet; +using StyletIoC; +using System; +using System.Dynamic; +using System.Linq; +using System.Linq.Expressions; +using System.Windows; +using ToDoStylet.Model; +using Expression = System.Linq.Expressions.Expression; + +namespace ToDoStylet.ViewModel +{ + /// + /// 后台代码-ViewModel + /// + public class StudentViewModel : Screen + { + + /// + /// Model集合,用于绑定前台ListView + /// + public IObservableCollection studentModels { get; private set; } + //事件管理 + private IEventAggregator eventAggregator; + + private StudentModel studentModel; + /// + /// 用于显示被选中的model,绑定前台listview的被选中项 + /// + public StudentModel SeletctStudentModel + { + get { return this.studentModel; } + set { SetAndNotify(ref this.studentModel, value); } + } + + + public StudentViewModel(IEventAggregator aggregator) + { + this.DisplayName = "Student-Detail"; + this.studentModels = new BindableCollection(); + this.studentModels.Add(new StudentModel() { ST_Name = "IRON", Gender = "M", Age = 13 }); + this.studentModels.Add(new StudentModel() { ST_Name = "Stylet", Gender = "FM", Age = 3 }); + //设置选中项 + this.SeletctStudentModel = this.studentModels.FirstOrDefault(); + //订阅 + eventAggregator = aggregator; + Subscriber subscriber = new Subscriber(eventAggregator); + + this.Closed += StudentViewModel_Closed; + + + } + + private void StudentViewModel_Closed(object sender, CloseEventArgs e) + { + //throw new Exception(); + Console.WriteLine("Close StudentView"); + } + + /// + /// 绑定事件 + /// + /// + /// + public void AddStudentModel(object sender, EventArgs e) + { + //新增model + this.studentModels.Add(new StudentModel() { ST_Name = "Unnamed", Gender = "N", Age = 0 }); + //测试AOP + TestLogger log = new TestLogger(); + //kingAOP必须使用dynamic才能切入 + dynamic entity = new StudentModel { ST_Name = "Jon", Age = 99, Gender = "wang"}; + log.LoginValdate(entity); + + //测试AUTOFAC + var builder = new ContainerBuilder(); + //注册拦截器到容器 + builder.RegisterType(); + builder.RegisterType(); + builder.Register(c => new TestInter()).As().EnableInterfaceInterceptors(); + using (var container = builder.Build()) + { + var test = container.Resolve(); + test.TestIntercept(); + test.Test(); + } + } + /// + /// 传参方法 + /// + /// model参数 + public void RemoveStudent(StudentModel item) + { + //去除model + this.studentModels.Remove(item); + } + /// + /// 传参方法 + /// + /// 字符串参数 + public void ParmeterAlert(string name) + { + Console.WriteLine(name + " has been selected"); + } + + public bool CanShowMessage + { + get { return this.SeletctStudentModel.ST_Name != "Unnamed"; } + } + + + public void ShowMessage() + { + ShellViewModel shell = new ShellViewModel(ShellViewModel.GlobalWindowManager, eventAggregator); + //将view当做对话框弹出 + // this.windowManager.ShowDialog(shell); + //将view当做窗体弹出 + ShellViewModel.GlobalWindowManager.ShowWindow(shell); + + } + + } + + + public class TestLogger : IDynamicMetaObjectProvider + { + public DynamicMetaObject GetMetaObject(Expression parameter) + { + return new AspectWeaver(parameter, this); + } + + public void Test() + { + Console.Write("Test"); + } + //添加登录切面 + [LoggerAspect] + public void LoginValdate(StudentModel entity) + { + //只需进行业务逻辑处理,无需进行日志处理 + if (entity.Age == 20 && entity.Gender == "wang") + { + entity.ST_Name = "Logged"; + } + else + { + entity.ST_Name = "Error"; + } + } + } + + [Intercept(typeof(LoggerHelper))] + public class TestInter:TestInterface + { + public virtual void TestIntercept() + { + Console.WriteLine("This is a Testing"); + } + + public void Test() + { + Console.WriteLine("This is a Testing"); + } + } +} diff --git a/ToDoStylet.ViewModel/ToDoStylet.ViewModel.csproj b/ToDoStylet.ViewModel/ToDoStylet.ViewModel.csproj new file mode 100644 index 0000000..ef81200 --- /dev/null +++ b/ToDoStylet.ViewModel/ToDoStylet.ViewModel.csproj @@ -0,0 +1,139 @@ + + + + + + Debug + AnyCPU + {978A6639-9BC1-468A-9A82-D07B5BFCDCF6} + WinExe + ToDoStylet.ViewModel + ToDoStylet.ViewModel + v4.5 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll + + + ..\packages\Autofac.Aop.1.0.1\lib\Autofac.Aop.dll + + + ..\packages\Autofac.Extras.DynamicProxy2.3.0.2\lib\net40\Autofac.Extras.DynamicProxy2.dll + + + ..\packages\Castle.Core.3.2.0\lib\net45\Castle.Core.dll + + + ..\packages\KingAOP.1.0.0\lib\KingAOP.dll + + + ..\packages\PropertyChanged.Fody.2.6.1\lib\netstandard1.0\PropertyChanged.dll + + + ..\packages\Stylet.1.2.0\lib\net45\Stylet.dll + + + + + + + + + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + + + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + {d5ee4ba2-5de3-40ed-bb05-c918cff34069} + ToDoStylet.Model + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + + \ No newline at end of file diff --git a/ToDoStylet.ViewModel/packages.config b/ToDoStylet.ViewModel/packages.config new file mode 100644 index 0000000..848f144 --- /dev/null +++ b/ToDoStylet.ViewModel/packages.config @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ToDoStylet.sln b/ToDoStylet.sln new file mode 100644 index 0000000..6a7272d --- /dev/null +++ b/ToDoStylet.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.705 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToDoStylet", "ToDoStylet\ToDoStylet.csproj", "{49A09BC1-627B-485A-9000-270A5E183A8D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToDoStylet.ViewModel", "ToDoStylet.ViewModel\ToDoStylet.ViewModel.csproj", "{978A6639-9BC1-468A-9A82-D07B5BFCDCF6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToDoStylet.Model", "ToDoStylet.Model\ToDoStylet.Model.csproj", "{D5EE4BA2-5DE3-40ED-BB05-C918CFF34069}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AOPConsole", "AOPConsole\AOPConsole.csproj", "{FF268F86-C30C-4FD8-8925-62C3AD8B8823}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {49A09BC1-627B-485A-9000-270A5E183A8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {49A09BC1-627B-485A-9000-270A5E183A8D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {49A09BC1-627B-485A-9000-270A5E183A8D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {49A09BC1-627B-485A-9000-270A5E183A8D}.Release|Any CPU.Build.0 = Release|Any CPU + {978A6639-9BC1-468A-9A82-D07B5BFCDCF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {978A6639-9BC1-468A-9A82-D07B5BFCDCF6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {978A6639-9BC1-468A-9A82-D07B5BFCDCF6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {978A6639-9BC1-468A-9A82-D07B5BFCDCF6}.Release|Any CPU.Build.0 = Release|Any CPU + {D5EE4BA2-5DE3-40ED-BB05-C918CFF34069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D5EE4BA2-5DE3-40ED-BB05-C918CFF34069}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D5EE4BA2-5DE3-40ED-BB05-C918CFF34069}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D5EE4BA2-5DE3-40ED-BB05-C918CFF34069}.Release|Any CPU.Build.0 = Release|Any CPU + {FF268F86-C30C-4FD8-8925-62C3AD8B8823}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF268F86-C30C-4FD8-8925-62C3AD8B8823}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF268F86-C30C-4FD8-8925-62C3AD8B8823}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF268F86-C30C-4FD8-8925-62C3AD8B8823}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0F991FE4-BD75-4A24-BB5A-79F75F612014} + EndGlobalSection +EndGlobal diff --git a/ToDoStylet/App.config b/ToDoStylet/App.config new file mode 100644 index 0000000..55ec32e --- /dev/null +++ b/ToDoStylet/App.config @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ToDoStylet/App.xaml b/ToDoStylet/App.xaml new file mode 100644 index 0000000..bd62020 --- /dev/null +++ b/ToDoStylet/App.xaml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ToDoStylet/App.xaml.cs b/ToDoStylet/App.xaml.cs new file mode 100644 index 0000000..e9f9234 --- /dev/null +++ b/ToDoStylet/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace ToDoStylet +{ + /// + /// App.xaml 的交互逻辑 + /// + public partial class App : Application + { + } +} diff --git a/ToDoStylet/BaseViewManager.cs b/ToDoStylet/BaseViewManager.cs new file mode 100644 index 0000000..110ec16 --- /dev/null +++ b/ToDoStylet/BaseViewManager.cs @@ -0,0 +1,76 @@ +/************************************************************************** +* +* ================================= +* CLR版本 :4.0.30319.42000 +* 命名空间 :ToDoStylet +* 文件名称 :StudentViewManager.cs +* ================================= +* 创 建 者 :LQZ +* 创建日期 :2019-8-14 14:54:05 +* 功能描述 : +* ================================= +* 修 改 者 : +* 修改日期 : +* 修改内容 : +* ================================= +* +***************************************************************************/ +using Stylet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace ToDoStylet +{ + /// + /// 自定义特性,用于跨项目绑定前后台 + /// + [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] + sealed class ViewModelAttribute : Attribute + { + readonly Type viewModel; + + public ViewModelAttribute(Type viewModel) + { + this.viewModel = viewModel; + } + + public Type ViewModel + { + get { return viewModel; } + } + } + + /// + /// 绑定view与viewmodel + /// + public class BaseViewManager : ViewManager + { + // 用于存储viewmodel与view类型的字典;Dictionary of ViewModel type -> View type + private readonly Dictionary viewModelToViewMapping; + + public BaseViewManager(ViewManagerConfig config) + : base(config) + { + + var mappings = from type in this.ViewAssemblies.SelectMany(x => x.GetExportedTypes()) + let attribute = type.GetCustomAttribute() + where attribute != null && typeof(UIElement).IsAssignableFrom(type) + select new { View = type, ViewModel = attribute.ViewModel }; + + this.viewModelToViewMapping = mappings.ToDictionary(x => x.ViewModel, x => x.View); + } + //根据viewmodel定位view + protected override Type LocateViewForModel(Type modelType) + { + Type viewType; + if (!this.viewModelToViewMapping.TryGetValue(modelType, out viewType)) + throw new Exception(String.Format("Could not find View for ViewModel {0}", modelType.Name)); + return viewType; + } + } +} diff --git a/ToDoStylet/Bootstrapper.cs b/ToDoStylet/Bootstrapper.cs new file mode 100644 index 0000000..669b08c --- /dev/null +++ b/ToDoStylet/Bootstrapper.cs @@ -0,0 +1,40 @@ +using System; +using System.Reflection; +using KingAOP.Aspects; +using Stylet; +using StyletIoC; +using ToDoStylet.Pages; +using ToDoStylet.ViewModel; + +namespace ToDoStylet +{ + public class Bootstrapper : Bootstrapper + { + protected override void ConfigureIoC(IStyletIoCBuilder builder) + { + //base.ConfigureIoC(builder); + //获取所有程序集 + var ass = System.AppDomain.CurrentDomain.GetAssemblies(); + //遍历程序集,将需要的程序集注册到容器中 + foreach (Assembly assembly in ass) + { + if (assembly.FullName.Contains("ToDoStyle") ) + { + + builder.Assemblies.Add(assembly); + } + } + //builder.Bind().To().InSingletonScope(); + //注册视图管理器 + builder.Bind().To(); + } + + protected override void OnStart() + { + //启用框架自带日志 + Stylet.Logging.LogManager.LoggerFactory = name => new LoggerAspect(name); + Stylet.Logging.LogManager.Enabled = true; + + } + } +} diff --git a/ToDoStylet/FodyWeavers.xml b/ToDoStylet/FodyWeavers.xml new file mode 100644 index 0000000..4e68ed1 --- /dev/null +++ b/ToDoStylet/FodyWeavers.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ToDoStylet/FodyWeavers.xsd b/ToDoStylet/FodyWeavers.xsd new file mode 100644 index 0000000..2f1b8aa --- /dev/null +++ b/ToDoStylet/FodyWeavers.xsd @@ -0,0 +1,54 @@ + + + + + + + + + + + Used to control if the On_PropertyName_Changed feature is enabled. + + + + + Used to change the name of the method that fires the notify event. This is a string that accepts multiple values in a comma separated form. + + + + + Used to control if equality checks should be inserted. If false, equality checking will be disabled for the project. + + + + + Used to control if equality checks should use the Equals method resolved from the base class. + + + + + Used to control if equality checks should use the static Equals method resolved from the base class. + + + + + + + + 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. + + + + + A comma-separated list of error codes that can be safely ignored in assembly verification. + + + + + 'false' to turn off automatic generation of the XML Schema file. + + + + + \ No newline at end of file diff --git a/ToDoStylet/Pages/ShellView.xaml b/ToDoStylet/Pages/ShellView.xaml new file mode 100644 index 0000000..b3ac647 --- /dev/null +++ b/ToDoStylet/Pages/ShellView.xaml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/ToDoStylet/Pages/ShellView.xaml.cs b/ToDoStylet/Pages/ShellView.xaml.cs new file mode 100644 index 0000000..3e7032a --- /dev/null +++ b/ToDoStylet/Pages/ShellView.xaml.cs @@ -0,0 +1,17 @@ +using System.Windows; +using ToDoStylet.ViewModel; + +namespace ToDoStylet.Pages +{ + /// + /// MainWindow.xaml 的交互逻辑 + /// + [ViewModel(typeof(ShellViewModel))] + public partial class ShellView : Window + { + public ShellView() + { + InitializeComponent(); + } + } +} diff --git a/ToDoStylet/Pages/StudentView.xaml b/ToDoStylet/Pages/StudentView.xaml new file mode 100644 index 0000000..d429137 --- /dev/null +++ b/ToDoStylet/Pages/StudentView.xaml @@ -0,0 +1,65 @@ + + + + + + + + + + + Name: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ToDoStylet/Pages/StudentView.xaml.cs b/ToDoStylet/Pages/StudentView.xaml.cs new file mode 100644 index 0000000..3fef7a1 --- /dev/null +++ b/ToDoStylet/Pages/StudentView.xaml.cs @@ -0,0 +1,46 @@ +/************************************************************************** +* +* ================================= +* CLR版本 :4.0.30319.42000 +* 命名空间 :ToDoStylet.Pages +* 文件名称 :StudentView.cs +* ================================= +* 创 建 者 :LQZ +* 创建日期 :2019-8-14 14:32:56 +* 功能描述 : +* ================================= +* 修改者 : +* 修改日期 : +* 修改内容 : +* ================================= +* +***************************************************************************/ +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.Shapes; +using ToDoStylet.ViewModel; + +namespace ToDoStylet.Pages +{ + /// + /// StudentView.xaml 的交互逻辑 + /// + [ViewModel(typeof(StudentViewModel))] + public partial class StudentView : Window + { + public StudentView() + { + InitializeComponent(); + } + } +} diff --git a/ToDoStylet/Properties/AssemblyInfo.cs b/ToDoStylet/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d52d696 --- /dev/null +++ b/ToDoStylet/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("ToDoStylet")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ToDoStylet")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +//若要开始生成可本地化的应用程序,请设置 +//.csproj 文件中的 CultureYouAreCodingWith +//例如,如果您在源文件中使用的是美国英语, +//使用的是美国英语,请将 设置为 en-US。 然后取消 +//对以下 NeutralResourceLanguage 特性的注释。 更新 +//以下行中的“en-US”以匹配项目文件中的 UICulture 设置。 + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //主题特定资源词典所处位置 + //(未在页面中找到资源时使用, + //或应用程序资源字典中找到时使用) + ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置 + //(未在页面中找到资源时使用, + //、应用程序或任何主题专用资源字典中找到时使用) +)] + + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ToDoStylet/Properties/Resources.Designer.cs b/ToDoStylet/Properties/Resources.Designer.cs new file mode 100644 index 0000000..1301948 --- /dev/null +++ b/ToDoStylet/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace ToDoStylet.Properties +{ + + + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ToDoStylet.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 覆盖当前线程的 CurrentUICulture 属性 + /// 使用此强类型的资源类的资源查找。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/ToDoStylet/Properties/Resources.resx b/ToDoStylet/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/ToDoStylet/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ToDoStylet/Properties/Settings.Designer.cs b/ToDoStylet/Properties/Settings.Designer.cs new file mode 100644 index 0000000..4b6b62e --- /dev/null +++ b/ToDoStylet/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ToDoStylet.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/ToDoStylet/Properties/Settings.settings b/ToDoStylet/Properties/Settings.settings new file mode 100644 index 0000000..033d7a5 --- /dev/null +++ b/ToDoStylet/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/ToDoStylet/ToDoStylet.csproj b/ToDoStylet/ToDoStylet.csproj new file mode 100644 index 0000000..a56d6e0 --- /dev/null +++ b/ToDoStylet/ToDoStylet.csproj @@ -0,0 +1,164 @@ + + + + + + Debug + AnyCPU + {49A09BC1-627B-485A-9000-270A5E183A8D} + WinExe + ToDoStylet + ToDoStylet + v4.5 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\KingAOP.1.0.0\lib\KingAOP.dll + True + + + ..\packages\MaterialDesignColors.1.2.0\lib\net45\MaterialDesignColors.dll + + + ..\packages\MaterialDesignThemes.2.6.0\lib\net45\MaterialDesignThemes.Wpf.dll + + + ..\packages\Panuon.UI.1.0.0\lib\net40\Panuon.UI.dll + + + False + bin\Debug\Panuon.UI.Silver.dll + + + ..\packages\PropertyChanged.Fody.2.6.1\lib\netstandard1.0\PropertyChanged.dll + + + ..\packages\ShowMeTheXAML.1.0.12\lib\net45\ShowMeTheXAML.dll + + + ..\packages\Stylet.1.2.0\lib\net45\Stylet.dll + + + + + + + + + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + + + + + ShellView.xaml + + + StudentView.xaml + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + + + {d5ee4ba2-5de3-40ed-bb05-c918cff34069} + ToDoStylet.Model + + + {978a6639-9bc1-468a-9a82-d07b5bfcdcf6} + ToDoStylet.ViewModel + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + + + + \ No newline at end of file diff --git a/ToDoStylet/packages.config b/ToDoStylet/packages.config new file mode 100644 index 0000000..0064405 --- /dev/null +++ b/ToDoStylet/packages.config @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file