bool AllowTap = true; private void Button_Clicked(object sender, EventArgs e) { if (AllowTap) { AllowTap = false; //Write logic here await Task.Delay(1000); AllowTap = true; } }
Learn .NET MAUI with step-by-step C# tutorials and ready-to-use code samples MVVM, SQLite, REST APIs, navigation, notifications and more for cross-platform apps.
Wednesday, 29 April 2020
How to avoid multiple buttons being clicked at same time in Xamarin.Forms
Thursday, 20 February 2020
Xamarin Form Read More Text Label (Custom Control)
Add the Content View in your Project.
CustomLabel.xaml<?xml version="1.0" encoding="UTF-8"?> <ContentView 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="TestApp.CustomControl.CustomLabel"> <ContentView.Content> <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> <Label x:Name="customLabel" > </Label> <Label x:Name="lblReadMore" FontSize="18" FontAttributes="Bold" > <Label.GestureRecognizers> <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" /> </Label.GestureRecognizers> </Label> </StackLayout> </ContentView.Content> </ContentView>
Create the bindable property and getter setter method for the label.
CustomLabel.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace TestApp.CustomControl { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class CustomLabel : ContentView { public CustomLabel() { InitializeComponent(); } #region Bindable Property public static readonly BindableProperty TextProperty = BindableProperty.Create( propertyName: nameof(TextProperty), returnType: typeof(string), declaringType: typeof(CustomLabel), defaultBindingMode: BindingMode.TwoWay, propertyChanged: TextPropertyChanged ); public string Text { get { return (string)base.GetValue(TextProperty); } set { base.SetValue(TextProperty, value); } } //Show the read more label if word length > 100 private static void TextPropertyChanged(BindableObject bindable, object oldValue, object newValue) { var control = (CustomLabel)bindable; if (newValue != null) { control.lblReadMore.IsVisible= false; control.customLabel.Text = (string)newValue; if (control.customLabel.Text.Split().Length>100) { control.ShortTextVisible = true; control.ReadMoreLabel = true; } } } #endregion public bool ReadMoreLabel { get; set; } private bool _shortTextVisible; public bool ShortTextVisible { get => _shortTextVisible; set { _shortTextVisible = value; ShortTextPropertyChanged(); } } //By Default show first 30 words. private void ShortTextPropertyChanged() { if (Text != null) { if (ShortTextVisible) { customLabel.Text = string.Join(" ",Text.Split().Take(30)); lblReadMore.Text = "Read More"; lblReadMore.IsVisible = true; } else { customLabel.Text = Text; lblReadMore.Text = "Read Less"; } } } private void TapGestureRecognizer_Tapped(object sender, EventArgs e) { ShortTextVisible = !ShortTextVisible; } } }
Implementation of Custom Control
Add the CustomLabel custom control on your content page.
MainPage.xaml
<?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" xmlns:local="clr-namespace:TestApp.CustomControl" mc:Ignorable="d" x:Class="TestApp.MainPage"> <StackLayout Margin="10"> <!-- Place new controls here --> <local:CustomLabel Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." /> </StackLayout> </ContentPage>
Subscribe to:
Posts (Atom)
Popular Posts
-
RatingBar Control- is a cross-platform plugin used for selecting the rating. How To Use Available on NuGet: https://www.nuget.org/...
-
<Label Text = "{Binding Date,StringFormat='{0:MM/dd/yy}'}" /> Output: 05/25/2019 Date Formats: DateTime....
-
Creation Of Certificates & Profiles Certificate Create Development Certificate In Your Apple Developer Account. Once Certificated Creat...
