@@ -64,48 +64,33 @@ public MainWindow()
64
64
65
65
LeftNavigationControl . Loaded += ( _ , _ ) =>
66
66
{
67
- LeftNavigationControlBorder . Width = LeftNavigationControl . Presenter ! . Child ? . Width ?? 0d ;
68
- AnimationHelper ? lastAnimation = null ;
69
- LeftNavigationControl . Presenter ! . PropertyChanged += async ( _ , e ) =>
67
+ LeftNavigationControlBorder . Width = LeftNavigationControl ! . Bounds . Width ;
68
+
69
+ LeftNavigationControl ! . SizeChanged += ( _ , args ) =>
70
70
{
71
- if ( e . Property != ContentPresenter . ChildProperty )
72
- return ;
73
- var oldValue = e . OldValue as Control ;
74
- var newValue = e . NewValue as Control ;
75
- lastAnimation ? . CancelAndClear ( ) ;
76
- var previousScaleTransform =
77
- ( LeftNavigationControlBorder . RenderTransform as TransformGroup ) ? . Children
78
- . FirstOrDefault ( x => x is ScaleTransform ) as ScaleTransform ;
79
- var previousScaleX = previousScaleTransform ? . ScaleX ?? 1d ;
80
- LeftNavigationControlBorder . Width = LeftNavigationControl . Presenter ! . Child ? . Width ?? 0d ;
81
- var scale = oldValue ? . Width / newValue ? . Width * previousScaleX ?? 1d ;
82
- lastAnimation = new AnimationHelper (
83
- [
84
- new ScaleTransformScaleXAnimation ( LeftNavigationControlBorder , TimeSpan . FromMilliseconds ( 300 ) , scale ,
85
- 1d , new CubicEaseOut ( ) )
86
- ] ) ;
87
- await lastAnimation . RunAsync ( ) ;
71
+ if ( args . WidthChanged )
72
+ LeftNavigationControlBorder . Width = args . NewSize . Width ;
88
73
} ;
89
74
} ;
90
75
91
76
// 获取导航控件的CompositionVisual,用于动画
92
77
_leftNavigationControlVisual = ElementComposition . GetElementVisual ( LeftNavigationControl ) ;
93
78
_rightNavigationControlVisual = ElementComposition . GetElementVisual ( RightNavigationControl ) ;
94
-
95
- if ( _leftNavigationControlVisual != null )
79
+
80
+ if ( _leftNavigationControlVisual != null )
96
81
{
97
82
_compositor = _leftNavigationControlVisual . Compositor ;
98
-
83
+
99
84
// 订阅导航事件
100
- this . Loaded += ( _ , _ ) =>
85
+ this . Loaded += ( _ , _ ) =>
101
86
{
102
87
if ( DataContext is MainWindowViewModel viewModel )
103
88
{
104
89
viewModel . NavigationService . Navigating += OnNavigating ;
105
90
}
106
91
} ;
107
-
108
- this . Unloaded += ( _ , _ ) =>
92
+
93
+ this . Unloaded += ( _ , _ ) =>
109
94
{
110
95
if ( DataContext is MainWindowViewModel viewModel )
111
96
{
@@ -117,12 +102,12 @@ public MainWindow()
117
102
GridRoot . Opacity = 0 ; // 在此处初始化透明度,不然将闪现
118
103
this . Loaded += ( _ , _ ) => AnimationIn ( ) ;
119
104
}
120
-
105
+
121
106
private void OnNavigating ( NavigationEventArgs e )
122
107
{
123
108
if ( _compositor == null || _leftNavigationControlVisual == null || _rightNavigationControlVisual == null )
124
109
return ;
125
-
110
+
126
111
// 导航动画效果
127
112
if ( e . NavigationType == NavigationType . Forward )
128
113
{
@@ -135,91 +120,91 @@ private void OnNavigating(NavigationEventArgs e)
135
120
PlayBackwardNavigationAnimation ( ) ;
136
121
}
137
122
}
138
-
123
+
139
124
private void PlayForwardNavigationAnimation ( )
140
125
{
141
126
if ( _compositor == null || _leftNavigationControlVisual == null || _rightNavigationControlVisual == null )
142
127
return ;
143
-
128
+
144
129
// 左侧面板动画 - 从左滑入
145
130
var leftOffsetAnimation = _compositor . CreateVector3KeyFrameAnimation ( ) ;
146
131
leftOffsetAnimation . Duration = TimeSpan . FromMilliseconds ( 300 ) ;
147
132
leftOffsetAnimation . InsertKeyFrame ( 0f , new Vector3 ( - 50f , 0f , 0f ) , new CubicEaseOut ( ) ) ;
148
133
leftOffsetAnimation . InsertKeyFrame ( 1f , new Vector3 ( 0f , 0f , 0f ) , new CubicEaseOut ( ) ) ;
149
134
leftOffsetAnimation . Target = "Offset" ;
150
-
135
+
151
136
var leftOpacityAnimation = _compositor . CreateScalarKeyFrameAnimation ( ) ;
152
137
leftOpacityAnimation . Duration = TimeSpan . FromMilliseconds ( 300 ) ;
153
138
leftOpacityAnimation . InsertKeyFrame ( 0f , 0f , new CubicEaseOut ( ) ) ;
154
139
leftOpacityAnimation . InsertKeyFrame ( 1f , 1f , new CubicEaseOut ( ) ) ;
155
140
leftOpacityAnimation . Target = "Opacity" ;
156
-
141
+
157
142
// 右侧面板动画 - 从右滑入
158
143
var rightOffsetAnimation = _compositor . CreateVector3KeyFrameAnimation ( ) ;
159
144
rightOffsetAnimation . Duration = TimeSpan . FromMilliseconds ( 300 ) ;
160
145
rightOffsetAnimation . InsertKeyFrame ( 0f , new Vector3 ( 50f , 0f , 0f ) , new CubicEaseOut ( ) ) ;
161
146
rightOffsetAnimation . InsertKeyFrame ( 1f , new Vector3 ( 0f , 0f , 0f ) , new CubicEaseOut ( ) ) ;
162
147
rightOffsetAnimation . Target = "Offset" ;
163
-
148
+
164
149
var rightOpacityAnimation = _compositor . CreateScalarKeyFrameAnimation ( ) ;
165
150
rightOpacityAnimation . Duration = TimeSpan . FromMilliseconds ( 300 ) ;
166
151
rightOpacityAnimation . InsertKeyFrame ( 0f , 0f , new CubicEaseOut ( ) ) ;
167
152
rightOpacityAnimation . InsertKeyFrame ( 1f , 1f , new CubicEaseOut ( ) ) ;
168
153
rightOpacityAnimation . Target = "Opacity" ;
169
-
154
+
170
155
// 应用动画
171
156
var leftAnimationGroup = _compositor . CreateAnimationGroup ( ) ;
172
157
leftAnimationGroup . Add ( leftOffsetAnimation ) ;
173
158
leftAnimationGroup . Add ( leftOpacityAnimation ) ;
174
-
159
+
175
160
var rightAnimationGroup = _compositor . CreateAnimationGroup ( ) ;
176
161
rightAnimationGroup . Add ( rightOffsetAnimation ) ;
177
162
rightAnimationGroup . Add ( rightOpacityAnimation ) ;
178
-
163
+
179
164
_leftNavigationControlVisual . StartAnimationGroup ( leftAnimationGroup ) ;
180
165
_rightNavigationControlVisual . StartAnimationGroup ( rightAnimationGroup ) ;
181
166
}
182
-
167
+
183
168
private void PlayBackwardNavigationAnimation ( )
184
169
{
185
170
if ( _compositor == null || _leftNavigationControlVisual == null || _rightNavigationControlVisual == null )
186
171
return ;
187
-
172
+
188
173
// 左侧面板动画 - 从右滑入
189
174
var leftOffsetAnimation = _compositor . CreateVector3KeyFrameAnimation ( ) ;
190
175
leftOffsetAnimation . Duration = TimeSpan . FromMilliseconds ( 300 ) ;
191
176
leftOffsetAnimation . InsertKeyFrame ( 0f , new Vector3 ( 50f , 0f , 0f ) , new CubicEaseOut ( ) ) ;
192
177
leftOffsetAnimation . InsertKeyFrame ( 1f , new Vector3 ( 0f , 0f , 0f ) , new CubicEaseOut ( ) ) ;
193
178
leftOffsetAnimation . Target = "Offset" ;
194
-
179
+
195
180
var leftOpacityAnimation = _compositor . CreateScalarKeyFrameAnimation ( ) ;
196
181
leftOpacityAnimation . Duration = TimeSpan . FromMilliseconds ( 300 ) ;
197
182
leftOpacityAnimation . InsertKeyFrame ( 0f , 0f , new CubicEaseOut ( ) ) ;
198
183
leftOpacityAnimation . InsertKeyFrame ( 1f , 1f , new CubicEaseOut ( ) ) ;
199
184
leftOpacityAnimation . Target = "Opacity" ;
200
-
185
+
201
186
// 右侧面板动画 - 从左滑入
202
187
var rightOffsetAnimation = _compositor . CreateVector3KeyFrameAnimation ( ) ;
203
188
rightOffsetAnimation . Duration = TimeSpan . FromMilliseconds ( 300 ) ;
204
189
rightOffsetAnimation . InsertKeyFrame ( 0f , new Vector3 ( - 50f , 0f , 0f ) , new CubicEaseOut ( ) ) ;
205
190
rightOffsetAnimation . InsertKeyFrame ( 1f , new Vector3 ( 0f , 0f , 0f ) , new CubicEaseOut ( ) ) ;
206
191
rightOffsetAnimation . Target = "Offset" ;
207
-
192
+
208
193
var rightOpacityAnimation = _compositor . CreateScalarKeyFrameAnimation ( ) ;
209
194
rightOpacityAnimation . Duration = TimeSpan . FromMilliseconds ( 300 ) ;
210
195
rightOpacityAnimation . InsertKeyFrame ( 0f , 0f , new CubicEaseOut ( ) ) ;
211
196
rightOpacityAnimation . InsertKeyFrame ( 1f , 1f , new CubicEaseOut ( ) ) ;
212
197
rightOpacityAnimation . Target = "Opacity" ;
213
-
198
+
214
199
// 应用动画
215
200
var leftAnimationGroup = _compositor . CreateAnimationGroup ( ) ;
216
201
leftAnimationGroup . Add ( leftOffsetAnimation ) ;
217
202
leftAnimationGroup . Add ( leftOpacityAnimation ) ;
218
-
203
+
219
204
var rightAnimationGroup = _compositor . CreateAnimationGroup ( ) ;
220
205
rightAnimationGroup . Add ( rightOffsetAnimation ) ;
221
206
rightAnimationGroup . Add ( rightOpacityAnimation ) ;
222
-
207
+
223
208
_leftNavigationControlVisual . StartAnimationGroup ( leftAnimationGroup ) ;
224
209
_rightNavigationControlVisual . StartAnimationGroup ( rightAnimationGroup ) ;
225
210
}
0 commit comments