XAML 使用Rg插件弹出与Xamarin表单

nhjlsmyf  于 2023-09-28  发布在  其他
关注(0)|答案(3)|浏览(129)

我是一个非常新的Xamarin表单开发,我需要一个弹出对话框。我在https://github.com/rotorgames/Rg.Plugins.Popup中找到了我想要的东西,但我无法找到如何使用它的方法。有人能给我一个工作示例或提供一些使用方向吗?网站上的README.md对我没有多大帮助。
我想弹出对话框时,出现一个信息按钮在顶部导航栏点击。所有弹出窗口需要的是1-2个按钮(和标签),用于设置用户设置。
这是Xamarin的版本。iOS和Android.

83qze16e

83qze16e1#

简单的步骤:
1.在所有项目中安装插件
1.在Xaml中添加弹出窗口

  • 使用文档中提供的方法显示/隐藏弹出窗口:
  • 任务PushAsync(PopupPage页面,bool animate = true)
  • 任务PopAllAsync(bool animate = true)

他们还提供了一个演示,检查它:https://github.com/rotorgames/Rg.Plugins.Popup/tree/master/src/Demo

tpxzln5u

tpxzln5u2#

添加对库的引用,即从nuget到所有项目。
在您的Android项目中,将此Rg.Plugins.Popup.Popup.Init(this, savedInstanceState);添加到MainActivity.cs OnCreate方法中的Xamarin Forms Inits之前。
iOS项目也一样,AppDelegate.cs FinishedLaunching方法()内

//Android
 protected override void OnCreate(Bundle savedInstanceState)
 {
    base.OnCreate(savedInstanceState);
    Rg.Plugins.Popup.Popup.Init(this, savedInstanceState);  /*Must add before the other Xamarin Inits*/
     Xamarin.Essentials.Platform.Init(this, savedInstanceState);
     Xamarin.Forms.Forms.Init(this, savedInstanceState);
 }

  //iOS
  public override bool FinishedLaunching(UIApplication app, NSDictionary options)
  {
      Rg.Plugins.Popup.Popup.Init();   /* place at the top*/
      ....
  }

在Views目录下添加新的ContentPage(.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"
  xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;     assembly=Rg.Plugins.Popup"     
x:Class="MyProjectName.Views.MyContentPageName">
<pages:PopupPage.Animation>
    <animations:ScaleAnimation 
        PositionIn="Center"
        PositionOut="Center"
        ScaleIn="1.2"
        ScaleOut="0.8"
        DurationIn="400"
        DurationOut="300"
        EasingIn="SinOut"
        EasingOut="SinIn"
        HasBackgroundAnimation="True"/>
</pages:PopupPage.Animation>
  <StackLayout HorizontalAlignment="FillAndExpand" VerticalAlignment="FillAndExpand"> 
    <!-- place your layout content here ....fx a close popup button -->
     <Button Clicked="CloseBtn_Clicked" Text="Close" />
  </StackLayout>

</pages:PopupPage>

在ContentPage(PopupPage)代码隐藏文件中,添加using Rg.Plugins.Popup.Services;,继承如下

using Rg.Plugins.Popup.Services;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyContentPageName: Rg.Plugins.Popup.Pages.PopupPage
{
     public MyContentPageName()
    {
        InitializeComponent();            
    }      

      public void OnAnimationStarted(bool isPopAnimation)
    {
        // optional code here   
    }

    public void OnAnimationFinished(bool isPopAnimation)
    {
        // optional code here   
    }   

      protected override bool OnBackButtonPressed()
    {
        // Return true if you don't want to close this popup page when a back button is pressed
        return true;
    }

    // Invoked when background is clicked
    protected override bool OnBackgroundClicked()
    {
        // Return false if you don't want to close this popup page when a background of the popup page is clicked
        return false;
    }
    
    private async void CloseBtn_Clicked(object sender, EventArgs e)
    {
        await PopupNavigation.Instance.PopAsync(true);   
    }
}

在. xaml.cs页面中,您要打开弹出窗口,添加以下内容:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Rg.Plugins.Popup.Contracts;
using Rg.Plugins.Popup.Services;

public partial class MyOtherPage : ContentPage
{
   private IPopupNavigation _popup { get; set; }
   private MyContentPageName _modalPage;

    public MyOtherPage()    
    {
        _popup = PopupNavigation.Instance;
        _modalPage = new MyContentPageName();
    }   

   protected override void OnAppearing()
   {
       base.OnAppearing();          
       _popup.Popped += Popup_Popped;
   }

   protected override void OnDisappearing()
   {
       base.OnDisappearing();           
       _popup.Popped -= Popup_Popped;
   }

   private async void Tapped_OpenModal(object sender, EventArgs e)
   {
       await _popup.PushAsync(_modalPage);
   }
  
   /// <summary> Triggered when the MyContentPageName popup is closed "PopAsync()" </summary>
    private async void Popup_Popped(object sender, Rg.Plugins.Popup.Events.PopupNavigationEventArgs e)
    {
        /* add your logic here, if necessary  */
    }
}
  • 注意:如果你的modal只是显示静态内容,在OnAppearing()/OnDisappearing()中不需要_pop事件委托。**
iugsix8n

iugsix8n3#

System.InvalidOperationException:出现“平台未创建”错误

相关问题