Skip to content

Commit 90e852e

Browse files
committed
Snackbar refactoring
1 parent 6f3e840 commit 90e852e

File tree

11 files changed

+561
-577
lines changed

11 files changed

+561
-577
lines changed

MainDemo.Wpf/MainWindow.xaml

Lines changed: 173 additions & 175 deletions
Large diffs are not rendered by default.

MainDemo.Wpf/Snackbar.xaml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,45 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:MaterialDesignDemo"
7+
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
78
mc:Ignorable="d"
89
d:DesignHeight="300" d:DesignWidth="600" Background="Transparent">
10+
<UserControl.Resources>
11+
<Border x:Key="dialogContent" Margin="8">
12+
<StackPanel Orientation="Vertical">
13+
<TextBlock Text="So you clicked on the button. Did you really got it?" />
14+
<Button Content="OK" Margin="0,16,0,0" IsDefault="True" HorizontalAlignment="Right" Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Style="{DynamicResource MaterialDesignFlatButton}" />
15+
</StackPanel>
16+
</Border>
17+
</UserControl.Resources>
918
<Grid>
1019
<ScrollViewer HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Auto" VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto">
1120
<Grid>
1221
<Grid.ColumnDefinitions>
1322
<ColumnDefinition Width="250" />
1423
<ColumnDefinition Width="250" />
1524
<ColumnDefinition Width="250" />
25+
<ColumnDefinition Width="300" />
1626
</Grid.ColumnDefinitions>
1727
<Grid.RowDefinitions>
1828
<RowDefinition Height="auto" />
1929
<RowDefinition Height="16" />
2030
<RowDefinition Height="auto" />
31+
<RowDefinition Height="16" />
32+
<RowDefinition Height="100*" />
2133
</Grid.RowDefinitions>
22-
<TextBlock Margin="8,0" Text="Click the button to show a simple Snackbar." TextWrapping="WrapWithOverflow" />
34+
<TextBlock Margin="8,0" Text="Click the button to show a simple Snackbar by setting the Content property." TextWrapping="WrapWithOverflow" />
2335
<Button Grid.Row="2" Content="SHOW" Width="80" Click="ShowSimpleSnackbarButtonClickHandler" />
24-
<TextBlock Grid.Column="1" Margin="8,0" Text="This button shows a Snackbar with an action. The action will show another Snackbar after a short delay." TextWrapping="WrapWithOverflow" />
36+
<TextBlock Grid.Column="1" Margin="8,0" Text="This button shows a Snackbar with an action by setting the ActionLabel Property. The action will show another Snackbar after a short delay." TextWrapping="WrapWithOverflow" />
2537
<Button Grid.Column="1" Grid.Row="2" Content="SHOW" Width="80" Click="ShowSnackbarButtonClickHandler" />
2638
<TextBlock Grid.Column="2" Margin="8,0" Text="A click on the button shows a multiline Snackbar." TextWrapping="WrapWithOverflow" />
2739
<Button Grid.Column="2" Grid.Row="2" Content="SHOW" Width="80" Click="ShowMultilineSnackbarButtonClickHandler" />
40+
<TextBlock Grid.Column="3" Margin="8,0" Text="By setting the Mode property to Manual you have the full control over the visibility. You just need to set the IsOpen property. Have a look in the XAML." TextWrapping="WrapWithOverflow" />
41+
<ToggleButton Grid.Column="3" Grid.Row="2" x:Name="manualToggle" HorizontalAlignment="Center" />
42+
43+
<materialDesign:Snackbar Grid.ColumnSpan="3" Grid.Row="4" x:Name="snackbar" Mode="HalfAutomatic" ActionClick="SnackbarActionClickHandler" />
44+
<!-- properties will be set in the order in the xaml, so set the Mode property first for the manual mode -->
45+
<materialDesign:Snackbar Grid.Column="3" Grid.Row="4" x:Name="manualSnackbar" Mode="Manual" Content="This is a manually controlled Snackbar." IsOpen="{Binding Path=IsChecked}" />
2846
</Grid>
2947
</ScrollViewer>
3048
</Grid>

