原文:
https://github.com/rotorgames/Rg.Plugins.Popup
譯者寫的教程:
http://www.reibang.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: 當(dāng)點擊背景的時候觸發(fā)至扰。
動畫:
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)
{
...
// 由于在釋放模式下編譯時出現(xiàn)錯誤暴氏,因此需要進行初始化厅贪。
// 詳情參考: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實現(xiàn)
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: 點擊背景時關(guān)閉彈出窗口
HasSystemPadding: 啟用/禁用系統(tǒng)填充偏移(僅適用于內(nèi)容不適用于背景)
SystemPadding:(只讀)厚度
如何使用:
// 在全局PopupNavigation中使用這些方法或在您的頁面中導(dǎo)航
// 打開一個新的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中動畫兒童的方法
// 自定義動畫結(jié)束后調(diào)用
protected virtual Task OnAppearingAnimationEnd()
{
return Content.FadeTo(0.5);
}
// PopupPage中動畫兒童的方法
// 在自定義動畫開始之前調(diào)用
protected virtual Task OnDisappearingAnimationBegin()
{
return Content.FadeTo(1);;
}
protected override bool OnBackButtonPressed()
{
// 防止隱藏彈出窗口
//return base.OnBackButtonPressed();
return true;
}
// 當(dāng)背景點擊時調(diào)用
protected override bool OnBackgroundClicked()
{
// 返回默認(rèn)值 - 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之前調(diào)用
public void Preparing(View content, PopupPage page)
{
// Preparing content and page
content.Opacity = 0;
}
// 在OnDisappering后調(diào)用
public void Disposing(View content, PopupPage page)
{
// Dispose Unmanaged Code
}
// OnAppering之后調(diào)用
public async Task Appearing(View content, PopupPage page)
{
// 顯示動畫
await content.FadeTo(1);
}
// OnDisappering之前調(diào)用
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>