天天看點

Xamarin.Forms彈出對話框插件

微信公衆号:Dotnet9,網站:Dotnet9,問題或建議,請網站留言;

如果您覺得Dotnet9對您有幫助,歡迎贊賞。

Xamarin.Forms彈出對話框插件

内容目錄

  1. 實作效果
  2. 業務場景
  3. 編碼實作
  4. 本文參考
  5. 源碼下載下傳

1.實作效果

彈出動畫

2.業務場景

主視窗彈出登入或者其他小視窗時使用

3.編碼實作

3.1 添加Nuget庫

建立名為“App5”的Xamarin.Forms項目,添加Rg.Plugins.PopupNuget庫:彈出框由該插件提供,看下圖1.31M下載下傳量,請放心使用。

Rg.Plugins.PopupNuget插件

3.2 工程結構

數個檔案變動:

  1. 共享庫中的MainPage:主視窗
  2. 共享庫中的LoginPage:彈出的登入對話框
  3. MainActivity.cs:Android中需要注冊上面的插件
  4. AppDelegate.cs:iOS中需要注冊上面的插件

3.3 共享庫中的MainPage

簡單的一個按鈕控件,點選模拟觸發彈出登入視窗

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App5.MainPage">

    <StackLayout Spacing="18"
                 VerticalOptions="Center">
        <Button Clicked="ShowPopup"
                Text="彈出窗體" />
    </StackLayout>

</ContentPage>
           

背景彈出登入視窗

private void ShowPopup(object o, EventArgs e)
{
    PopupNavigation.Instance.PushAsync(new LoginPage());
}
           

3.4 共享庫中的LoginPage

登入視窗,引入彈出插件Rg.Plugins.Popup,設定彈出框動畫

<?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:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
        
    xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
    xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
    x:Class="App5.Views.LoginPage">

    <pages:PopupPage.Animation>
        <animations:ScaleAnimation DurationIn="400"
                                   DurationOut="300"
                                   EasingIn="SinOut"
                                   EasingOut="SinIn"
                                   HasBackgroundAnimation="True"
                                   PositionIn="Center"
                                   PositionOut="Center"
                                   ScaleIn="1.2"
                                   ScaleOut="0.8" />
    </pages:PopupPage.Animation>

    <Grid BackgroundColor="White" VerticalOptions="Center" Margin="30" HeightRequest="350">
        <Grid.RowDefinitions>
            <RowDefinition Height="80"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Margin="0,10,0,0">
            <Label Text="選擇語言"/>
            <Image Source="down_arrow.png" Opacity="0.6" VerticalOptions="Start" Margin="0,3,0,0"/>
        </StackLayout>
        <Grid Grid.Row="1" Margin="20,0,20,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="40"/>
                <RowDefinition Height="40"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Image Source="person.png" HeightRequest="70" VerticalOptions="End"/>
            <Entry Grid.Row="1" Placeholder="賬号" PlaceholderColor="#bababa" FontSize="16"/>
            <Entry Grid.Row="2" Placeholder="密碼" PlaceholderColor="#bababa" FontSize="16"/>
            <Button Grid.Row="3" Text="登入" BackgroundColor="#3897f0" TextColor="White" HeightRequest="50" VerticalOptions="Start"/>
            <Label Grid.Row="4" Text="沒有賬号?請聯系管理者。" HorizontalOptions="Center" Margin="0,10,0,0" FontSize="12"/>
        </Grid>
    </Grid>

</pages:PopupPage>
           

3.6 Android項目中的MainActivity.cs

注冊彈出插件

3.7 iOS項目中的AppDelegate.cs

4.本文參考

Houssem Dellai 大神的學習視訊:Popup in Xamarin Forms

5.代碼下載下傳

文中代碼已經全部提供,參考Github源碼:Xamarin-Forms-Popup-Demo

除非注明,文章均由 Dotnet9 整理釋出,歡迎轉載。

轉載請注明本文位址:https://dotnet9.com/6829.html

歡迎掃描下方二維碼關注 Dotnet9 的微信公衆号,本站會及時推送最新技術文章

時間如流水,隻能流去不流回。