@@ -52,6 +52,35 @@ public object Message
52
52
53
53
public SnackbarActionEventHandler ActionHandler { get ; internal set ; }
54
54
55
+ private SnackbarState _state ;
56
+
57
+ private SnackbarState State
58
+ {
59
+ get
60
+ {
61
+ return _state ;
62
+ }
63
+
64
+ set
65
+ {
66
+ _state = value ;
67
+
68
+ // set the according visual state to trigger the animations
69
+ if ( _state == SnackbarState . Initialized )
70
+ {
71
+ VisualStateManager . GoToState ( this , HiddenStateName , false ) ;
72
+ }
73
+ else if ( _state == SnackbarState . Visible )
74
+ {
75
+ VisualStateManager . GoToState ( this , VisibleStateName , true ) ;
76
+ }
77
+ else if ( _state == SnackbarState . Hidden )
78
+ {
79
+ VisualStateManager . GoToState ( this , HiddenStateName , true ) ;
80
+ }
81
+ }
82
+ }
83
+
55
84
private DispatcherTimer _timer ;
56
85
57
86
static Snackbar ( )
@@ -66,15 +95,21 @@ public override void OnApplyTemplate()
66
95
Button actionButton = ( Button ) GetTemplateChild ( PartActionButtonName ) ;
67
96
actionButton . Click += ActionButtonClickHandler ;
68
97
69
- VisualStateManager . GoToState ( this , HiddenStateName , false ) ;
98
+ State = SnackbarState . Initialized ;
70
99
71
100
base . OnApplyTemplate ( ) ;
72
101
}
73
102
74
103
public async Task Show ( )
75
104
{
76
- // trigger animation and wait for it
77
- VisualStateManager . GoToState ( this , VisibleStateName , true ) ;
105
+ if ( State != SnackbarState . Initialized )
106
+ {
107
+ // only a fresh initialized Snackbar can be shown
108
+ return ;
109
+ }
110
+
111
+ // set the state, trigger the animation and wait for it
112
+ State = SnackbarState . Visible ;
78
113
79
114
await Task . Delay ( 300 ) ;
80
115
@@ -90,11 +125,17 @@ public async Task Show()
90
125
91
126
public async Task Hide ( )
92
127
{
128
+ if ( State != SnackbarState . Visible )
129
+ {
130
+ // only a visible Snackbar can be hidden
131
+ return ;
132
+ }
133
+
93
134
// stop the timer
94
135
_timer . Stop ( ) ;
95
136
96
- // trigger animation and wait for it
97
- VisualStateManager . GoToState ( this , HiddenStateName , true ) ;
137
+ // set the state, trigger the animation and wait for it
138
+ State = SnackbarState . Hidden ;
98
139
99
140
await Task . Delay ( 300 ) ;
100
141
@@ -126,5 +167,12 @@ private SnackbarHost FindSnackbarHost()
126
167
127
168
return null ;
128
169
}
170
+
171
+ private enum SnackbarState : byte
172
+ {
173
+ Initialized ,
174
+ Visible ,
175
+ Hidden
176
+ }
129
177
}
130
178
}
0 commit comments