MainDemo.Wpf/Snackbar.xaml.cs

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,70 @@ namespace MaterialDesignColors.WpfExample
1919
{
2020
public partial class Snackbar : UserControl
2121
{
22+
private enum SnackbarDemoMode
23+
{
24+
Simple,
25+
ShowSecondAfterDelay,
26+
Multiline
27+
}
28+
29+
private SnackbarDemoMode? _mode;
30+
2231
public Snackbar()
2332
{
33+
_mode = null;
34+
2435
InitializeComponent();
36+
37+
manualSnackbar.DataContext = manualToggle;
38+
}
39+
40+
private void ShowSimpleSnackbarButtonClickHandler(object sender, RoutedEventArgs args)
41+
{
42+
_mode = SnackbarDemoMode.Simple;
43+
44+
snackbar.ActionLabel = null;
45+
snackbar.VisibilityTimeout = 3000;
46+
47+
// .NET caches and reuses string constants
48+
// without explicitly create a new string object, calling the setter will not raise a property changed after the second call
49+
// (reference to same string in memory)
50+
snackbar.Content = new string("This is a simple Snackbar.".ToCharArray());
2551
}
2652

27-
private async void ShowSimpleSnackbarButtonClickHandler(object sender, RoutedEventArgs args)
53+
private void ShowSnackbarButtonClickHandler(object sender, RoutedEventArgs args)
2854
{
29-
await SnackbarHost.ShowAsync("RootSnackbarHost", "This is a simple Snackbar.");
55+
_mode = SnackbarDemoMode.ShowSecondAfterDelay;
56+
57+
snackbar.ActionLabel = "HELLO";
58+
snackbar.VisibilityTimeout = 3000;
59+
snackbar.Content = new string("Hello from the Snackbar!".ToCharArray());
3060
}
3161

32-
private async void ShowSnackbarButtonClickHandler(object sender, RoutedEventArgs args)
62+
private void ShowMultilineSnackbarButtonClickHandler(object sender, RoutedEventArgs args)
3363
{
34-
await SnackbarHost.ShowAsync("RootSnackbarHost", "Hello from the Snackbar!", new SnackbarAction("HELLO", async (object s, RoutedEventArgs a) => {
35-
await Task.Delay(2000);
64+
_mode = SnackbarDemoMode.Multiline;
3665

37-
await SnackbarHost.ShowAsync("RootSnackbarHost", "A second hello from the Snackbar!", new SnackbarAction("BYE"));
38-
}));
66+
snackbar.ActionLabel = "GOT IT";
67+
snackbar.VisibilityTimeout = 6000;
68+
snackbar.Content = new string("The specs says that the maximum with should be 568dp. However there sould be at most only two lines of text.".ToCharArray());
3969
}
4070

41-
private async void ShowMultilineSnackbarButtonClickHandler(object sender, RoutedEventArgs args)
71+
private async void SnackbarActionClickHandler(object sender, RoutedEventArgs args)
4272
{
43-
await SnackbarHost.ShowAsync(
44-
"RootSnackbarHost",
45-
"The specs says that the maximum with should be 568dp. However there sould be at most only two lines of text.",
46-
new SnackbarAction("GOT IT"));
73+
if (_mode == SnackbarDemoMode.Simple || _mode == SnackbarDemoMode.Multiline)
74+
{
75+
await DialogHost.Show(Resources["dialogContent"], "RootDialog");
76+
}
77+
else if (_mode == SnackbarDemoMode.ShowSecondAfterDelay)
78+
{
79+
await Task.Delay(2000);
80+
81+
_mode = null;
82+
83+
snackbar.ActionLabel = "BYE";
84+
snackbar.Content = new string("A second hello from the Snackbar!".ToCharArray());
85+
}
4786
}
4887
}
4988
}

MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ public sealed class MathMultipleConverter : IMultiValueConverter
1515

1616
public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
1717
{
18-
double value1, value2;
19-
if (Double.TryParse(value[0].ToString(), out value1) && Double.TryParse(value[1].ToString(), out value2))
18+
if (value == null || value.Length != 2 || value[0]==null || value[1] == null)
19+
{
20+
return 0.0;
21+
}
22+
23+
double value1, value2 = 0.0;
24+
25+
if (double.TryParse(value[0].ToString(), out value1) && double.TryParse(value[1].ToString(), out value2))
2026
{
2127
switch (Operation)
2228
{

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,6 @@
292292
<Compile Include="ListBoxAssist.cs" />
293293
<Compile Include="Palette.cs" />
294294
<Compile Include="Snackbar.cs" />
295-
<Compile Include="SnackbarAction.cs" />
296-
<Compile Include="SnackbarActionEventHandler.cs" />
297-
<Compile Include="SnackbarHost.cs" />
298295
<Compile Include="Transitions\CircleWipe.cs" />
299296
<Compile Include="IHintProxy.cs" />
300297
<Compile Include="Transitions\IndexedItemOffsetMultiplierExtension.cs" />

0 commit comments

Comments
 (0)