Introduction
This article demonstrates how to enable slide-up panel in Xamarin.Forms application. This means if the user drags the panel from down to up, it shows title, image, description and more. Similarly, the user can drag the panel from up to down and it will show only the title and set the custom behavior of it. These features are not listed by default in a Xamarin.Forms application. So, we need to install a plug-in for this.
NuGet Package - Search Xamarin.Forms
- DK.SlideUpPanel
- Reactive.Fody
Xamarin.Android Output
Let's start.
Step 1
You can create Xamarin.Forms app by going to File >> New >> Visual C# >> Cross-Platform >>Cross Platform App (Xamarin.Native or Xamarin.Forms), give the application name, and click OK.
(Eg- Project name: SlidingDemo)
Step 2
After the project creation, add the following NuGet package to your project.
- DK.SlideUpPanel
- Reactive.Fody
For that, open Solution Explorer and select your solution. Right-click and select "manage Nuget Package for the Solution". In the popup window, open "Browse " tab and browse "DK.SlideUpPanel","Reactive.Fody" and select the following NuGet packages and select yours then install it.
Step 3
Afterwards, Add ViewModels Folder. For that, open Solution Explorer >>SlidingDemo(PCL) >> right click and select Add followed by selecting New Folder named ViewModels.
Open Solution Explorer >>SlidinDemo(PCL)>>right-click and select Add folloed by selecting New Item. In the popup window select class. In this way, you can create a new class.
In this step, add a new class named AbstractViewModels.cs and add the following code.
C# Code
- using ReactiveUI;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace SlideUpDemo.ViewModels
- {
- public class AbstractViewModel : ReactiveObject
- {
- public AbstractViewModel()
- {
-
- }
- }
- }
Next, add another class named TestPageAllXamlViewModel.cs and insert the code given below.
C# Code
- using ReactiveUI.Fody.Helpers;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Input;
- using Xamarin.Forms;
-
- namespace SlideUpDemo.ViewModels
- {
- public class TestPageAllXamlViewModel : AbstractViewModel
- {
-
- [Reactive]
- public bool IsPlaying { get; set; }
- [Reactive]
- public bool IsFavorite { get; set; }
-
- [Reactive]
- public ImageSource PlayButtonImage { get; set; }
-
- [Reactive]
- public ImageSource FavoriteButtonImage { get; set; }
-
-
-
- public ICommand PlayCommand { get; set; }
-
- public ICommand ShowCommand { get; set; }
- public ICommand HideCommand { get; set; }
-
-
- [Reactive]
- public double PanelRatio { get; set; }
-
- [Reactive]
- public bool HideTitleView { get; set; }
-
- public TestPageAllXamlViewModel()
- {
- }
- }
- }
Step 4
Open Solution Explorer >> SlidingDemo(PCL) >> double-click to open MainPage.Xaml page and You can add assembly and XAML code to your project. Here is the code.
- SlidingUpPanel.MainiView - MainView Layout.
- SlidingUpPanel.HideTitleView - we need to show Title of Sliding Panel on Hidden Mode, so enable true.
- SlidingUpPanel.HideNavBar - we need to show NavBar on Hidden Mode, so enable true or false.
- SlidingUpPanel.HeaderView - It's the header view of Sliding panel.
- SlidingUpPanel.PictureView -It's the Picture view of Sliding panel.
- SlidingUpPanel.BodyView - It's the body view of sliding panel.
- SlidingUpPanel.PanelRatio - Set the panel ratio of sliding panel.
NameSpace
- xmlns:DK="clr-namespace:DK.SlidingPanel.Interface;assembly=DK.SlidingPanel.Interface"
XAML Code
- <?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:local="clr-namespace:SlideUpDemo"
- xmlns:DK="clr-namespace:DK.SlidingPanel.Interface;assembly=DK.SlidingPanel.Interface"
- x:Class="SlideUpDemo.MainPage"
- >
- <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
-
- <DK:SlidingUpPanel x:Name="spTest" BackgroundColor="Coral" SlideAnimationSpeed="250" IsExpandable="True" IsPanSupport="True">
-
-
- <DK:SlidingUpPanel.MainView>
- <ScrollView>
- <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- <Label Text="Hai All this is Main Layout" FontSize="30"/>
- </StackLayout>
- </ScrollView>
- </DK:SlidingUpPanel.MainView>
-
-
-
- <DK:SlidingUpPanel.HideTitleView>True</DK:SlidingUpPanel.HideTitleView>
- <DK:SlidingUpPanel.HideNavBar>True</DK:SlidingUpPanel.HideNavBar>
- <DK:SlidingUpPanel.PanelRatio>0.5</DK:SlidingUpPanel.PanelRatio>
-
- <DK:SlidingUpPanel.HeaderView>
-
- <StackLayout Orientation="Horizontal" HeightRequest="48" Padding="0, 12, 0, 12" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="#303030">
-
- <Image HeightRequest="24" WidthRequest="24" Source="ic_keyboard_arrow_left_black_48dp.png" HorizontalOptions="Start" VerticalOptions="FillAndExpand">
- <Image.GestureRecognizers>
- <TapGestureRecognizer Tapped="BackButtonTapGesture_Tapped"></TapGestureRecognizer>
- </Image.GestureRecognizers>
- </Image>
-
- <Label Text="Title" HorizontalOptions="CenterAndExpand" TextColor="White"></Label>
- </StackLayout>
-
-
- </DK:SlidingUpPanel.HeaderView>
-
- <DK:SlidingUpPanel.PictureView>
- <Image BackgroundColor="White" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand" Aspect="AspectFill" Source="honda.jpg"></Image>
- </DK:SlidingUpPanel.PictureView>
-
- <DK:SlidingUpPanel.TitleView>
- <StackLayout Orientation="Vertical" HeightRequest="80" BackgroundColor="GreenYellow">
- <Label Text="Title 1" FontSize="20"></Label>
- <Label Text="Title2" FontSize="20"></Label>
- </StackLayout>
- </DK:SlidingUpPanel.TitleView>
- <DK:SlidingUpPanel.BodyView>
- <ScrollView BackgroundColor="Blue">
- <Label Text="Test Body y Test Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body yTest Body y ">
-
- </Label>
- </ScrollView>
- </DK:SlidingUpPanel.BodyView>
-
- </DK:SlidingUpPanel>
- </StackLayout>
-
- </ContentPage>
Step 5
Next, open Solution Explorer >> SlidingDemo(PCL) >> click open desgin view of MainPage.Xaml.cs and here is the code for this page.
- PrimaryFloatingButton - In this the floating button shows on before we slidingup
- SecondaryFloatingButton - In this the floating Button shows on after we slidingup
C# Code Behind
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DK.SlidingPanel.Interface;
- using ReactiveUI;
- using SlideUpDemo.ViewModels;
- using Xamarin.Forms;
-
- namespace SlideUpDemo
- {
- public partial class MainPage : ContentPage
- {
- private TestPageAllXamlViewModel ViewModel;
-
-
- protected override void OnBindingContextChanged()
- {
- base.OnBindingContextChanged();
-
- this.ViewModel = BindingContext as TestPageAllXamlViewModel;
-
- this.spTest.SetBinding(SlidingUpPanel.PanelRatioProperty, new Binding { Path = "PanelRatio" });
- this.spTest.SetBinding(SlidingUpPanel.HideTitleViewProperty, new Binding { Path = "HideTitleView" });
-
- this.spTest.WhenPanelRatioChanged += SpTest_WhenPanelRatioChanged;
- this.spTest.WhenSlidingPanelStateChanged += SpTest_WhenSlidingPanelStateChanged;
-
- SlidingPanelConfig config = new SlidingPanelConfig();
-
- config.PrimaryFloatingActionButton = GetPrimaryFloatingActionButton();
- config.SecondaryFloatingActionButton = GetSecondaryFlotingButton();
-
- spTest.ApplyConfig(config);
-
-
-
-
- if (spTest.CurrentState != SlidingPanelState.Expanded)
- {
- spTest.HidePanel();
-
- }
- else
- {
- spTest.ShowCollapsedPanel();
- }
-
-
- }
-
- private Image GetSecondaryFlotingButton()
- {
- Image primaryImage = new Image();
- primaryImage.HeightRequest = 48;
- primaryImage.WidthRequest = 48;
- primaryImage.Margin = new Thickness(0, 6, 0, 0);
- primaryImage.Source = ImageSource.FromFile("StopButton.png");
-
- TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer();
- tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped;
- primaryImage.GestureRecognizers.Add(tapGestureRecognizer);
- return (primaryImage);
- }
-
- private Image GetPrimaryFloatingActionButton()
- {
- Image primaryImage = new Image();
- primaryImage.HeightRequest = 48;
- primaryImage.WidthRequest = 48;
- primaryImage.Source = ImageSource.FromFile("PlayButton.png");
-
- TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer();
- tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped;
- primaryImage.GestureRecognizers.Add(tapGestureRecognizer);
- return (primaryImage);
- }
-
- private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
- {
- this.ViewModel.IsPlaying = !(this.ViewModel.IsPlaying);
- }
-
- public MainPage()
- {
- InitializeComponent();
- }
- private void SpTest_WhenSlidingPanelStateChanged(object sender, DK.SlidingPanel.Interface.StateChangedEventArgs e)
- {
- switch (e.State)
- {
- case SlidingPanelState.Expanded:
-
- NavigationPage.SetHasNavigationBar(this, false);
- break;
- case SlidingPanelState.Collapsed:
- case SlidingPanelState.Hidden:
- default:
- //NavigationPage.SetHasNavigationBar(this, true);
- break;
- }
- }
- private void SpTest_WhenPanelRatioChanged(object sender, EventArgs e)
- {
- spTest.ShowCollapsedPanel();
- }
- private void BackButtonTapGesture_Tapped(object sender, EventArgs e)
- {
- spTest.ShowCollapsedPanel();
- }
-
- private void TapGestureRecognizer_OnTapped(object sender, EventArgs e)
- {
- this.ViewModel.IsPlaying = !(this.ViewModel.IsPlaying);
- }
- }
- }
Step 6
Add FodyWeavers.xml file. For that, open Solution Explorer >>SlidingDemo(PCL)>> Right-click and select New Item. In the popup window, select Xml file named FodyWeavers.xml and click ok. Copy and Paste this file to all projects( Xamarin.Android, Xamarin.IOS).
Add the Following Code.
XML code
- <?xml version="1.0" encoding="utf-8" ?>
- <Weavers>
- <ReactiveUI />
- </Weavers>
Step 7
Now, go to "Build" menu and click "Configure Manager". Here, you can configure startup projects. Click F5 or start to build and run your application.
After a few seconds, the app will start running on Android simulator or emulator and we will see your app working successfully.
Finally, we have successfully created a Xamarin.Forms SlidingDemo application using DK.SlideUpPanel plugin.
Please download the source code from
here.