├── .gitattributes ├── .gitignore ├── MvvmExtension ├── BindingBase.cs ├── BindingExtension.cs ├── DataSourceUpdateTrigger.cs ├── MvvmExtension.csproj ├── Notification.cs ├── Properties │ └── AssemblyInfo.cs └── WinformMvvmKey.snk ├── README.md ├── Sample ├── App.config ├── Form1.Designer.cs ├── Form1.cs ├── Form1.resx ├── Model │ └── UserModel.cs ├── Program.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── Sample.csproj ├── View │ ├── MainView.Designer.cs │ ├── MainView.cs │ ├── MainView.resx │ ├── Page1View.Designer.cs │ ├── Page1View.cs │ ├── Page1View.resx │ ├── Page2View.Designer.cs │ ├── Page2View.cs │ └── Page2View.resx ├── ViewModel │ ├── MainViewModel.cs │ ├── Page1ViewModel.cs │ └── Page2ViewModel.cs └── WinformMvvmKey.snk └── WinformMvvm.sln /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc 262 | /MvvmExtension/nuget.exe 263 | /MvvmExtension/MvvmExtension.nuspec 264 | -------------------------------------------------------------------------------- /MvvmExtension/BindingBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Linq; 5 | using System.Runtime.CompilerServices; 6 | using System.Text; 7 | using System.Threading; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | 11 | namespace MvvmExtension 12 | { 13 | public abstract class BindingBase : INotifyPropertyChanged 14 | { 15 | protected void SetValue(ref T target, T value, 16 | [CallerMemberName] string propertyName = null) 17 | { 18 | if ((target == null && value != null) || (target != null && !target.Equals(value))) 19 | { 20 | target = value; 21 | if (Application.OpenForms.Count == 0) return; 22 | var form = Application.OpenForms[0]; 23 | if (form != null && form.IsHandleCreated) 24 | { 25 | form.Invoke(new Action(() => 26 | { 27 | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 28 | })); 29 | } 30 | } 31 | } 32 | public event PropertyChangedEventHandler PropertyChanged; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MvvmExtension/BindingExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows.Forms; 7 | 8 | namespace MvvmExtension 9 | { 10 | public static class BindingExtension 11 | { 12 | public static void TwoWayBinding( 13 | this Control control, 14 | string propertyName, 15 | object dataSource, 16 | string dataMember, 17 | bool formattingEnabled = false, 18 | string formatString = null, 19 | object nullValue = null, 20 | DataSourceUpdateTrigger trigger = DataSourceUpdateTrigger.OnValidation) 21 | { 22 | if (control == null 23 | || propertyName == null 24 | || dataSource == null 25 | || dataMember == null) 26 | throw new NullReferenceException(); 27 | else 28 | { 29 | Binding binding = new Binding(propertyName, dataSource, dataMember); 30 | binding.FormattingEnabled = formattingEnabled; 31 | binding.FormatString = formatString; 32 | binding.NullValue = nullValue; 33 | if (trigger.Equals(DataSourceUpdateTrigger.OnLostFocus)) 34 | { 35 | binding.DataSourceUpdateMode = DataSourceUpdateMode.Never; 36 | control.LostFocus += Control_LostFocus; 37 | } 38 | else 39 | binding.DataSourceUpdateMode = (DataSourceUpdateMode)trigger; 40 | control.DataBindings.Add(binding); 41 | } 42 | } 43 | 44 | private static void Control_LostFocus(object sender, EventArgs e) 45 | { 46 | foreach (Binding v in (sender as Control).DataBindings) 47 | { 48 | v.WriteValue(); 49 | } 50 | } 51 | 52 | public static void OneWayToSourceBinding( 53 | this Control control, 54 | string propertyName, 55 | object dataSource, 56 | string dataMember, 57 | bool formattingEnabled = false, 58 | string formatString = null, 59 | object nullValue = null, 60 | DataSourceUpdateTrigger trigger = DataSourceUpdateTrigger.OnValidation) 61 | { 62 | if (control == null 63 | || propertyName == null 64 | || dataSource == null 65 | || dataMember == null) 66 | throw new NullReferenceException(); 67 | else 68 | { 69 | Binding binding = new Binding(propertyName, dataSource, dataMember, 70 | formattingEnabled: formattingEnabled, 71 | formatString: formatString, 72 | nullValue: nullValue, 73 | dataSourceUpdateMode: DataSourceUpdateMode.OnValidation); 74 | binding.FormattingEnabled = formattingEnabled; 75 | binding.FormatString = formatString; 76 | binding.NullValue = nullValue; 77 | if (trigger.Equals(DataSourceUpdateTrigger.OnLostFocus)) 78 | { 79 | binding.DataSourceUpdateMode = DataSourceUpdateMode.Never; 80 | control.LostFocus += Control_LostFocus; 81 | } 82 | else 83 | binding.DataSourceUpdateMode = (DataSourceUpdateMode)trigger; 84 | binding.ControlUpdateMode = ControlUpdateMode.Never; 85 | control.DataBindings.Add(binding); 86 | } 87 | } 88 | public static void OneWayBinding( 89 | this Control control, 90 | string propertyName, 91 | object dataSource, 92 | string dataMember, 93 | bool formattingEnabled = false, 94 | string formatString = null, 95 | object nullValue = null) 96 | { 97 | if (control == null 98 | || propertyName == null 99 | || dataSource == null 100 | || dataMember == null) 101 | throw new NullReferenceException(); 102 | else 103 | { 104 | Binding binding = new Binding(propertyName, dataSource, dataMember, 105 | formattingEnabled: formattingEnabled, 106 | formatString: formatString, 107 | nullValue: nullValue, 108 | dataSourceUpdateMode: DataSourceUpdateMode.Never); 109 | binding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged; 110 | control.DataBindings.Add(binding); 111 | } 112 | } 113 | public static void Binding(this Control control, Binding binding) 114 | { 115 | if (control == null || binding == null) 116 | throw new NullReferenceException(); 117 | else 118 | control.DataBindings.Add(binding); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /MvvmExtension/DataSourceUpdateTrigger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MvvmExtension 8 | { 9 | public enum DataSourceUpdateTrigger 10 | { 11 | // 12 | // 摘要: 13 | // 当验证控件属性后,更新数据源 14 | OnValidation = 0, 15 | // 16 | // 摘要: 17 | // 每当控件属性的值更改时,将更新数据源。 18 | OnPropertyChanged = 1, 19 | // 20 | // 摘要: 21 | // 永远不会更新数据源并且不分析、 验证或重新格式化文本框控件中输入的值。 22 | Never = 2, 23 | // 24 | // 摘要: 25 | // 每当控件丢失焦点时,将更新数据源。 26 | OnLostFocus = 3 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MvvmExtension/MvvmExtension.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {9DBB1C08-E9AD-4246-BFA7-42C412BCE59C} 8 | Library 9 | Properties 10 | MvvmExtension 11 | WinformMvvm 12 | v4.5 13 | 512 14 | true 15 | 16 | 17 | 18 | 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | false 27 | 28 | 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | false 36 | 37 | 38 | false 39 | 40 | 41 | true 42 | 43 | 44 | WinformMvvmKey.snk 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /MvvmExtension/Notification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MvvmExtension 8 | { 9 | public delegate T Notification(E e); 10 | public delegate void Notification(E e); 11 | public delegate void Notification(); 12 | } 13 | -------------------------------------------------------------------------------- /MvvmExtension/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("WinformMvvm")] 9 | [assembly: AssemblyDescription("An Extended Library to Help Developers Develop Winform Programs with Mvvm Architecture(一个可以帮助开发人员以Mvvm结构开发Winform程序的扩展库)")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("DaiAnpeng")] 12 | [assembly: AssemblyProduct("WinformMvvm")] 13 | [assembly: AssemblyCopyright("Copyright © DaiAnpeng 2019")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("9dbb1c08-e9ad-4246-bfa7-42c412bce59c")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | //通过使用 "*",如下所示: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("2.2.0.0")] 36 | [assembly: AssemblyFileVersion("2.2.0.0")] 37 | -------------------------------------------------------------------------------- /MvvmExtension/WinformMvvmKey.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaiAnpeng/WinformMvvm/700e8ac01367f32c40274935341ab977f6e2f434/MvvmExtension/WinformMvvmKey.snk -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WinformMvvm 2 | An Extended Library to Help Developers Develop Winform applications with Mvvm Architecture(一个可以帮助开发人员以Mvvm结构开发Winform程序的扩展库) 3 | 4 | This extension has been uploaded to nuget.org.Just search "WinformMvvm" in nuget if you want to use it in your project. 5 | You can refer to the Sample in this project if you want to know how to use it. 6 | 7 | For more information:https://blog.csdn.net/dap769815768/article/details/99945560 8 | -------------------------------------------------------------------------------- /Sample/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Sample/Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace Sample 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// 必需的设计器变量。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 清理所有正在使用的资源。 12 | /// 13 | /// 如果应释放托管资源,为 true;否则为 false。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows 窗体设计器生成的代码 24 | 25 | /// 26 | /// 设计器支持所需的方法 - 不要修改 27 | /// 使用代码编辑器修改此方法的内容。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.panel1 = new System.Windows.Forms.Panel(); 32 | this.SuspendLayout(); 33 | // 34 | // panel1 35 | // 36 | this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; 37 | this.panel1.Location = new System.Drawing.Point(0, 0); 38 | this.panel1.Name = "panel1"; 39 | this.panel1.Size = new System.Drawing.Size(800, 450); 40 | this.panel1.TabIndex = 0; 41 | // 42 | // Form1 43 | // 44 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 45 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 46 | this.ClientSize = new System.Drawing.Size(800, 450); 47 | this.Controls.Add(this.panel1); 48 | this.Name = "Form1"; 49 | this.Text = "Form1"; 50 | this.Load += new System.EventHandler(this.Form1_Load); 51 | this.ResumeLayout(false); 52 | 53 | } 54 | 55 | #endregion 56 | 57 | private System.Windows.Forms.Panel panel1; 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Sample/Form1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | 11 | namespace Sample 12 | { 13 | public partial class Form1 : Form 14 | { 15 | public Form1() 16 | { 17 | InitializeComponent(); 18 | var v= this.IsHandleCreated; 19 | if (IsHandleCreated) 20 | { 21 | this.Invoke(new Action(() => 22 | { 23 | 24 | })); 25 | } 26 | this.panel1.Controls.Add(new View.MainView()); 27 | } 28 | 29 | private void Form1_Load(object sender, EventArgs e) 30 | { 31 | if (IsHandleCreated) 32 | { 33 | this.Invoke(new Action(() => 34 | { 35 | 36 | })); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Sample/Form1.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /Sample/Model/UserModel.cs: -------------------------------------------------------------------------------- 1 | using MvvmExtension; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Sample.Model 9 | { 10 | public class UserModel : BindingBase 11 | { 12 | private string id; 13 | private string name; 14 | public string Id { 15 | get => id; 16 | set => SetValue(ref id, value); 17 | } 18 | public string Name { get => name; set => SetValue(ref name, value); } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Sample/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using System.Windows.Forms; 6 | 7 | namespace Sample 8 | { 9 | static class Program 10 | { 11 | /// 12 | /// 应用程序的主入口点。 13 | /// 14 | [STAThread] 15 | static void Main() 16 | { 17 | Application.EnableVisualStyles(); 18 | Application.SetCompatibleTextRenderingDefault(false); 19 | Application.Run(new Form1()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Sample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("Sample")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("Sample")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2019")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("f74d8703-2c9a-42bb-9020-2b3d068732a6")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Sample/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本: 4.0.30319.42000 5 | // 6 | // 对此文件的更改可能导致不正确的行为,如果 7 | // 重新生成代码,则所做更改将丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace Sample.Properties 12 | { 13 | 14 | 15 | /// 16 | /// 强类型资源类,用于查找本地化字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// 返回此类使用的缓存 ResourceManager 实例。 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Sample.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// 覆盖当前线程的 CurrentUICulture 属性 56 | /// 使用此强类型的资源类的资源查找。 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Sample/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /Sample/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace Sample.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Sample/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Sample/Sample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F74D8703-2C9A-42BB-9020-2B3D068732A6} 8 | WinExe 9 | Sample 10 | Sample 11 | v4.6.1 12 | 512 13 | true 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | true 37 | 38 | 39 | WinformMvvmKey.snk 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | Form 57 | 58 | 59 | Form1.cs 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | UserControl 69 | 70 | 71 | MainView.cs 72 | 73 | 74 | UserControl 75 | 76 | 77 | Page1View.cs 78 | 79 | 80 | UserControl 81 | 82 | 83 | Page2View.cs 84 | 85 | 86 | Form1.cs 87 | 88 | 89 | ResXFileCodeGenerator 90 | Resources.Designer.cs 91 | Designer 92 | 93 | 94 | True 95 | Resources.resx 96 | 97 | 98 | MainView.cs 99 | 100 | 101 | Page1View.cs 102 | 103 | 104 | Page2View.cs 105 | 106 | 107 | SettingsSingleFileGenerator 108 | Settings.Designer.cs 109 | 110 | 111 | True 112 | Settings.settings 113 | True 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | {9dbb1c08-e9ad-4246-bfa7-42c412bce59c} 123 | MvvmExtension 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /Sample/View/MainView.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace Sample.View 2 | { 3 | partial class MainView 4 | { 5 | /// 6 | /// 必需的设计器变量。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 清理所有正在使用的资源。 12 | /// 13 | /// 如果应释放托管资源,为 true;否则为 false。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region 组件设计器生成的代码 24 | 25 | /// 26 | /// 设计器支持所需的方法 - 不要修改 27 | /// 使用代码编辑器修改此方法的内容。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.Page1 = new System.Windows.Forms.Button(); 32 | this.Page2 = new System.Windows.Forms.Button(); 33 | this.panel1 = new System.Windows.Forms.Panel(); 34 | this.SuspendLayout(); 35 | // 36 | // Page1 37 | // 38 | this.Page1.Location = new System.Drawing.Point(75, 342); 39 | this.Page1.Name = "Page1"; 40 | this.Page1.Size = new System.Drawing.Size(75, 23); 41 | this.Page1.TabIndex = 1; 42 | this.Page1.Text = "页面1"; 43 | this.Page1.UseVisualStyleBackColor = true; 44 | this.Page1.Click += new System.EventHandler(this.button1_Click); 45 | // 46 | // Page2 47 | // 48 | this.Page2.Location = new System.Drawing.Point(426, 342); 49 | this.Page2.Name = "Page2"; 50 | this.Page2.Size = new System.Drawing.Size(75, 23); 51 | this.Page2.TabIndex = 2; 52 | this.Page2.Text = "页面2"; 53 | this.Page2.UseVisualStyleBackColor = true; 54 | this.Page2.Click += new System.EventHandler(this.button1_Click); 55 | // 56 | // panel1 57 | // 58 | this.panel1.Dock = System.Windows.Forms.DockStyle.Top; 59 | this.panel1.Location = new System.Drawing.Point(0, 0); 60 | this.panel1.Name = "panel1"; 61 | this.panel1.Size = new System.Drawing.Size(603, 309); 62 | this.panel1.TabIndex = 3; 63 | // 64 | // MainView 65 | // 66 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 67 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 68 | this.Controls.Add(this.panel1); 69 | this.Controls.Add(this.Page2); 70 | this.Controls.Add(this.Page1); 71 | this.Name = "MainView"; 72 | this.Size = new System.Drawing.Size(603, 388); 73 | this.ResumeLayout(false); 74 | 75 | } 76 | 77 | #endregion 78 | private System.Windows.Forms.Button Page1; 79 | private System.Windows.Forms.Button Page2; 80 | private System.Windows.Forms.Panel panel1; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /Sample/View/MainView.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Drawing; 5 | using System.Data; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | using MvvmExtension; 11 | 12 | namespace Sample.View 13 | { 14 | public partial class MainView : UserControl 15 | { 16 | public ViewModel.MainViewModel MainVM { set; get; } = new ViewModel.MainViewModel(); 17 | public MainView() 18 | { 19 | InitializeComponent(); 20 | MainVM.User.Id = DateTime.Now.ToString(); 21 | //textBox1.Binding("Text", this.MainVM, "User.Name"); 22 | MainVM.ShowMessageBox = new Notification(this.showMessageBox); 23 | MainVM.ChangePage = new Notification(this.ChangePage); 24 | } 25 | private void ChangePage(string pageName) 26 | { 27 | this.panel1.Controls.Clear(); 28 | Control control=null; 29 | switch (pageName) 30 | { 31 | case "Page1": 32 | control = new Page1View() { Dock=DockStyle.Fill}; 33 | break; 34 | case "Page2": 35 | control = new Page2View() { Dock = DockStyle.Fill }; 36 | break; 37 | default: 38 | break; 39 | } 40 | this.panel1.Controls.Add(control); 41 | } 42 | private bool showMessageBox(string str) 43 | { 44 | DialogResult result = MessageBox.Show(str, "消息", MessageBoxButtons.OKCancel); 45 | return result == DialogResult.OK ? true : false; 46 | } 47 | private void button1_Click(object sender, EventArgs e) 48 | { 49 | MainVM.UpdateName((sender as Button).Name); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Sample/View/MainView.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /Sample/View/Page1View.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace Sample.View 2 | { 3 | partial class Page1View 4 | { 5 | /// 6 | /// 必需的设计器变量。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 清理所有正在使用的资源。 12 | /// 13 | /// 如果应释放托管资源,为 true;否则为 false。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region 组件设计器生成的代码 24 | 25 | /// 26 | /// 设计器支持所需的方法 - 不要修改 27 | /// 使用代码编辑器修改此方法的内容。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.label1 = new System.Windows.Forms.Label(); 32 | this.label2 = new System.Windows.Forms.Label(); 33 | this.label3 = new System.Windows.Forms.Label(); 34 | this.SuspendLayout(); 35 | // 36 | // label1 37 | // 38 | this.label1.Anchor = System.Windows.Forms.AnchorStyles.None; 39 | this.label1.AutoSize = true; 40 | this.label1.Location = new System.Drawing.Point(105, 126); 41 | this.label1.Name = "label1"; 42 | this.label1.Size = new System.Drawing.Size(41, 12); 43 | this.label1.TabIndex = 1; 44 | this.label1.Text = "label1"; 45 | // 46 | // label2 47 | // 48 | this.label2.AutoSize = true; 49 | this.label2.Location = new System.Drawing.Point(67, 171); 50 | this.label2.Name = "label2"; 51 | this.label2.Size = new System.Drawing.Size(35, 12); 52 | this.label2.TabIndex = 2; 53 | this.label2.Text = "Time:"; 54 | // 55 | // label3 56 | // 57 | this.label3.AutoSize = true; 58 | this.label3.Location = new System.Drawing.Point(108, 171); 59 | this.label3.Name = "label3"; 60 | this.label3.Size = new System.Drawing.Size(41, 12); 61 | this.label3.TabIndex = 3; 62 | this.label3.Text = "label3"; 63 | // 64 | // Page1View 65 | // 66 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 67 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 68 | this.Controls.Add(this.label3); 69 | this.Controls.Add(this.label2); 70 | this.Controls.Add(this.label1); 71 | this.Name = "Page1View"; 72 | this.Size = new System.Drawing.Size(333, 295); 73 | this.ResumeLayout(false); 74 | this.PerformLayout(); 75 | 76 | } 77 | 78 | #endregion 79 | 80 | private System.Windows.Forms.Label label1; 81 | private System.Windows.Forms.Label label2; 82 | private System.Windows.Forms.Label label3; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Sample/View/Page1View.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Drawing; 5 | using System.Data; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | using MvvmExtension; 11 | 12 | namespace Sample.View 13 | { 14 | public partial class Page1View : UserControl 15 | { 16 | public ViewModel.Page1ViewModel ViewModel { set; get; } = new ViewModel.Page1ViewModel(); 17 | public Page1View() 18 | { 19 | InitializeComponent(); 20 | label1.TwoWayBinding(nameof(label1.Text), ViewModel, nameof(ViewModel.PageName)); 21 | label3.OneWayBinding(nameof(label3.Text), ViewModel, nameof(ViewModel.CurrentTime),true,"yyyy-MM-dd HH:mm:ss"); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Sample/View/Page1View.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /Sample/View/Page2View.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace Sample.View 2 | { 3 | partial class Page2View 4 | { 5 | /// 6 | /// 必需的设计器变量。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 清理所有正在使用的资源。 12 | /// 13 | /// 如果应释放托管资源,为 true;否则为 false。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region 组件设计器生成的代码 24 | 25 | /// 26 | /// 设计器支持所需的方法 - 不要修改 27 | /// 使用代码编辑器修改此方法的内容。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.label1 = new System.Windows.Forms.Label(); 32 | this.SuspendLayout(); 33 | // 34 | // label1 35 | // 36 | this.label1.Anchor = System.Windows.Forms.AnchorStyles.None; 37 | this.label1.AutoSize = true; 38 | this.label1.Location = new System.Drawing.Point(98, 103); 39 | this.label1.Name = "label1"; 40 | this.label1.Size = new System.Drawing.Size(41, 12); 41 | this.label1.TabIndex = 0; 42 | this.label1.Text = "label1"; 43 | // 44 | // Page2View 45 | // 46 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 47 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 48 | this.Controls.Add(this.label1); 49 | this.Name = "Page2View"; 50 | this.Size = new System.Drawing.Size(282, 313); 51 | this.ResumeLayout(false); 52 | this.PerformLayout(); 53 | 54 | } 55 | 56 | #endregion 57 | 58 | private System.Windows.Forms.Label label1; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Sample/View/Page2View.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Drawing; 5 | using System.Data; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | using MvvmExtension; 11 | 12 | namespace Sample.View 13 | { 14 | public partial class Page2View : UserControl 15 | { 16 | public ViewModel.Page2ViewModel ViewModel { set; get; } = new ViewModel.Page2ViewModel(); 17 | public Page2View() 18 | { 19 | InitializeComponent(); 20 | label1.TwoWayBinding(nameof(label1.Text), ViewModel, nameof(ViewModel.PageName)); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Sample/View/Page2View.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /Sample/ViewModel/MainViewModel.cs: -------------------------------------------------------------------------------- 1 | using MvvmExtension; 2 | using Sample.Model; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading; 8 | using System.Threading.Tasks; 9 | 10 | namespace Sample.ViewModel 11 | { 12 | public class MainViewModel : BindingBase 13 | { 14 | public UserModel User { set; get; } = new UserModel(); 15 | public Notification ShowMessageBox { set; get; } 16 | public Notification ChangePage { set; get; } 17 | public string CurrentPage { set; get; } 18 | public void UpdateName(string pageName) 19 | { 20 | var result = ShowMessageBox("是否切换页面"); 21 | if (!result) return; 22 | ChangePage(pageName); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sample/ViewModel/Page1ViewModel.cs: -------------------------------------------------------------------------------- 1 | using MvvmExtension; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Sample.ViewModel 9 | { 10 | public class Page1ViewModel:BindingBase 11 | { 12 | private string pageName; 13 | 14 | public string PageName { get => pageName; set => SetValue(ref pageName, value); } 15 | public DateTime currentTime; 16 | public DateTime CurrentTime { get => currentTime; set => SetValue(ref currentTime, value); } 17 | public Page1ViewModel() 18 | { 19 | PageName = "页面1"; 20 | Task.Run(() => 21 | { 22 | while (true) 23 | { 24 | CurrentTime = DateTime.Now; 25 | System.Threading.Thread.Sleep(100); 26 | } 27 | }); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Sample/ViewModel/Page2ViewModel.cs: -------------------------------------------------------------------------------- 1 | using MvvmExtension; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Sample.ViewModel 9 | { 10 | public class Page2ViewModel:BindingBase 11 | { 12 | private string pageName; 13 | public string PageName { get => pageName; set => SetValue(ref pageName, value); } 14 | public Page2ViewModel() 15 | { 16 | PageName = "页面2"; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Sample/WinformMvvmKey.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaiAnpeng/WinformMvvm/700e8ac01367f32c40274935341ab977f6e2f434/Sample/WinformMvvmKey.snk -------------------------------------------------------------------------------- /WinformMvvm.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.779 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "Sample\Sample.csproj", "{F74D8703-2C9A-42BB-9020-2B3D068732A6}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvvmExtension", "MvvmExtension\MvvmExtension.csproj", "{9DBB1C08-E9AD-4246-BFA7-42C412BCE59C}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {F74D8703-2C9A-42BB-9020-2B3D068732A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {F74D8703-2C9A-42BB-9020-2B3D068732A6}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {F74D8703-2C9A-42BB-9020-2B3D068732A6}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {F74D8703-2C9A-42BB-9020-2B3D068732A6}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {9DBB1C08-E9AD-4246-BFA7-42C412BCE59C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {9DBB1C08-E9AD-4246-BFA7-42C412BCE59C}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {9DBB1C08-E9AD-4246-BFA7-42C412BCE59C}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {9DBB1C08-E9AD-4246-BFA7-42C412BCE59C}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {CD669DEE-51C5-43E0-8F10-AD172E3FF19D} 30 | EndGlobalSection 31 | EndGlobal 32 | --------------------------------------------------------------------------------