Rg.Plugins.Popup 使用教程
Rg.Plugins.PopupXamarin Forms popup plugin项目地址:https://gitcode.com/gh_mirrors/rg/Rg.Plugins.Popup
项目介绍
Rg.Plugins.Popup 是一个用于 Xamarin.Forms 的开源弹出窗口插件。它允许开发者在不离开当前页面的情况下显示弹出窗口,提供了丰富的自定义选项和灵活的使用方式。该插件支持 iOS 和 Android 平台,并且与 .NET Standard 2.0、.NET Core 3.1 和 .NET Framework 4.7.2 兼容。
项目快速启动
安装 NuGet 包
首先,通过 NuGet 安装 Rg.Plugins.Popup 包:
dotnet add package Rg.Plugins.Popup --version 2.1.0
或者在 Visual Studio 的 Package Manager Console 中运行:
Install-Package Rg.Plugins.Popup -Version 2.1.0
初始化插件
在每个平台的项目中初始化插件:
Android
在 MainActivity.cs
中添加初始化代码:
using Rg.Plugins.Popup.Services;
using Rg.Plugins.Popup.Droid;
[Activity(Label = "YourApp", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Popup.Init(this, savedInstanceState);
LoadApplication(new App());
}
}
iOS
在 AppDelegate.cs
中添加初始化代码:
using Rg.Plugins.Popup.Services;
using Rg.Plugins.Popup.iOS;
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
Popup.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
创建弹出窗口
创建一个新的 XAML 页面作为弹出窗口:
<?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="YourApp.Popups.MyPopupPage">
<StackLayout>
<Label Text="Hello, Popup!" />
<Button Text="Close" Clicked="OnCloseButtonClicked" />
</StackLayout>
</pages:PopupPage>
在代码后台文件中处理关闭按钮的点击事件:
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;
namespace YourApp.Popups
{
public partial class MyPopupPage : PopupPage
{
public MyPopupPage()
{
InitializeComponent();
}
private async void OnCloseButtonClicked(object sender, EventArgs e)
{
await PopupNavigation.Instance.PopAsync();
}
}
}
显示弹出窗口
在主页面中显示弹出窗口:
using Rg.Plugins.Popup.Services;
namespace YourApp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnShowPopupButtonClicked(object sender, EventArgs e)
{
var popupPage = new YourApp.Popups.MyPopupPage();
await PopupNavigation.Instance.PushAsync(popupPage);
}
}
}
Rg.Plugins.PopupXamarin Forms popup plugin项目地址:https://gitcode.com/gh_mirrors/rg/Rg.Plugins.Popup