diff --git a/DIDemoLib/DIDemoLib.csproj b/DIDemoLib/DIDemoLib.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/DIDemoLib/DIDemoLib.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/DIDemoLib/IMessages.cs b/DIDemoLib/IMessages.cs new file mode 100644 index 0000000..c15ec63 --- /dev/null +++ b/DIDemoLib/IMessages.cs @@ -0,0 +1,8 @@ +namespace DIDemoLib +{ + public interface IMessages + { + string SayGoodBye(); + string SayHello(); + } +} \ No newline at end of file diff --git a/DIDemoLib/Messages.cs b/DIDemoLib/Messages.cs new file mode 100644 index 0000000..e98af33 --- /dev/null +++ b/DIDemoLib/Messages.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DIDemoLib +{ + public class Messages : IMessages + { + public string SayHello() => "Hello Viewer"; + public string SayGoodBye() => "Goodbye, farewell and good day!"; + } +} diff --git a/WinFormDi/ContainerConfig.cs b/WinFormDi/ContainerConfig.cs new file mode 100644 index 0000000..9323517 --- /dev/null +++ b/WinFormDi/ContainerConfig.cs @@ -0,0 +1,26 @@ +using DIDemoLib; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormDi +{ + public static class ContainerConfig + { + public static IHost? Configure() + { + var builder = new HostBuilder() + .ConfigureServices((_, services) => + { + services + .AddTransient() + .AddTransient(); + }); + return builder.Build(); + } + } +} diff --git a/WinFormDi/MainWindow.Designer.cs b/WinFormDi/MainWindow.Designer.cs new file mode 100644 index 0000000..6198c60 --- /dev/null +++ b/WinFormDi/MainWindow.Designer.cs @@ -0,0 +1,75 @@ +namespace WinFormDi +{ + partial class MainWindow + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + helloText = new Label(); + goodbyeText = new Label(); + SuspendLayout(); + // + // helloText + // + helloText.AutoSize = true; + helloText.Font = new Font("Segoe UI", 20.25F, FontStyle.Regular, GraphicsUnit.Point); + helloText.Location = new Point(45, 55); + helloText.Name = "helloText"; + helloText.Size = new Size(83, 37); + helloText.TabIndex = 0; + helloText.Text = "------"; + // + // goodbyeText + // + goodbyeText.AutoSize = true; + goodbyeText.Font = new Font("Segoe UI", 20.25F, FontStyle.Regular, GraphicsUnit.Point); + goodbyeText.Location = new Point(45, 129); + goodbyeText.Name = "goodbyeText"; + goodbyeText.Size = new Size(83, 37); + goodbyeText.TabIndex = 1; + goodbyeText.Text = "------"; + // + // MainWindow + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(goodbyeText); + Controls.Add(helloText); + Name = "MainWindow"; + Text = "MainWindow"; + FormClosing += MainWindow_FormClosing; + Load += MainWindow_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label helloText; + private Label goodbyeText; + } +} \ No newline at end of file diff --git a/WinFormDi/MainWindow.cs b/WinFormDi/MainWindow.cs new file mode 100644 index 0000000..e895899 --- /dev/null +++ b/WinFormDi/MainWindow.cs @@ -0,0 +1,35 @@ +using DIDemoLib; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace WinFormDi +{ + public partial class MainWindow : Form + { + private readonly IMessages _messages; + + public MainWindow(IMessages messages) + { + InitializeComponent(); + _messages = messages; + } + + private void MainWindow_Load(object sender, EventArgs e) + { + helloText.Text= _messages.SayHello(); + } + + private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) + { + goodbyeText.Text= _messages.SayGoodBye(); + MessageBox.Show("Closing...", "Warning", MessageBoxButtons.OK); + } + } +} diff --git a/WinFormDi/MainWindow.resx b/WinFormDi/MainWindow.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/WinFormDi/MainWindow.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/WinFormDi/Program.cs b/WinFormDi/Program.cs new file mode 100644 index 0000000..c9ba154 --- /dev/null +++ b/WinFormDi/Program.cs @@ -0,0 +1,36 @@ +using DIDemoLib; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace WinFormDi +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + var host = ContainerConfig.Configure(); + using var scope = host.Services.CreateScope(); + + try + { + var services = scope.ServiceProvider; + + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.SetHighDpiMode(HighDpiMode.SystemAware); + + var frm = services.GetRequiredService(); + Application.Run(frm); + } + catch (Exception ex) + { + Console.WriteLine($"An error has occured: {ex.Message}"); + } + + } + } +} \ No newline at end of file diff --git a/WinFormDi/WinFormDi.csproj b/WinFormDi/WinFormDi.csproj new file mode 100644 index 0000000..a395e17 --- /dev/null +++ b/WinFormDi/WinFormDi.csproj @@ -0,0 +1,20 @@ + + + + WinExe + net7.0-windows + enable + true + enable + + + + + + + + + + + + \ No newline at end of file diff --git a/WinFormDiApp.sln b/WinFormDiApp.sln new file mode 100644 index 0000000..a2e9994 --- /dev/null +++ b/WinFormDiApp.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33829.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormDi", "WinFormDi\WinFormDi.csproj", "{940A0A13-2195-47DD-9FDE-16A0C57AF3BF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DIDemoLib", "DIDemoLib\DIDemoLib.csproj", "{839E6DE6-4644-4279-A7B1-449BD63897A3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {940A0A13-2195-47DD-9FDE-16A0C57AF3BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {940A0A13-2195-47DD-9FDE-16A0C57AF3BF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {940A0A13-2195-47DD-9FDE-16A0C57AF3BF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {940A0A13-2195-47DD-9FDE-16A0C57AF3BF}.Release|Any CPU.Build.0 = Release|Any CPU + {839E6DE6-4644-4279-A7B1-449BD63897A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {839E6DE6-4644-4279-A7B1-449BD63897A3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {839E6DE6-4644-4279-A7B1-449BD63897A3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {839E6DE6-4644-4279-A7B1-449BD63897A3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E1B6FC55-3F9C-40C2-975E-3D08DF8A32AA} + EndGlobalSection +EndGlobal