xamarin UWP平台下 HUD 自定义弹窗

时间:2023-03-09 02:16:04
xamarin UWP平台下 HUD 自定义弹窗

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

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

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

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

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

xaml文件代码:

 <UserControl
x:Class="Test.UWP.ExtendControls.UWPHUD"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test.UWP.ExtendControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight=""
d:DesignWidth=""> <Grid Name="mask_grid" Background="{ThemeResource TKHUDColorBrush}" >
<Grid Grid.Row="">
<Border Background="Black" CornerRadius="" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Margin="" Name="msg_Txt" Foreground="White" Text="..." MinWidth="" ></TextBlock>
</Border>
</Grid> </Grid>
</UserControl>

UWPHUD.xaml

xaml.cs文件代码:

 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; // The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236 namespace Test.UWP.ExtendControls
{
public sealed partial class UWPHUD : UserControl
{ Popup popup;
public UWPHUD()
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().BackRequested += UWPHUD_BackRequested;
}
public UWPHUD(string message)
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().BackRequested += UWPHUD_BackRequested;
msg_Txt.Text = message;
} private void UWPHUD_BackRequested(object sender, BackRequestedEventArgs e)
{
HUDClose();
} public void Show()
{
//var appView = ApplicationView.GetForCurrentView();
popup = new Popup();
popup.Child = this;
var bounds = Window.Current.Bounds;
this.Width = bounds.Width;
this.Height = bounds.Height;
popup.IsOpen = true;
}
public void HUDClose()
{
if (popup.IsOpen)
{
popup.IsOpen = false;
}
} }
}

UWPHUD.xaml.cs

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

 using Mixin.Dependencies;
using Mixin.UWP.ExtendControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Xamarin.Forms; [assembly: Dependency(typeof(Mixin.UWP.Dependencies.HUD))]
namespace Test.UWP.Dependencies
{
public class HUD : IHUD
{
private UWPHUD uwpHUD = null;
public HUD() { } public void Show(string msg)
{
ShowProgress(msg);
}
public void ShowSuccess(string msg)
{
ShowProgress(msg, );
}
public void ShowError(string msg)
{
ShowProgress(msg, );
}
public void ShowErrorLong(string msg)
{
ShowProgress(msg, );
} public void ShowProgress(string msg, int second = )
{
uwpHUD = new UWPHUD(msg);
uwpHUD.Show(); if (second > )
{
Task.Delay(second)
.ContinueWith(t =>
{
Device.BeginInvokeOnMainThread(() =>
{
uwpHUD.HUDClose();
});
}
);
}
else
{
Task.Delay()
.ContinueWith(t =>
{
Device.BeginInvokeOnMainThread(() =>
{
uwpHUD.HUDClose();
});
}
);
}
}
public void Dismiss()
{
Device.BeginInvokeOnMainThread(() =>
{
uwpHUD.HUDClose();
});
}
}
}

HUD.cs

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