Skip to content

Commit

Permalink
Add Error icon on Snackbar error messages
Browse files Browse the repository at this point in the history
Add WpfAppDemo application
  • Loading branch information
Maxhy committed May 23, 2024
1 parent 5dbbd45 commit e772bde
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 5 deletions.
6 changes: 6 additions & 0 deletions WpfApp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfApp", "WpfApp\WpfApp.csproj", "{E29065B9-9FFC-4D05-8B0B-FB1B8EB440A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfAppDemo", "WpfAppDemo\WpfAppDemo.csproj", "{31F019D6-B05B-47CA-8D4C-F1D54B971C50}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{E29065B9-9FFC-4D05-8B0B-FB1B8EB440A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E29065B9-9FFC-4D05-8B0B-FB1B8EB440A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E29065B9-9FFC-4D05-8B0B-FB1B8EB440A4}.Release|Any CPU.Build.0 = Release|Any CPU
{31F019D6-B05B-47CA-8D4C-F1D54B971C50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31F019D6-B05B-47CA-8D4C-F1D54B971C50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31F019D6-B05B-47CA-8D4C-F1D54B971C50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{31F019D6-B05B-47CA-8D4C-F1D54B971C50}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 6 additions & 2 deletions WpfApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
FontFamily="{DynamicResource MaterialDesignFont}" Loaded="Window_Loaded">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack:https://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Snackbar.xaml" />
</ResourceDictionary.MergedDictionaries>
<materialDesign:NullableToVisibilityConverter x:Key="NullableToVisibilityConverter" />
</ResourceDictionary>
</Window.Resources>
Expand Down Expand Up @@ -170,8 +173,9 @@
</ScrollViewer>

<materialDesign:Snackbar x:Name="MainSnackbar"
Grid.Row="1"
MessageQueue="{materialDesign:MessageQueue}" />
Grid.Row="1"
ActionButtonStyle="{StaticResource MaterialDesignSnackbarActionDarkButton}"
MessageQueue="{materialDesign:MessageQueue}" />
</Grid>
</DockPanel>
</materialDesign:DrawerHost>
Expand Down
15 changes: 13 additions & 2 deletions WpfApp/SnackbarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@ public static void EnqueueError(ISnackbarMessageQueue? queue, Exception? ex, str
message = "An error occured.";
}

EnqueueMessage(queue, message);
var panel = new DockPanel();
var errorIcon = new PackIcon() { Kind = PackIconKind.ExclamationThick };
DockPanel.SetDock(errorIcon, Dock.Left);
panel.Children.Add(errorIcon);
panel.Children.Add(new TextBlock() { Text = message, Margin = new Thickness(5, 0, 0 ,0), TextWrapping = TextWrapping.Wrap });

EnqueueMessage(queue, panel);
}

public static void EnqueueMessage(ISnackbarMessageQueue? queue, PackIconKind icon, object message)
{
queue?.Enqueue(message, new PackIcon { Kind = icon }, (object? _) => { }, null, false, true, TimeSpan.FromSeconds(5));
}

public static void EnqueueMessage(ISnackbarMessageQueue? queue, object message)
{
queue?.Enqueue(message, new PackIcon { Kind = PackIconKind.CloseBold }, (object? _) => { }, null, false, true, TimeSpan.FromSeconds(5));
EnqueueMessage(queue, PackIconKind.CloseBold, message);
}

public static void HandlePreviewMouseWheel(object sender, MouseWheelEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion WpfApp/WpfApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>Leosac.$(AssemblyName)</PackageId>
<Version>1.19.0</Version>
<Version>1.20.0</Version>
<Product>Leosac WpfApp Library</Product>
<Description>C# Library components/helpers for Leosac branded Wpf applications .</Description>
<Company>Leosac SAS</Company>
Expand Down
15 changes: 15 additions & 0 deletions WpfAppDemo/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Application x:Class="WpfAppDemo.App"
xmlns="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfAppDemo"
xmlns:materialDesign="http:https://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="pack:https://application:,,,/WpfApp;component/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="Orange" SecondaryColor="Pink" />
<ResourceDictionary Source="pack:https://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
18 changes: 18 additions & 0 deletions WpfAppDemo/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Leosac.SharedServices;
using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfAppDemo
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
LeosacAppInfo.Instance = new DemoAppInfo();
}
}
}
10 changes: 10 additions & 0 deletions WpfAppDemo/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
37 changes: 37 additions & 0 deletions WpfAppDemo/DemoAppInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using CommunityToolkit.Mvvm.Input;
using Leosac.WpfApp;
using Leosac.WpfApp.Domain;
using WpfAppDemo.Domain;

namespace WpfAppDemo
{
public class DemoAppInfo : LeosacWinAppInfo
{
public DemoAppInfo()
{
ApplicationName = "Demo App";
ApplicationTitle = "Leosac Wpf Demo App";
}

public override void InitializeAboutWindow(AboutWindowViewModel model)
{

}

public override void InitializeMainWindow(MainWindowViewModel model)
{
var HomeCommand = new RelayCommand(
() =>
{
model.SelectedIndex = 0;
});

model.MenuItems.Add(new NavItem(
"Home",
typeof(HomeControl),
"House",
new HomeControlViewModel(model.SnackbarMessageQueue)
));
}
}
}
31 changes: 31 additions & 0 deletions WpfAppDemo/Domain/HomeControlViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Leosac.WpfApp;
using MaterialDesignThemes.Wpf;

namespace WpfAppDemo.Domain
{
public class HomeControlViewModel : ObservableValidator
{
private readonly ISnackbarMessageQueue _snackbarMessageQueue;

public HomeControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
{
_snackbarMessageQueue = snackbarMessageQueue;

SnackbarInfoCommand = new RelayCommand(() =>
{
SnackbarHelper.EnqueueMessage(_snackbarMessageQueue, "Simple information message.");
});

SnackbarErrorCommand = new RelayCommand(() =>
{
SnackbarHelper.EnqueueError(_snackbarMessageQueue, "An error message. This message is usually longer than other messages as some details may be included.");
});
}

public RelayCommand SnackbarInfoCommand { get; }

public RelayCommand SnackbarErrorCommand { get; }
}
}
15 changes: 15 additions & 0 deletions WpfAppDemo/HomeControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<UserControl x:Class="WpfAppDemo.HomeControl"
xmlns="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http:https://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http:https://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppDemo"
xmlns:domain="clr-namespace:WpfAppDemo.Domain"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance domain:HomeControlViewModel}"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel Margin="5">
<Button Content="Snackbar Information Message" Width="250" Command="{Binding SnackbarInfoCommand}" Margin="5" />
<Button Content="Snackbar Error Message" Width="250" Command="{Binding SnackbarErrorCommand}" Margin="5" />
</StackPanel>
</UserControl>
15 changes: 15 additions & 0 deletions WpfAppDemo/HomeControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Windows.Controls;

namespace WpfAppDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class HomeControl : UserControl
{
public HomeControl()
{
InitializeComponent();
}
}
}
15 changes: 15 additions & 0 deletions WpfAppDemo/WpfAppDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\WpfApp\WpfApp.csproj" />
</ItemGroup>

</Project>

0 comments on commit e772bde

Please sign in to comment.