xamarin UWP平台下 HUD 自定义弹窗

   在我的上一篇博客中我写了一个在xamarin的UWP平台下的自定义弹窗控件。在上篇文章中介绍了一种弹窗的写法,但在实际应用中发现了该方法的不足:

   1、当弹窗出现后,我们拖动整个窗口大小的时候,弹窗的窗口大小没有随着主窗口大小变化。

   2、当弹窗出现时,在我们的主窗口上的后退按钮并没有被弹窗遮罩住,这导致了在弹窗的时候窗口还能返回。

   为解决上述问题,我们需更改原来的方式,方法如下:

   1、定义一个uwp平台的用户控件

    xaml文件代码:

 1 <UserControl
 2     x:Class="Test.UWP.ExtendControls.UWPHUD"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:local="using:Test.UWP.ExtendControls"
 6     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     mc:Ignorable="d"
 9     d:DesignHeight="300"
10     d:DesignWidth="400">
11 
12     <Grid Name="mask_grid" Background="{ThemeResource TKHUDColorBrush}" >
13         <Grid Grid.Row="0">
14             <Border Background="Black" CornerRadius="10" HorizontalAlignment="Center" VerticalAlignment="Center">
15                 <TextBlock Margin="10" Name="msg_Txt" Foreground="White" Text="..." MinWidth="30" ></TextBlock>
16             </Border>
17         </Grid>
18         
19     </Grid>
20 </UserControl>
UWPHUD.xaml

    xaml.cs文件代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.IO;
 5 using System.Linq;
 6 using System.Runtime.InteropServices.WindowsRuntime;
 7 using Windows.Foundation;
 8 using Windows.Foundation.Collections;
 9 using Windows.UI.Core;
10 using Windows.UI.ViewManagement;
11 using Windows.UI.Xaml;
12 using Windows.UI.Xaml.Controls;
13 using Windows.UI.Xaml.Controls.Primitives;
14 using Windows.UI.Xaml.Data;
15 using Windows.UI.Xaml.Input;
16 using Windows.UI.Xaml.Media;
17 using Windows.UI.Xaml.Navigation;
18 
19 // The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
20 
21 namespace Test.UWP.ExtendControls
22 {
23     public sealed partial class UWPHUD : UserControl
24     {
25         
26         Popup popup;
27         public UWPHUD()
28         {
29             this.InitializeComponent();
30             SystemNavigationManager.GetForCurrentView().BackRequested += UWPHUD_BackRequested;
31         }
32         public UWPHUD(string message)
33         {
34             this.InitializeComponent();
35             SystemNavigationManager.GetForCurrentView().BackRequested += UWPHUD_BackRequested;
36             msg_Txt.Text = message;
37         }
38 
39         private void UWPHUD_BackRequested(object sender, BackRequestedEventArgs e)
40         {
41             HUDClose();
42         }
43 
44         public void Show()
45         {
46             //var appView = ApplicationView.GetForCurrentView();
47             popup = new Popup();
48             popup.Child = this;
49             var bounds = Window.Current.Bounds;
50             this.Width = bounds.Width;
51             this.Height = bounds.Height;
52             popup.IsOpen = true;
53         }
54         public void HUDClose()
55         {
56             if (popup.IsOpen)
57             {
58                 popup.IsOpen = false;
59             }
60         }
61         
62     }
63 }
UWPHUD.xaml.cs

    同时在原来的基础代码上调用现在定义好的uwp用户控件,代码如下:

 1 using Mixin.Dependencies;
 2 using Mixin.UWP.ExtendControls;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 using Windows.UI;
 9 using Windows.UI.Xaml;
10 using Windows.UI.Xaml.Controls;
11 using Windows.UI.Xaml.Controls.Primitives;
12 using Windows.UI.Xaml.Media;
13 using Xamarin.Forms;
14 
15 [assembly: Dependency(typeof(Mixin.UWP.Dependencies.HUD))]
16 namespace Test.UWP.Dependencies
17 {
18     public class HUD : IHUD
19     {
20         private UWPHUD uwpHUD = null; 
21         public HUD() { }
22 
23         public void Show(string msg)
24         {
25             ShowProgress(msg);
26         }
27         public void ShowSuccess(string msg)
28         {
29             ShowProgress(msg, 2);
30         }
31         public void ShowError(string msg)
32         {
33             ShowProgress(msg, 2);
34         }
35         public void ShowErrorLong(string msg)
36         {
37             ShowProgress(msg, 30);
38         }
39 
40         public void ShowProgress(string msg, int second = 0)
41         {
42             uwpHUD = new UWPHUD(msg);
43             uwpHUD.Show();
44 
45             if (second > 0)
46             {
47                 Task.Delay(second)
48                 .ContinueWith(t =>
49                 {
50                     Device.BeginInvokeOnMainThread(() =>
51                     {
52                         uwpHUD.HUDClose();
53                     });
54                 }
55               );
56             }
57             else
58             {
59                 Task.Delay(3000)
60                 .ContinueWith(t =>
61                 {
62                     Device.BeginInvokeOnMainThread(() =>
63                     {
64                         uwpHUD.HUDClose();
65                     });
66                 }
67               );
68             }
69         }
70         public void Dismiss()
71         {
72             Device.BeginInvokeOnMainThread(() =>
73             {
74                 uwpHUD.HUDClose();
75             });
76         }
77     }
78 }
HUD.cs

     经如上方式可解决弹窗的两个问题。

转载于:https://www.cnblogs.com/zuimengaitianya/p/5846356.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值