| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.ComponentModel; |
| 4 | using System.Drawing; |
| 5 | using System.Linq; |
| 6 | using System.Runtime.InteropServices; |
| 7 | using System.Text; |
| 8 | using System.Threading; |
| 9 | using System.Threading.Tasks; |
| 10 | using System.Windows; |
| 11 | using System.Windows.Controls; |
| 12 | using System.Windows.Data; |
| 13 | using System.Windows.Input; |
| 14 | using System.Windows.Interop; |
| 15 | using System.Windows.Media; |
| 16 | using System.Windows.Media.Animation; |
| 17 | using System.Windows.Media.Imaging; |
| 18 | using System.Windows.Threading; |
| 19 | using UI.Controls.Button; |
| 20 | using UI.Controls.Input; |
| 21 | |
| 22 | namespace UI.Controls.Window |
| 23 | { |
| 24 | public class DefaultWindow : System.Windows.Window |
| 25 | { |
| 26 | #region 1.依赖属性 |
| 27 | #region Dialog |
| 28 | public static readonly DependencyProperty DialogTitleProperty = DependencyProperty.Register("DialogTitle", typeof(string), typeof(DefaultWindow)); |
| 29 | ///<summary> |
| 30 | /// |
| 31 | ///</summary> |
| 32 | public string DialogTitle |
| 33 | { |
| 34 | get { return (string)GetValue(DialogTitleProperty); } |
| 35 | set { SetValue(DialogTitleProperty, value); } |
| 36 | } |
| 37 | |
| 38 | public static readonly DependencyProperty DialogMessageProperty = DependencyProperty.Register("DialogMessage", typeof(string), typeof(DefaultWindow)); |
| 39 | ///<summary> |
| 40 | /// |
| 41 | ///</summary> |
| 42 | public string DialogMessage |
| 43 | { |
| 44 | get { return (string)GetValue(DialogMessageProperty); } |
| 45 | set { SetValue(DialogMessageProperty, value); } |
| 46 | } |
| 47 | #endregion |
| 48 | #region InputModal |
| 49 | public static readonly DependencyProperty InputModalValueProperty = DependencyProperty.Register("InputModalValue", typeof(string), typeof(DefaultWindow)); |
| 50 | ///<summary> |
| 51 | /// |
| 52 | ///</summary> |
| 53 | public string InputModalValue |
| 54 | { |
| 55 | get { return (string)GetValue(InputModalValueProperty); } |
| 56 | set { SetValue(InputModalValueProperty, value); } |
| 57 | } |
| 58 | #endregion |
| 59 | public static readonly DependencyProperty IsShowToastDeproperty = DependencyProperty.Register("IsShowToast", typeof(bool), typeof(DefaultWindow), new PropertyMetadata(false, new PropertyChangedCallback(OnIsShowToastChanged))); |
| 60 | public bool IsShowToast { get { return (bool)GetValue(IsShowToastDeproperty); } set { SetValue(IsShowToastDeproperty, value); } } |
| 61 | private static void OnIsShowToastChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 62 | { |
| 63 | var that = d as DefaultWindow; |
| 64 | if (that != null) |
| 65 | { |
| 66 | if (that.IsShowToast) |
| 67 | { |
| 68 | that.ShowToast(); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | that.HideToast(); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public static readonly DependencyProperty ToastIconProperty = DependencyProperty.Register("ToastIcon", typeof(Base.IconTypes), typeof(DefaultWindow)); |
| 78 | public Base.IconTypes ToastIcon |
| 79 | { |
| 80 | get |
| 81 | { |
| 82 | return (Base.IconTypes)GetValue(ToastIconProperty); |
| 83 | } |
| 84 | set { SetValue(ToastIconProperty, value); } |
| 85 | } |
| 86 | public static readonly DependencyProperty ToastContentProperty = DependencyProperty.Register("ToastContent", typeof(string), typeof(DefaultWindow)); |
| 87 | ///<summary> |
| 88 | /// |
| 89 | ///</summary> |
| 90 | public string ToastContent |
| 91 | { |
| 92 | get { return (string)GetValue(ToastContentProperty); } |
| 93 | set { SetValue(ToastContentProperty, value); } |
| 94 | } |
| 95 | public static readonly DependencyProperty ToastTypeProperty = DependencyProperty.Register("ToastType", typeof(ToastType), typeof(DefaultWindow), new PropertyMetadata(ToastType.Info)); |
| 96 | ///<summary> |
| 97 | /// |
| 98 | ///</summary> |
| 99 | public ToastType ToastType |
| 100 | { |
| 101 | get { return (ToastType)GetValue(ToastTypeProperty); } |
| 102 | set { SetValue(ToastTypeProperty, value); } |
| 103 | } |
| 104 | public static readonly DependencyProperty PageContainerProperty = DependencyProperty.Register("PageContainer", typeof(PageContainer), typeof(DefaultWindow), new PropertyMetadata(null, new PropertyChangedCallback(OnPageContainerChanged))); |
| 105 | public PageContainer PageContainer { get { return (PageContainer)GetValue(PageContainerProperty); } set { SetValue(PageContainerProperty, value); } } |
| 106 | #region 最小化按钮显示状态 |
| 107 | |
| 108 | public static readonly DependencyProperty MinimizeVisibilityProperty = DependencyProperty.Register("MinimizeVisibility", typeof(Visibility), typeof(DefaultWindow)); |
| 109 | |
| 110 | private static void OnPageContainerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 111 | { |
| 112 | var that = (DefaultWindow)d; |
| 113 | if (that != null) |
| 114 | { |
| 115 | if (e.NewValue != null) |
| 116 | { |
| 117 | that.IsCanBack = that.PageContainer.Index >= 1; |
| 118 | |
| 119 | that.PageContainer.OnLoadPaged += (s, v) => |
| 120 | { |
| 121 | var pc = s as PageContainer; |
| 122 | that.IsCanBack = pc?.Index >= 1; |
| 123 | }; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | ///<summary> |
| 129 | /// |
| 130 | ///</summary> |
| 131 | public Visibility MinimizeVisibility |
| 132 | { |
| 133 | get { return (Visibility)GetValue(MinimizeVisibilityProperty); } |
| 134 | set { SetValue(MinimizeVisibilityProperty, value); } |
| 135 | } |
| 136 | #endregion |
| 137 | |
| 138 | #region 最大化按钮显示状态 |
| 139 | |
| 140 | public static readonly DependencyProperty MaximizeVisibilityProperty = DependencyProperty.Register("MaximizeVisibility", typeof(Visibility), typeof(DefaultWindow)); |
| 141 | |
| 142 | ///<summary> |
| 143 | /// |
| 144 | ///</summary> |
| 145 | public Visibility MaximizeVisibility |
| 146 | { |
| 147 | get { return (Visibility)GetValue(MaximizeVisibilityProperty); } |
| 148 | set { SetValue(MaximizeVisibilityProperty, value); } |
| 149 | } |
| 150 | #endregion |
| 151 | |
| 152 | #region 关闭按钮显示状态 |
| 153 | |
| 154 | public static readonly DependencyProperty CloseVisibilityProperty = DependencyProperty.Register("CloseVisibility", typeof(Visibility), typeof(DefaultWindow)); |
| 155 | |
| 156 | ///<summary> |
| 157 | /// |
| 158 | ///</summary> |
| 159 | public Visibility CloseVisibility |
| 160 | { |
| 161 | get { return (Visibility)GetValue(CloseVisibilityProperty); } |
| 162 | set { SetValue(CloseVisibilityProperty, value); } |
| 163 | } |
| 164 | #endregion |
| 165 | |
| 166 | #region 拓展内容 |
| 167 | public static readonly DependencyProperty ExtElementProperty = DependencyProperty.Register("ExtElement", typeof(object), typeof(DefaultWindow)); |
| 168 | ///<summary> |
| 169 | /// |
| 170 | ///</summary> |
| 171 | public object ExtElement |
| 172 | { |
| 173 | get { return GetValue(ExtElementProperty); } |
| 174 | set { SetValue(ExtElementProperty, value); } |
| 175 | } |
| 176 | #endregion |
| 177 | |
| 178 | |
| 179 | |
| 180 | #region 是否穿透窗口 |
| 181 | |
| 182 | public static readonly DependencyProperty IsThruWindowProperty = DependencyProperty.Register("IsThruWindow", typeof(bool), typeof(DefaultWindow), new PropertyMetadata(false)); |
| 183 | |
| 184 | ///<summary> |
| 185 | /// |
| 186 | ///</summary> |
| 187 | public bool IsThruWindow |
| 188 | { |
| 189 | get { return (bool)GetValue(IsThruWindowProperty); } |
| 190 | set { SetValue(IsThruWindowProperty, value); } |
| 191 | } |
| 192 | #endregion |
| 193 | |
| 194 | |
| 195 | public static readonly DependencyProperty IsCanBackProperty = DependencyProperty.Register("IsCanBack", typeof(bool), typeof(DefaultWindow), new PropertyMetadata(false, new PropertyChangedCallback(OnIsCanBackChanged))); |
| 196 | |
| 197 | private static void OnIsCanBackChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 198 | { |
| 199 | var that = d as DefaultWindow; |
| 200 | if (that != null) |
| 201 | { |
| 202 | if (that.IsCanBack) |
| 203 | { |
| 204 | VisualStateManager.GoToState(that, "CanBackState", true); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | VisualStateManager.GoToState(that, "Normal", true); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | ///<summary> |
| 214 | /// |
| 215 | ///</summary> |
| 216 | public bool IsCanBack { get { return (bool)GetValue(IsCanBackProperty); } set { SetValue(IsCanBackProperty, value); } } |
| 217 | #endregion |
| 218 | |
| 219 | private bool IsWindowClosed_ = false; |
| 220 | ///<summary> |
| 221 | /// |
| 222 | ///</summary> |
| 223 | public bool IsWindowClosed { get { return IsWindowClosed_; } } |
| 224 | |
| 225 | private Border ToastBorder, Masklayer, DialogBorder, InputModalBorder; |
| 226 | private Grid ToastGrid; |
| 227 | private DispatcherTimer toastTimer; |
| 228 | private bool IsDialogConfirm; |
| 229 | private bool IsShowConfirmDialog, IsShowInputModal; |
| 230 | private Button.Button CancelBtn, ConfirmBtn, InputModalCancelBtn, InputModalConfirmBtn; |
| 231 | private InputBox InputModalInputBox; |
| 232 | private string InputValue; |
| 233 | private Func<string, bool> InputModalValidFnc; |
| 234 | #region 3.初始化 |
| 235 | public DefaultWindow() |
| 236 | { |
| 237 | this.DefaultStyleKey = typeof(DefaultWindow); |
| 238 | |
| 239 | |
| 240 | |
| 241 | this.CommandBindings.Add(new CommandBinding(DefaultWindowCommands.MinimizeWindowCommand, OnMinimizeWindowCommand)); |
| 242 | this.CommandBindings.Add(new CommandBinding(DefaultWindowCommands.MaximizeWindowCommand, OnMaximizeWindowCommand)); |
| 243 | this.CommandBindings.Add(new CommandBinding(DefaultWindowCommands.RestoreWindowCommand, OnRestoreWindowCommand)); |
| 244 | this.CommandBindings.Add(new CommandBinding(DefaultWindowCommands.CloseWindowCommand, OnCloseWindowCommand)); |
| 245 | this.CommandBindings.Add(new CommandBinding(DefaultWindowCommands.BackCommand, OnBackCommand)); |
| 246 | |
| 247 | |
| 248 | if (Icon == null) |
| 249 | { |
| 250 | string exeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location; |
| 251 | Icon = ToImageSource(System.Drawing.Icon.ExtractAssociatedIcon(exeFilePath)); |
| 252 | } |
| 253 | |
| 254 | Loaded += window_Loaded; |
| 255 | Unloaded += DefaultWindow_Unloaded; |
| 256 | |
| 257 | toastTimer = new DispatcherTimer(); |
| 258 | toastTimer.Tick += (s, e) => |
| 259 | { |
| 260 | toastTimer.Stop(); |
| 261 | IsShowToast = false; |
| 262 | }; |
| 263 | toastTimer.Interval = new TimeSpan(0, 0, 3); |
| 264 | } |
| 265 | |
| 266 | private void DefaultWindow_Unloaded(object sender, RoutedEventArgs e) |
| 267 | { |
| 268 | Unloaded -= DefaultWindow_Unloaded; |
| 269 | Loaded -= window_Loaded; |
| 270 | |
| 271 | } |
| 272 | |
| 273 | private void OnBackCommand(object sender, ExecutedRoutedEventArgs e) |
| 274 | { |
| 275 | if (PageContainer != null) |
| 276 | { |
| 277 | PageContainer.Back(); |
| 278 | if (PageContainer.Index == 0) |
| 279 | { |
| 280 | IsCanBack = false; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | [DllImport("gdi32.dll", SetLastError = true)] |
| 286 | private static extern bool DeleteObject(IntPtr hObject); |
| 287 | |
| 288 | public static ImageSource ToImageSource(Icon icon) |
| 289 | { |
| 290 | Bitmap bitmap = icon.ToBitmap(); |
| 291 | IntPtr hBitmap = bitmap.GetHbitmap(); |
| 292 | |
| 293 | ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap( |
| 294 | hBitmap, |
| 295 | IntPtr.Zero, |
| 296 | Int32Rect.Empty, |
| 297 | BitmapSizeOptions.FromEmptyOptions()); |
| 298 | |
| 299 | if (!DeleteObject(hBitmap)) |
| 300 | { |
| 301 | throw new Win32Exception(); |
| 302 | } |
| 303 | |
| 304 | return wpfBitmap; |
| 305 | } |
| 306 | |
| 307 | private void window_Loaded(object sender, RoutedEventArgs e) |
| 308 | { |
| 309 | OnSystemButtonsVisibility(); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | public override void OnApplyTemplate() |
| 316 | { |
| 317 | base.OnApplyTemplate(); |
| 318 | |
| 319 | ToastBorder = GetTemplateChild("ToastBorder") as Border; |
| 320 | Masklayer = GetTemplateChild("Masklayer") as Border; |
| 321 | ToastGrid = GetTemplateChild("ToastGrid") as Grid; |
| 322 | DialogBorder = GetTemplateChild("DialogBorder") as Border; |
| 323 | CancelBtn = GetTemplateChild("CancelBtn") as Button.Button; |
| 324 | ConfirmBtn = GetTemplateChild("ConfirmBtn") as Button.Button; |
| 325 | InputModalBorder = GetTemplateChild("InputModalBorder") as Border; |
| 326 | InputModalCancelBtn = GetTemplateChild("InputModalCancelBtn") as Button.Button; |
| 327 | InputModalConfirmBtn = GetTemplateChild("InputModalConfirmBtn") as Button.Button; |
| 328 | InputModalInputBox = GetTemplateChild("InputModalInputBox") as InputBox; |
| 329 | |
| 330 | if (PageContainer != null) |
| 331 | { |
| 332 | IsCanBack = PageContainer.Index >= 1; |
| 333 | if (IsCanBack) |
| 334 | { |
| 335 | VisualStateManager.GoToState(this, "CanBackState", true); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | VisualStateManager.GoToState(this, "Normal", true); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (CancelBtn != null) |
| 344 | { |
| 345 | CancelBtn.Click += (e, c) => |
| 346 | { |
| 347 | IsDialogConfirm = false; |
| 348 | HideDialog(); |
| 349 | }; |
| 350 | } |
| 351 | |
| 352 | if (ConfirmBtn != null) |
| 353 | { |
| 354 | ConfirmBtn.Click += (e, c) => |
| 355 | { |
| 356 | IsDialogConfirm = true; |
| 357 | HideDialog(); |
| 358 | }; |
| 359 | } |
| 360 | |
| 361 | if (InputModalCancelBtn != null) |
| 362 | { |
| 363 | InputModalCancelBtn.Click += (e, c) => |
| 364 | { |
| 365 | IsDialogConfirm = false; |
| 366 | HideInputModal(); |
| 367 | }; |
| 368 | } |
| 369 | |
| 370 | if (InputModalConfirmBtn != null) |
| 371 | { |
| 372 | InputModalConfirmBtn.Click += (e, c) => |
| 373 | { |
| 374 | if (InputModalValidFnc != null && !InputModalValidFnc(InputValue)) |
| 375 | { |
| 376 | return; |
| 377 | } |
| 378 | IsDialogConfirm = true; |
| 379 | HideInputModal(); |
| 380 | }; |
| 381 | } |
| 382 | |
| 383 | if (InputModalInputBox != null) |
| 384 | { |
| 385 | InputModalInputBox.TextChanged += (e, c) => |
| 386 | { |
| 387 | InputValue = InputModalInputBox.Text; |
| 388 | }; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | |
| 393 | ///<summary> |
| 394 | /// |
| 395 | ///</summary> |
| 396 | private void OnSystemButtonsVisibility() |
| 397 | { |
| 398 | switch (ResizeMode) |
| 399 | { |
| 400 | case ResizeMode.NoResize: |
| 401 | |
| 402 | MinimizeVisibility = Visibility.Collapsed; |
| 403 | MaximizeVisibility = Visibility.Collapsed; |
| 404 | CloseVisibility = Visibility.Visible; |
| 405 | break; |
| 406 | case ResizeMode.CanResize: |
| 407 | MinimizeVisibility = Visibility.Visible; |
| 408 | MaximizeVisibility = Visibility.Visible; |
| 409 | CloseVisibility = Visibility.Visible; |
| 410 | break; |
| 411 | case ResizeMode.CanMinimize: |
| 412 | MinimizeVisibility = Visibility.Visible; |
| 413 | MaximizeVisibility = Visibility.Collapsed; |
| 414 | CloseVisibility = Visibility.Visible; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | #endregion |
| 419 | |
| 420 | protected override void OnClosed(EventArgs e) |
| 421 | { |
| 422 | base.OnClosed(e); |
| 423 | IsWindowClosed_ = true; |
| 424 | } |
| 425 | #region 5.命令 |
| 426 | |
| 427 | private void OnCloseWindowCommand(object sender, ExecutedRoutedEventArgs e) |
| 428 | { |
| 429 | Close(); |
| 430 | } |
| 431 | |
| 432 | private void OnRestoreWindowCommand(object sender, ExecutedRoutedEventArgs e) |
| 433 | { |
| 434 | WindowState = WindowState.Normal; |
| 435 | } |
| 436 | |
| 437 | private void OnMaximizeWindowCommand(object sender, ExecutedRoutedEventArgs e) |
| 438 | { |
| 439 | WindowState = WindowState.Maximized; |
| 440 | } |
| 441 | |
| 442 | private void OnMinimizeWindowCommand(object sender, ExecutedRoutedEventArgs e) |
| 443 | { |
| 444 | WindowState = WindowState.Minimized; |
| 445 | } |
| 446 | |
| 447 | #endregion |
| 448 | |
| 449 | private void ShowToast() |
| 450 | { |
| 451 | ToastGrid.Visibility = Visibility.Visible; |
| 452 | DialogBorder.Visibility = Visibility.Collapsed; |
| 453 | if (!IsShowInputModal) |
| 454 | { |
| 455 | InputModalBorder.Visibility = Visibility.Collapsed; |
| 456 | } |
| 457 | ToastBorder.Visibility = Visibility.Visible; |
| 458 | Storyboard storyboard = new Storyboard(); |
| 459 | |
| 460 | DoubleAnimation scrollAnimation = new DoubleAnimation(); |
| 461 | scrollAnimation.From = -150; |
| 462 | scrollAnimation.To = 0; |
| 463 | |
| 464 | scrollAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 465 | Storyboard.SetTarget(scrollAnimation, ToastBorder); |
| 466 | Storyboard.SetTargetProperty(scrollAnimation, new PropertyPath("RenderTransform.Children[0].Y")); |
| 467 | scrollAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.35)); |
| 468 | |
| 469 | DoubleAnimation opacityAnimation = new DoubleAnimation(); |
| 470 | opacityAnimation.To = 0.6; |
| 471 | opacityAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 472 | Storyboard.SetTarget(opacityAnimation, Masklayer); |
| 473 | Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); |
| 474 | opacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.25)); |
| 475 | storyboard.Completed += (e, c) => |
| 476 | { |
| 477 | toastTimer.Start(); |
| 478 | ToastGrid.MouseLeftButtonDown += ToastGrid_MouseLeftButtonDown; |
| 479 | }; |
| 480 | storyboard.Children.Add(scrollAnimation); |
| 481 | if (!IsShowInputModal) |
| 482 | { |
| 483 | storyboard.Children.Add(opacityAnimation); |
| 484 | } |
| 485 | storyboard.Begin(); |
| 486 | } |
| 487 | |
| 488 | private void ToastGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 489 | { |
| 490 | toastTimer.Stop(); |
| 491 | IsShowToast = false; |
| 492 | ToastGrid.MouseLeftButtonDown -= ToastGrid_MouseLeftButtonDown; |
| 493 | } |
| 494 | |
| 495 | private void HideToast() |
| 496 | { |
| 497 | Storyboard storyboard = new Storyboard(); |
| 498 | |
| 499 | DoubleAnimation scrollAnimation = new DoubleAnimation(); |
| 500 | scrollAnimation.To = -150; |
| 501 | |
| 502 | scrollAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 503 | Storyboard.SetTarget(scrollAnimation, ToastBorder); |
| 504 | Storyboard.SetTargetProperty(scrollAnimation, new PropertyPath("RenderTransform.Children[0].Y")); |
| 505 | scrollAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.35)); |
| 506 | |
| 507 | DoubleAnimation opacityAnimation = new DoubleAnimation(); |
| 508 | opacityAnimation.To = 0; |
| 509 | opacityAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 510 | Storyboard.SetTarget(opacityAnimation, Masklayer); |
| 511 | Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); |
| 512 | opacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.25)); |
| 513 | |
| 514 | storyboard.Children.Add(scrollAnimation); |
| 515 | if (!IsShowInputModal) |
| 516 | { |
| 517 | storyboard.Children.Add(opacityAnimation); |
| 518 | storyboard.Completed += (e, c) => |
| 519 | { |
| 520 | ToastGrid.Visibility = Visibility.Collapsed; |
| 521 | }; |
| 522 | } |
| 523 | |
| 524 | storyboard.Begin(); |
| 525 | } |
| 526 | |
| 527 | |
| 528 | #region Dialog |
| 529 | public Task<bool> ShowConfirmDialogAsync(string title_, string message_) |
| 530 | { |
| 531 | IsShowConfirmDialog = true; |
| 532 | ToastGrid.Visibility = Visibility.Visible; |
| 533 | ToastBorder.Visibility = Visibility.Collapsed; |
| 534 | DialogBorder.Visibility = Visibility.Visible; |
| 535 | InputModalBorder.Visibility = Visibility.Collapsed; |
| 536 | |
| 537 | DialogMessage = message_; |
| 538 | DialogTitle = title_; |
| 539 | |
| 540 | Storyboard storyboard = new Storyboard(); |
| 541 | |
| 542 | DoubleAnimation scrollAnimation = new DoubleAnimation(); |
| 543 | scrollAnimation.From = -150; |
| 544 | scrollAnimation.To = 10; |
| 545 | |
| 546 | scrollAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 547 | Storyboard.SetTarget(scrollAnimation, DialogBorder); |
| 548 | Storyboard.SetTargetProperty(scrollAnimation, new PropertyPath("RenderTransform.Children[0].Y")); |
| 549 | scrollAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.15)); |
| 550 | |
| 551 | DoubleAnimation opacityAnimation = new DoubleAnimation(); |
| 552 | opacityAnimation.To = 0.6; |
| 553 | opacityAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 554 | Storyboard.SetTarget(opacityAnimation, Masklayer); |
| 555 | Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); |
| 556 | opacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.15)); |
| 557 | storyboard.Children.Add(scrollAnimation); |
| 558 | storyboard.Children.Add(opacityAnimation); |
| 559 | storyboard.Begin(); |
| 560 | |
| 561 | return Task.Run(() => |
| 562 | { |
| 563 | |
| 564 | while (IsShowConfirmDialog) |
| 565 | { |
| 566 | Thread.Sleep(10); |
| 567 | } |
| 568 | |
| 569 | return IsDialogConfirm; |
| 570 | }); |
| 571 | } |
| 572 | private void HideDialog() |
| 573 | { |
| 574 | Storyboard storyboard = new Storyboard(); |
| 575 | |
| 576 | DoubleAnimation scrollAnimation = new DoubleAnimation(); |
| 577 | scrollAnimation.To = -150; |
| 578 | |
| 579 | scrollAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 580 | Storyboard.SetTarget(scrollAnimation, DialogBorder); |
| 581 | Storyboard.SetTargetProperty(scrollAnimation, new PropertyPath("RenderTransform.Children[0].Y")); |
| 582 | scrollAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.12)); |
| 583 | |
| 584 | DoubleAnimation opacityAnimation = new DoubleAnimation(); |
| 585 | opacityAnimation.To = 0; |
| 586 | opacityAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 587 | Storyboard.SetTarget(opacityAnimation, Masklayer); |
| 588 | Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); |
| 589 | opacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.15)); |
| 590 | |
| 591 | storyboard.Children.Add(scrollAnimation); |
| 592 | storyboard.Children.Add(opacityAnimation); |
| 593 | storyboard.Completed += (e, c) => |
| 594 | { |
| 595 | ToastGrid.Visibility = Visibility.Collapsed; |
| 596 | IsShowConfirmDialog = false; |
| 597 | }; |
| 598 | storyboard.Begin(); |
| 599 | } |
| 600 | #endregion |
| 601 | |
| 602 | #region InputModal |
| 603 | public Task<string> ShowInputModalAsync(string title_, string message_, string value_, Func<string, bool> validFnc_) |
| 604 | { |
| 605 | IsShowInputModal = true; |
| 606 | ToastGrid.Visibility = Visibility.Visible; |
| 607 | ToastBorder.Visibility = Visibility.Collapsed; |
| 608 | DialogBorder.Visibility = Visibility.Collapsed; |
| 609 | InputModalBorder.Visibility = Visibility.Visible; |
| 610 | |
| 611 | DialogMessage = message_; |
| 612 | DialogTitle = title_; |
| 613 | InputModalValue = value_; |
| 614 | InputModalValidFnc = validFnc_; |
| 615 | |
| 616 | InputModalInputBox.Text = InputModalValue; |
| 617 | |
| 618 | Storyboard storyboard = new Storyboard(); |
| 619 | |
| 620 | DoubleAnimation scrollAnimation = new DoubleAnimation(); |
| 621 | scrollAnimation.From = -150; |
| 622 | scrollAnimation.To = 10; |
| 623 | |
| 624 | scrollAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 625 | Storyboard.SetTarget(scrollAnimation, InputModalBorder); |
| 626 | Storyboard.SetTargetProperty(scrollAnimation, new PropertyPath("RenderTransform.Children[0].Y")); |
| 627 | scrollAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.15)); |
| 628 | |
| 629 | DoubleAnimation opacityAnimation = new DoubleAnimation(); |
| 630 | opacityAnimation.To = 0.6; |
| 631 | opacityAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 632 | Storyboard.SetTarget(opacityAnimation, Masklayer); |
| 633 | Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); |
| 634 | opacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.15)); |
| 635 | storyboard.Children.Add(scrollAnimation); |
| 636 | storyboard.Children.Add(opacityAnimation); |
| 637 | storyboard.Completed += (e, c) => |
| 638 | { |
| 639 | InputModalInputBox.Focus(); |
| 640 | }; |
| 641 | storyboard.Begin(); |
| 642 | |
| 643 | return Task.Run(() => |
| 644 | { |
| 645 | |
| 646 | while (IsShowInputModal) |
| 647 | { |
| 648 | Thread.Sleep(10); |
| 649 | } |
| 650 | |
| 651 | if (IsDialogConfirm) |
| 652 | { |
| 653 | return InputValue; |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | throw new Exception("Input cancel"); |
| 658 | } |
| 659 | }); |
| 660 | } |
| 661 | private void HideInputModal() |
| 662 | { |
| 663 | Storyboard storyboard = new Storyboard(); |
| 664 | |
| 665 | DoubleAnimation scrollAnimation = new DoubleAnimation(); |
| 666 | scrollAnimation.To = -150; |
| 667 | |
| 668 | scrollAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 669 | Storyboard.SetTarget(scrollAnimation, InputModalBorder); |
| 670 | Storyboard.SetTargetProperty(scrollAnimation, new PropertyPath("RenderTransform.Children[0].Y")); |
| 671 | scrollAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.12)); |
| 672 | |
| 673 | DoubleAnimation opacityAnimation = new DoubleAnimation(); |
| 674 | opacityAnimation.To = 0; |
| 675 | opacityAnimation.EasingFunction = new SineEase() { EasingMode = EasingMode.EaseIn }; |
| 676 | Storyboard.SetTarget(opacityAnimation, Masklayer); |
| 677 | Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); |
| 678 | opacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.15)); |
| 679 | |
| 680 | storyboard.Children.Add(scrollAnimation); |
| 681 | storyboard.Children.Add(opacityAnimation); |
| 682 | storyboard.Completed += (e, c) => |
| 683 | { |
| 684 | ToastGrid.Visibility = Visibility.Collapsed; |
| 685 | IsShowInputModal = false; |
| 686 | }; |
| 687 | storyboard.Begin(); |
| 688 | } |
| 689 | #endregion |
| 690 | |
| 691 | #region 6.事件 |
| 692 | |
| 693 | protected override void OnSourceInitialized(EventArgs e) |
| 694 | { |
| 695 | base.OnSourceInitialized(e); |
| 696 | if (IsThruWindow) |
| 697 | { |
| 698 | IntPtr hwnd = new WindowInteropHelper(this).Handle; uint extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE); |
| 699 | SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT); |
| 700 | } |
| 701 | } |
| 702 | #endregion |
| 703 | |
| 704 | #region win32 |
| 705 | private const int WS_EX_TRANSPARENT = 0x20; |
| 706 | private const int GWL_EXSTYLE = (-20); |
| 707 | |
| 708 | [DllImport("user32", EntryPoint = "SetWindowLong")] |
| 709 | private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong); |
| 710 | |
| 711 | [DllImport("user32", EntryPoint = "GetWindowLong")] |
| 712 | private static extern uint GetWindowLong(IntPtr hwnd, int nIndex); |
| 713 | #endregion |
| 714 | } |
| 715 | } |