Learning about SwipeView in Xamarin Forms

栏目: IT技术 · 发布时间: 3年前

内容简介:Hidden menus make applications more aesthetic and easy to use by keeping some options hidden. This post explores how this works using the SwipeView control in Xamarin Forms.Have you ever seen apps with cool hidden menus with a set of options that you need

Hidden menus make applications more aesthetic and easy to use by keeping some options hidden. This post explores how this works using the SwipeView control in Xamarin Forms.

Have you ever seen apps with cool hidden menus with a set of options that you need to slide left or right to get to them? No? Let me give an example… :thought_balloon: imagine a restaurant app in which you have a list of foods and within that list you can slide the chosen food. By doing this slide, you can see different actions that were hidden ( for example, modify or delete food ).

Thanks to these types of menus, we can make our applications more aesthetic and reduce space in the interface design by keeping some options hidden, making our user experience more intuitive and fun! And guess what!? In Xamarin Forms this control is named SwipeView and in this article we will be learning how to use it! Let’s see!

First of all… What Do I Need to Know?

:heavy_minus_sign: What is an Experimental Preview?

Learning about SwipeView in Xamarin Forms Learning about SwipeView in Xamarin Forms

An Experimental Preview is a set of functions or libraries from preview versions of Xamarin.Forms that is available for experimental purposes. To learn more about it, you can read this article .

⚠ It’s important to know that being an Experimental Preview does not make the components less important. On the contrary, they are put in this way to improve the experiences and the operation obtained thanks to the use, implementation and feedback from the users!

As SwipeView is an Experimental Preview, you need to indicate that you want to enable it so you can use it. You must add a configuration in the following files:

Platform File name
Android MainActivity.cs
iOS AppDelegate.cs

In the indicated files for each platform, add the following line of code just before calling Forms.Init() :

Forms.SetFlags(“SwipeView_Experimental”);

Let’s Start!

✔ What is SwipeView?

SwipeView is a container control that is available in Xamarin.Forms 4.4 as experimental preview. It wraps around an item of content and provides context menu items that are revealed by a swipe gesture. By default, once a swipe item has been executed the swipe items are hidden and the SwipeView content is re-displayed. (This behavior can be modified.)

:green_book:

As it is in experimental preview, you can’t forget to apply the configuration explained above.

Learning about SwipeView in Xamarin Forms

⚠ Image obtained from Microsoft official documentation.

Knowing the Implementation

Swipeview Structure

In order to implement the SwipeView, we have to declare one of its Properties and populate it with swipe items. We show the structure graphically later, but before that, let’s see in detail what these properties are.

Properties

To make possible the movement inside the SwipeView elements, we have to declare at least one of these properties, and populate it with elements of type SwipeItem, which represents the items that can be shown when the control is swiped Left, Right, Up or Down.

Property name
LeftItems
RightItems
TopItems
BottomItems

Important to know: These properties define the place where the item will be.

Having understood the properties, now we will see the structure graphically!

Learning about SwipeView in Xamarin Forms

Invoked event is fired when the swipe item is executed.

And now let’s code the example!

Defining the Invoked method:

async void OnDeleteSwipeItemInvoked(object sender, EventArgs e)
            {
                // Here add the action that you want to do when you make a click in the Delete option.
            }

Some Great Features!

With the SwipeItems class, we can define Modes or Behaviors . I’ll show them one by one!

:ledger: Mode: Is a property that allows us to indicate the effect of a swipe interaction.

It should be set to one of the SwipeMode enumeration members:

  • Reveal:It indicates that a swipe reveals the swipe items. (Default value)

  • Execute:Indicates that a swipe executes the swipe items.

Once executed, the swipe items are closed and the SwipeView content is re-displayed.

Code example

    <SwipeView>  

        <SwipeView.LeftItems>  

           <SwipeItems Mode="Execute">  

             <SwipeItem Text="Delete"

                        IconImageSource="delete.png"     

                        BackgroundColor="LightPink" 

                        Command="{Binding DeleteCommand}" />  

             </SwipeItems>  

       </SwipeView.LeftItems>

    </SwipeView>

:closed_book: Behavior: SwipeItem class has a SwipeBehaviorOnInvoked property, which indicates how a SwipeView behaves after a swipe item is invoked.

It should be set to one of the SwipeBehaviorOnInvoked enumeration members:

  • Auto: Indicates that in reveal mode the SwipeView closes after a swipe item is invoked, and in execute mode the SwipeView remains open after a swipe item is invoked. (Default value)

  • Close:Indicates that the SwipeView closes after a swipe item is invoked.

  • RemainOpen:Indicates that the SwipeView remains open after a swipe item is invoked.

Code example

    <SwipeView>

        <SwipeView.LeftItems>

            <SwipeItems SwipeBehaviorOnInvoked="RemainOpen">

                <SwipeItem Text="Favorite"

                           IconImageSource="favorite.png"

                           BackgroundColor="LightGreen"

                           Invoked="OnFavoriteSwipeItemInvoked" />

                <SwipeItem Text="Delete"

                           IconImageSource="delete.png"

                           BackgroundColor="LightPink"

                           Invoked="OnDeleteSwipeItemInvoked" />

            </SwipeItems>

        </SwipeView.LeftItems>

        <!-- Content -->

    </SwipeView>

And finally… Handling events!

You handle event with the SwipeView:

Event name Description
SwipeStarted Fired when a swipe starts
SwipeChanging Fired as the swipe moves
SwipeEnded Fired when a swipe ends
CloseRequested Fired when the swipe items are closed

And that’s all for today! I hope that this post will be of help to you! :relaxed:

Thanks for reading! :green_heart:

References:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/swipeview#swipe-mode


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Spring揭秘

Spring揭秘

王福强 / 人民邮电出版社 / 2009.8 / 99.00元

没有教程似的训导,更多的是说故事般的娓娓道来,本书是作者在多年的工作中积累的第一手Spring框架使用经验的总结,深入剖析了Spring框架各个模块的功能、出现的背景、设计理念和设计原理,揭开了Spring框架的神秘面纱,使你“知其然,更知其所以然”。每部分的扩展篇帮助读者活学活用Spring框架的方方面面,同时可以触类旁通,衍生出新的思路和解决方案。 本书内容全面,论述深刻入理,必将成为每......一起来看看 《Spring揭秘》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具