Commits

Christian Specht committed 9a1ca22 Draft

prettify MessageBox (replace by WPF window)

Comments (0)

Files changed (8)

src/MissileSharp.Launcher/App.xaml.cs

 
             var container = builder.Build();
 
+            // register the container
+            var builder2 = new ContainerBuilder();
+            builder2.RegisterInstance<IContainer>(container);
+            builder2.Update(container);
+
             var window = container.Resolve<MainWindow>();
             window.Show();
         }

src/MissileSharp.Launcher/MissileSharp.Launcher.csproj

       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
+    <Compile Include="ViewModels\MessageBoxWindowViewModel.cs" />
+    <Compile Include="Views\MessageBoxWindow.xaml.cs">
+      <DependentUpon>MessageBoxWindow.xaml</DependentUpon>
+    </Compile>
     <Page Include="Views\MainWindow.xaml">
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     <Compile Include="Services\IConfigService.cs" />
     <Compile Include="Services\IMessageService.cs" />
     <Compile Include="Services\IShutdownService.cs" />
+    <Compile Include="Services\IWindowService.cs" />
     <Compile Include="Services\MessageService.cs" />
     <Compile Include="Services\ShutdownService.cs" />
+    <Compile Include="Services\WindowService.cs" />
     <Compile Include="ViewModels\MainWindowViewModel.cs" />
     <Compile Include="Views\MainWindow.xaml.cs">
       <DependentUpon>MainWindow.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
+    <Page Include="Views\MessageBoxWindow.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Properties\AssemblyInfo.cs">

src/MissileSharp.Launcher/Services/IWindowService.cs

+using MahApps.Metro.Controls;
+
+namespace MissileSharp.Launcher.Services
+{
+    public interface IWindowService
+    {
+        T GetWindow<T>() where T : MetroWindow;
+    }
+}

src/MissileSharp.Launcher/Services/MessageService.cs

-using System.Windows;
+using MissileSharp.Launcher.ViewModels;
+using MissileSharp.Launcher.Views;
 
 namespace MissileSharp.Launcher.Services
 {
     /// <summary>
-    /// helper service to abstract MessageBox.Show() away
+    /// helper service to show message box
     /// </summary>
     public class MessageService : IMessageService
     {
+        private readonly IWindowService windowService;
+
+        public MessageService(IWindowService windowService)
+        {
+            this.windowService = windowService;
+        }
+
         public void ShowMessage(string messageText)
         {
-            MessageBox.Show(messageText);
+            var window = windowService.GetWindow<MessageBoxWindow>();
+            ((MessageBoxWindowViewModel)window.DataContext).MessageText = messageText;
+            window.ShowDialog();
         }
     }
 }

src/MissileSharp.Launcher/Services/WindowService.cs

+using Autofac;
+using MahApps.Metro.Controls;
+
+namespace MissileSharp.Launcher.Services
+{
+    public class WindowService : IWindowService
+    {
+        private IContainer container;
+
+        public WindowService(IContainer container)
+        {
+            this.container = container;
+        }
+
+        public T GetWindow<T>() where T : MetroWindow
+        {
+            return (T)this.container.Resolve<T>();
+        }
+    }
+}

src/MissileSharp.Launcher/ViewModels/MessageBoxWindowViewModel.cs

+using System.ComponentModel;
+
+namespace MissileSharp.Launcher.ViewModels
+{
+    public class MessageBoxWindowViewModel : INotifyPropertyChanged
+    {
+        public event PropertyChangedEventHandler PropertyChanged;
+
+        private string messageText;
+
+        public string MessageText
+        {
+            get
+            {
+                return this.messageText;
+            }
+            set
+            {
+                this.messageText = value;
+                OnPropertyChanged("MessageText");
+            }
+        }
+
+        protected void OnPropertyChanged(string name)
+        {
+            PropertyChangedEventHandler handler = PropertyChanged;
+            if (handler != null)
+            {
+                handler(this, new PropertyChangedEventArgs(name));
+            }
+        }
+    }
+}

src/MissileSharp.Launcher/Views/MessageBoxWindow.xaml

+<Controls:MetroWindow x:Class="MissileSharp.Launcher.Views.MessageBoxWindow"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
+        Title="MessageBoxWindow" Height="150" Width="300">
+    <Grid>
+        <TextBlock Margin="5" Text="{Binding MessageText}" />
+    </Grid>
+</Controls:MetroWindow>

src/MissileSharp.Launcher/Views/MessageBoxWindow.xaml.cs

+using MahApps.Metro.Controls;
+using MissileSharp.Launcher.ViewModels;
+
+namespace MissileSharp.Launcher.Views
+{
+    /// <summary>
+    /// Interaction logic for MessageBoxWindow.xaml
+    /// </summary>
+    public partial class MessageBoxWindow : MetroWindow
+    {
+        public MessageBoxWindow(MessageBoxWindowViewModel viewmodel)
+        {
+            this.DataContext = viewmodel;
+            InitializeComponent();
+        }
+    }
+}