Rg.Plugins.Popup简介
Rg.Plugins.Popup是Xamarin.Forms的一个弹出框插件。可以在Xamarin.Forms里轻松的实现弹出框的效果。支持android、IOS、UWP。
GitHub地址:https://github.com/rotorgames/Rg.Plugins.Popup
Rg.Plugins.Popup简单使用示例
Rg.Plugins.Popup有对应的demo,可以下载查看。这里简单做一个loading页面。
- 安装Rg.Plugins.Popup。新版到发文章的时候还是beta版本,请选择安装beta版本。
- 在android项目的
MainActivity.cs
中、在IOS项目的AppDelegate.cs
、在UWP的App.xaml.cs
中的Xamarin.Forms.Forms.Init
前添加Rg.Plugins.Popup.Popup.Init(this, bundle);
- 建立一个
LoadingPopPage.xaml
,页面继承自Rg.Plugins.Popup.Pages.PopPage
,里面添加一个StackLayout
,包含一个ActivityIndicator
和一个Label
。<?xml version="1.0" encoding="utf-8" ?><pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup" x:Class="BookReader.View.LoadingPopPage"> <Frame VerticalOptions="Center" HorizontalOptions="Center" BackgroundColor="White"> <StackLayout Orientation="Horizontal"> <ActivityIndicator Color="Black" IsRunning="True" IsEnabled="True" /> <Label x:Name="LoadingMsg" FontSize="Large" VerticalTextAlignment="Center"></Label> </StackLayout> </Frame> </pages:PopupPage>
在LoadingPopPage.xaml.cs
里的构造函数中,我们给它赋值:public LoadingPopPage (string loadingMsg){ InitializeComponent (); LoadingMsg.Text = loadingMsg;}
如果需要屏蔽安卓的返回键,或者对返回做其他操作,可以重写OnBackButtonPressed
。protected override bool OnBackButtonPressed(){ return true;}
会屏蔽掉安卓的返回键。 - 如果需要屏蔽安卓的返回键,还需要在
MainActivity.cs
中添加public override void OnBackPressed() { if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed)) { // Do something if there are some pages in the `PopupStack` } else { // Do something if there are not any pages in the `PopupStack` } }
- 调用PopPage的方式很简单,
await Navigation.PushPopupAsync(new LoadingPopPage("正在加载,请稍后..."));
即可调起PopPage。如果使用MVVM,PageModel里没有Navigation,可以使用await PopupNavigation.Instance.PushAsync(new LoadingPopPage("正在加载,请稍后..."));
来调用。 - 关闭PopPage的方式有三种。
第一种是在PopPage里执行await Navigation.PopPopupAsync();
第二种是在调用的位置使用await PopupNavigation.Instance.RemovePageAsync(loadingPopPage);// 或者await Navigation.RemovePageAsync(loadingPopPage);// loadingPopPage是Push之前new出来的,需要与PushPopupAsync中使用相同的对象
第三种是关闭所有的弹出框对象Navigation.PopAllPopupAsync();