Rg.Popup-Xamarin Forms翻译

原文:

https://github.com/rotorgames/Rg.Plugins.Popup

译者写的教程:

http://www.jianshu.com/p/5e5663b6d1ba

Xamarin Forms的弹出页面插件

插件可以让你使用任何Page弹出。
Nuget:
https://www.nuget.org/packages/Rg.Plugins.Popup/

支持的平台:

  • Android
  • iOS
  • Windows Phone
  • UWP

PopupPage重写的方法:

  • OnAppearing
  • OnDisappearing
  • OnBackButtonPressed
  • OnAppearingAnimationEnd
  • OnDisappearingAnimationBegin
  • OnBackgroundClicked

事件:

  • BackgroundClicked: 当点击背景的时候触发。

动画:

FadeAnimation(淡化动画):

  • DurationIn (uint):
  • DurationOut (uint)
  • EasingIn (Easing)
  • EasingOut (Easing)
  • HasBackgroundAnimation (bool):背景动画

MoveAnimation(移动动画):

  • PositionIn (MoveAnimationOptions)
  • PositionOut (MoveAnimationOptions)
  • DurationIn (uint)
  • DurationOut (uint)
  • EasingIn (Easing)
  • EasingOut (Easing)
  • HasBackgroundAnimation (bool)

ScaleAnimation(缩放动画):

  • ScaleIn (double)
  • ScaleOut (double)
  • PositionIn (MoveAnimationOptions)
  • PositionOut (MoveAnimationOptions)
  • DurationIn (uint)
  • DurationOut (uint)
  • EasingIn (Easing)
  • EasingOut (Easing)
  • HasBackgroundAnimation (bool)

初始化:

Android, iOS, WP不需要初始化。
如果您使用.NET Native编译,UWP需要在Xamarin.Forms.Forms.Init方法中添加程序集:

UWP 例子:

// In App.xaml.cs
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...

    // 由于在释放模式下编译时出现错误,因此需要进行初始化。
    // 详情参考:https://developer.xamarin.com/guides/xamarin-forms/platform-features/windows/installation/universal/#Troubleshooting
    
    Xamarin.Forms.Forms.Init(e, Rg.Plugins.Popup.Windows.Popup.GetExtraAssemblies());

    // 或者如果您有其他渲染器和DependencyService实现

    var assemblies = new List<Assembly>
    {
       typeof(YourRenderer).GetTypeInfo().Assembly
       typeof(YourServiceImplementation).GetTypeInfo().Assembly
    };

    Xamarin.Forms.Forms.Init(e, Rg.Plugins.Popup.Windows.Popup.GetExtraAssemblies(assemblies));

    ...
}

PopupPage属性:

IsAnimating
Animation
BackgroundColor: 16进制 #80FF5C5C 其中 #80 是不透明度 Range
CloseWhenBackgroundIsClicked: 点击背景时关闭弹出窗口
HasSystemPadding: 启用/禁用系统填充偏移(仅适用于内容不适用于背景)

image.png
image.png

SystemPadding:(只读)厚度

如何使用:

// 在全局PopupNavigation中使用这些方法或在您的页面中导航

// 打开一个新的PopupPage
Task PushAsync(PopupPage page, bool animate = true) // Navigation.PushPopupAsync

// 隐藏最后一个PopupPage
Task PopAsync(bool animate = true) // Navigation.PopPopupAsync

// 隐藏所有PopupPage与动画
Task PopAllAsync(bool animate = true) // Navigation.PopAllPopupAsync

// 在堆栈中删除一个弹出页面
Task RemovePageAsync(PopupPage page, bool animate = true) // Navigation.RemovePopupPageAsync
<?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="Demo.Pages.MyPopupPage">
  <!--动画使用示例-->
  <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>
  <!-- Content -->
</pages:PopupPage>
public partial class MyPopupPage : PopupPage
    {
        public SecondPopupPage()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
        }
        
        // PopupPage中动画儿童的方法
        // 自定义动画结束后调用
        protected virtual Task OnAppearingAnimationEnd()
        {
            return Content.FadeTo(0.5);
        }

        // PopupPage中动画儿童的方法
        // 在自定义动画开始之前调用
        protected virtual Task OnDisappearingAnimationBegin()
        {
            return Content.FadeTo(1);;
        }

        protected override bool OnBackButtonPressed()
        {
            // 防止隐藏弹出窗口
            //return base.OnBackButtonPressed();
            return true; 
        }

        // 当背景点击时调用
        protected override bool OnBackgroundClicked()
        {
            // 返回默认值 - CloseWhenBackgroundIsClicked
            return base.OnBackgroundClicked();
        }
    }

    ```
    // MainPage
    
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        
        // 按钮点击
        private async void OnOpenPupup(object sender, EventArgs e)
        {
            var page = new MyPopupPage();
            
            await Navigation.PushPopupAsync(page);
            // 或者
            await PopupNavigation.PushAsync(page);
        }
    }
## 使用动画:

// 使用动画
class UserAnimation : IPopupAnimation
{
// 在OnAppering之前调用
public void Preparing(View content, PopupPage page)
{
// Preparing content and page
content.Opacity = 0;
}

    // 在OnDisappering后调用
    public void Disposing(View content, PopupPage page)
    {
        // Dispose Unmanaged Code
    }
    
    //  OnAppering之后调用
    public async Task Appearing(View content, PopupPage page)
    {
        // 显示动画
        await content.FadeTo(1);
    }
    
    // OnDisappering之前调用
    public async Task Disappearing(View content, PopupPage page)
    {
        // 隐藏动画
        await content.FadeTo(0);
    }
}

// Popup Page
public partial class UserPopupPage : PopupPage
{
public SecondPopupPage()
{
InitializeComponent();
Animation = new UserAnimation();
}
}

## 或者在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:Demo.Animations;assembly=Demo"
x:Class="Demo.Pages.UserAnimationPage">
<pages:PopupPage.Animation>
<animations:UserAnimation/>
</pages:PopupPage.Animation>
...
</pages:PopupPage>

推荐阅读更多精彩内容