Rg.Plugins.Popup 使用教程

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

班民航Small

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值