Hints for programming a Xamarin.Forms application - visual-studio

Sorry for my perhaps silly question. As I am new to Xamarin Forms, I need some basic hints:
I need a window ("secondary window") sliding from left into my main window. In this window, a list of items (with images and text) should be displayed and this window should cover only 1/4 to 1/2 of my main window. Dragging an item from this secondary window to the main window should start some action with this item on main window.
What type of view is best for this purpose and what are the keywords to search for? This looks like a flyout menu but how can I create such view from my main menu or clicking on a button?
I am using C# and Visual Studio 2022

Maybe like this , one page (ContentPage) with a Grid in a Grid.
The Grid MainContentGrid is sliding and the MenuContainer Grid is showing.
Then use Drag and Drop to put the Like Image in MenuContainer on the Drophere Image in MainContentGrid, then an event.
The MainContentGrid is using TranslateTo to slide away and back.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/drag-and-drop
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/animation/simple
The MainPage
<Grid>
<!-- Menu Grid -->
<Grid x:Name="MenuContainer" BackgroundColor="Gray">
<StackLayout
Margin="24,100,0,0"
HorizontalOptions="Start"
Spacing="30">
<Label
FontSize="28"
HorizontalOptions="Center"
Text="MENU Options" />
<Image
HeightRequest="50"
HorizontalOptions="Center"
Source="imglike.png"
VerticalOptions="Center"
WidthRequest="50">
<Image.Clip>
<EllipseGeometry
Center="25,25"
RadiusX="25"
RadiusY="25" />
</Image.Clip>
<Image.GestureRecognizers>
<DragGestureRecognizer />
</Image.GestureRecognizers>
</Image>
</StackLayout>
</Grid>
<!-- Main Content -->
<Grid
x:Name="MainContentGrid"
Padding="24,5,24,0"
BackgroundColor="Red"
ColumnDefinitions="*,Auto"
RowDefinitions="Auto,*">
<!-- Header Text -->
<StackLayout
Grid.Row="0"
Grid.Column="1"
Spacing="4"
VerticalOptions="Center">
<Label Text="Slide Menu" />
</StackLayout>
<!-- Hamburger Pic -->
<Frame
Grid.Row="0"
Grid.Column="0"
BackgroundColor="Red"
BorderColor="Gray"
CornerRadius="28"
HeightRequest="56"
HorizontalOptions="Start"
VerticalOptions="Center"
WidthRequest="56">
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="ProfilePic_Clicked" />
</Frame.GestureRecognizers>
<Image
HeightRequest="50"
HorizontalOptions="Center"
Source="icnhamburger.png"
VerticalOptions="Center"
WidthRequest="50">
<Image.Clip>
<EllipseGeometry
Center="25,25"
RadiusX="25"
RadiusY="25" />
</Image.Clip>
</Image>
</Frame>
<Frame
Grid.Row="1"
Grid.Column="0"
BackgroundColor="Red"
BorderColor="Gray"
CornerRadius="28"
HeightRequest="56"
HorizontalOptions="Start"
VerticalOptions="Center"
WidthRequest="56">
<Image
Aspect="AspectFill"
HeightRequest="50"
HorizontalOptions="Center"
Source="drop.png"
VerticalOptions="Center"
WidthRequest="50">
<Image.GestureRecognizers>
<DropGestureRecognizer Drop="DropGestureRecognizer_Drop" />
</Image.GestureRecognizers>
</Image>
</Frame>
</Grid>
</Grid>
MainPage.cs
public partial class MainPage : ContentPage
{
private const uint AnimationDuration = 500u;
public MainPage()
{
InitializeComponent();
}
private async Task CloseMenu()
{
//Close the menu and bring back back the main content
_ = MainContentGrid.FadeTo(1, AnimationDuration);
_ = MainContentGrid.ScaleTo(1, AnimationDuration);
await MainContentGrid.TranslateTo(0, 0, AnimationDuration, Easing.CubicIn);
}
async void ProfilePic_Clicked(System.Object sender, System.EventArgs e)
{
// Reveal our menu and move the main content out of the view
_ = MainContentGrid.TranslateTo(this.Width * 0.5, this.Height * 0, AnimationDuration, Easing.CubicIn);
await MainContentGrid.ScaleTo(0.8, AnimationDuration);
_ = MainContentGrid.FadeTo(0.8, AnimationDuration);
}
private async void DropGestureRecognizer_Drop(object sender, DropEventArgs e)
{
await CloseMenu();
await DisplayAlert("Job", "I have a job for you to do !", "OK");
}
}
This is how it looks

Related

Xamarin App opens different Page from Navigation.PushAsync() Page

Description
Xamarin application opens different Page upon NavigationPage.Pushaync().
NavigationPage.Asyncpush() is called within a ButtonClicked Method.
That different page used to be the one being pushed but it no longer is.
ViewModel.cs
private async void OnAboutUsClicked()
{
var modifiedContactUs = new ModifiedContactUs() { BackgroundColor = ResourceColourModel.BackgroundColor };
await Application.Current.MainPage.Navigation.PushAsync(modifiedContactUs);
}
ModifiedContactUs.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"
x:Class="Project.Views.Pages.ModifiedContact"
xmlns:mapObject="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps">
<ContentPage.Content>
<StackLayout>
<Grid BackgroundColor="white">
<mapObject:Map x:Name="mapId" HasZoomEnabled="True" MapType="Street">
</mapObject:Map>
</Grid>
</StackLayout>
</ContentPage.Content>
</ContentPage>
ModifiedContactUs.xaml.cs
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Project.Views.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ModifiedContactUs : ContentPage
{
public ModifiedContactUs()
{
InitializeComponent();
}
}
ContactUs.xaml(The different Page)
<?xml version="1.0" encoding="utf-8" ?>
<customControl:CustomContentPage
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:map ="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:ffimageloading="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms"
xmlns:customControl="clr-namespace:Project.CustomControls.Renderers"
xmlns:templates_navbar="clr-namespace:Project.Views.Templates.Menus"
xmlns:fontAwesome="clr-namespace:Project._Utilities"
mc:Ignorable="d"
x:Class="Project.Views.Pages.ContactUs"
>
<ContentPage.Content>
<StackLayout BackgroundColor="Transparent" Spacing="0">
<templates_navbar:NavBar DotMenu_IsVisible="False" BackButton_IsVisible="True"/>
<Grid BackgroundColor="white">
<Grid.RowDefinitions>
<RowDefinition Height=".33*"/>
<RowDefinition Height=".07*"/>
<RowDefinition Height=".6*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".7*"/>
<ColumnDefinition Width=".3*"/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Column="0" Padding="10">
<StackLayout>
<Label
Text="Company Name"
FontAttributes="Bold"
FontSize="Medium"
FontFamily="Lato-Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="StartAndExpand"
/>
<Label
Text="{Binding Address}"
FontFamily="Lato-Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="StartAndExpand"
/>
</StackLayout>
<StackLayout>
<Label
Text="Opening Times"
FontAttributes="Bold"
FontSize="Medium"
FontFamily="Lato-Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="StartAndExpand"
/>
<Label
x:Name="weekdays_lbl"
Text="{Binding Weekday}"
FontFamily="Lato-Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="StartAndExpand"
/>
<Label
x:Name="weekend_lbl"
Text="{Binding Weekend}"
FontFamily="Lato-Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="StartAndExpand"
/>
</StackLayout>
</StackLayout>
<StackLayout Grid.Column="1">
<StackLayout VerticalOptions="CenterAndExpand">
<Button
x:Name="CallUsBtn"
Text="Call Us"
TextColor="White"
BackgroundColor="{StaticResource PrimaryColour}"
FontFamily="Lato-Bold"
FontSize="12"
BorderWidth="2"
BorderColor="White"
Margin="1"
CornerRadius="22"
Command="{Binding PhoneCommand}"
/>
<Button
x:Name="MessageUsBtn"
Text="Message"
TextColor="White"
BackgroundColor="{StaticResource PrimaryColour}"
FontFamily="Lato-Bold"
FontSize="12"
BorderWidth="2"
BorderColor="White"
Margin="1"
CornerRadius="22"
Command="{Binding EmailCommand}"
/>
<Button
x:Name="RequestCallBack"
Text="Request CallBack"
TextColor="White"
BackgroundColor="{StaticResource AccentColour}"
FontFamily="Lato-Bold"
FontSize="11"
BorderWidth="2"
BorderColor="White"
Margin="1"
CornerRadius="22"
Command="{Binding CallbackCommand}"
/>
</StackLayout>
</StackLayout>
</Grid>
<Grid Grid.Row="1" Padding="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".25*"/>
<ColumnDefinition Width=".25*"/>
<ColumnDefinition Width=".25*"/>
<ColumnDefinition Width=".25*"/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Column="0">
<Grid VerticalOptions="CenterAndExpand">
<Image
Source="{FontImage FontFamily={StaticResource MaterialFontFamily},
Glyph={x:Static fontAwesome:IconFonts.Facebook}, Color=#1877F2,Size=60}"
HeightRequest="80"
/>
<Button Clicked="SocialMediaBtnHandler" BackgroundColor="Transparent" Margin="5, 0" CommandParameter="facebook"/>
</Grid>
</StackLayout>
<StackLayout Grid.Column="1">
<Grid VerticalOptions="CenterAndExpand">
<Image
Source="{FontImage FontFamily={StaticResource MaterialFontFamily},
Glyph={x:Static fontAwesome:IconFonts.Twitter}, Color=#00ACED, Size=60}"/>
<Button Clicked="SocialMediaBtnHandler" BackgroundColor="Transparent" Margin="5, 0" CommandParameter="twitter"/>
</Grid>
</StackLayout>
<StackLayout Grid.Column="2">
<Grid VerticalOptions="CenterAndExpand">
<Image
Source="{FontImage FontFamily={StaticResource MaterialFontFamily},
Glyph={x:Static fontAwesome:IconFonts.Linkedin}, Color=#0E76A8, Size=60}"/>
<Button Clicked="SocialMediaBtnHandler" BackgroundColor="Transparent" Margin="5, 0" CommandParameter="linkedIn"/>
</Grid>
</StackLayout>
<StackLayout Grid.Column="3">
<Grid VerticalOptions="CenterAndExpand" Padding="2">
<ffimageloading:SvgCachedImage
Source="social_logo_instagram.png"
HeightRequest="40"/>
<Button Clicked="SocialMediaBtnHandler" BackgroundColor="Transparent" Margin="5, 0" CommandParameter="instagram"/>
</Grid>
</StackLayout>
</Grid>
<Grid Grid.Row="2">
<Grid>
<StackLayout Grid.Row="0" Padding="5">
<map:Map x:Name="mapObject" HasZoomEnabled="True" MapType="Street">
<map:Map.ItemTemplate>
<DataTemplate>
<map:Pin
Position="{Binding Position}"
Address="{Binding Address}"
Label="{Binding PlaceName}"
/>
</DataTemplate>
</map:Map.ItemTemplate>
</map:Map>
</StackLayout>
</Grid>
</Grid>
</Grid>
<AbsoluteLayout BackgroundColor="Red" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="SizeProportional">
</AbsoluteLayout>
</StackLayout>
</ContentPage.Content>
</customControl:CustomContentPage>
ContactUs.xaml.cs (The different page)
using Project._SharedData;
using Project.CustomControls.Renderers;
using Project.Models;
using Project.ViewModels;
using Rg.Plugins.Popup.Services;
using System;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Xaml;
namespace Project.Views.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[System.ComponentModel.DesignTimeVisible(false)]
public partial class ContactUsPage : CustomContentPage
{
ContactUsVM _ContactUsVM;
public ContactUsPage()
{
InitializeComponent();
BindingContext = _ContactUsVM = new ContactUsVM();
initMap();
configureDisplay();
setFontOpeningTime();
}
protected override void OnAppearing()
{
base.OnAppearing();
_ContactUsVM.OnAppearing();
configureDisplay();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
_ContactUsVM.OnDisappearing();
try
{
if (PopupNavigation.Instance != null)
PopupNavigation.Instance.PopAsync(true);
}
catch (Exception) { }
}
private void setFontOpeningTime()
{
DayOfWeek day = DateTime.Now.DayOfWeek;
// Set the font based on whether current day is weekday or weekend
if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
{
weekend_lbl.FontAttributes = FontAttributes.Bold;
}
else
{
weekdays_lbl.FontAttributes = FontAttributes.Bold;
}
}
private void configureDisplay()
{
if (!string.IsNullOrEmpty(SharedPrefs.UserSessionEmail))
{
RequestCallBack.IsVisible = true;
MessageUsBtn.Text = "Email";
}
else
{
RequestCallBack.IsVisible = false;
MessageUsBtn.Text = "Message";
}
}
private async void SocialMediaBtnHandler(object sender, EventArgs e)
{
string socialMediaName = (sender as Button).CommandParameter.ToString().ToLower();
await Launcher.OpenAsync(SocialMediaModel.WebLink(socialMediaName));
}
private void initMap()
{
try
{
double latitude = Convert.ToDouble(SharedData.CompanyDetailsList[0].Latitude);
double longitude = Convert.ToDouble(SharedData.CompanyDetailsList[0].Longitude);
mapObject.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(latitude, longitude), Distance.FromMeters(100)));
mapObject.ItemsSource = SharedData.CompanyDetailsList;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Expected behavior
To navigate to a new Page named ModifiedContactUs
Actual behavior:
Navigates to page called named ContactUs
Basic Information:
Android Build:
-minimum level 21
-Target level 30
Devices:
-Huawei Y6 2019
-API level 29
-Pixel 5
-API level 30
Notes
This question is different to from the first question I asked recently (Where the device crashes). This is behavior happens on all devices I test on except the one from that first question
Expected behavior
To navigate to a new Page named ModifiedContactPage
Actual behavior:
Navigates to page called named ContactUsPage
From your code I saw you just push to ModifiedContactUs not ModifiedContactPage .
var modifiedContactUs = new ModifiedContactUs() { BackgroundColor = ResourceColourModel.BackgroundColor };
await Application.Current.MainPage.Navigation.PushAsync(modifiedContactUs);
Suggestion
Try to add breakpoint to see if it triggers button click event es expected .
Delete the old class and try again .
Delete the app from the device and try again .
This worked: Clone the repo into a new folder as a new project and copy the files and changes to this new repo.
Attempted configuration solutions that did not work: Restarting visual studio. Untick and Tick Debug Mode. Untick and tick Deploy in Configuration Manager under Build. In Android Properties> android Options making sure debugging is checked and debugger is Xamarin.

How to change button color when it is clicked in Xamarin

I am quite new in xamarin and I am trying to add a click events to my buttons. So here are my 4 buttons:
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 1" TextColor="#EEF2FF" FontSize="20" HorizontalOptions="Center" BackgroundColor="#FF5959" />
</StackLayout>
<StackLayout Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 2" Clicked="Handle_Page" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
<StackLayout Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 3" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
<StackLayout Grid.Column="3" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 4" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
So the colour of the buttons are #EEF2FF but when I click on the button I want it to be changed to #FF5959. But if there is another button clicked already, then it should also change to the main colour, #EEF2FF. So we can cay synchronized click event.
I am quite new, so any help would be appreciated.
add a clicked handler
<Button Clicked="ButtonClicked" ... />
then in the handler
protected void ButtonClicked(object sender, EventArgs args)
{
// set the other buttons back to the base color
// you will need to assign each button an x:Name in xaml
btn1.BackgroundColor = Color.FromHex("EEF2FF");
...
btn10.BackgroundColor = Color.FromHex("EEF2FF");
// then change the color of only the selected button
var btn = (Button)sender;
btn.BackgroundColor = Color.FromHex("FF5959");
}
protected override void OnCreate(Bundle savedInstanceState)
{
ScreenClickButton.Click += ChangeColorToRed;
}
private void ChangeColorToRed(object sender, EventArgs e)
{
ScreenClickButton.SetBackgroundColor(color: Color.Red);
}

Xamarin form carouselview scrolls with multiple items for single scroll

Iam using Carousel view control available in Xamarin form(Version 5).
In my case i used Listviews inside carousel view. It works fine if scroll by swiping to left or right, but if i programmatically set the ScrollTo or Position property for the carousel view using button click then it is scrolling multiple times though i'm setting single incremental value.
I added SnapPointsType="MandatorySingle" as mentioned here
One more issue is if my current carousel position is zero and moving to last position or more then one is not working.
<AbsoluteLayout Margin="2" BackgroundColor="Transparent">
<CarouselView
x:Name="MyCarousalView"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
Scrolled="CarouselViewScrolled">
<CarouselView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal" ItemSpacing="0" SnapPointsType="MandatorySingle" />
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<ListView
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
ItemsSource="{Binding .}"
x:FieldModifier="public">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Tapped="ViewCellTapped">
<StackLayout Padding="0">
<StackLayout Padding="20,1" VerticalOptions="StartAndExpand" Spacing="0" >
<Label
FontSize="16"
Text="{Binding Description}"
FontAttributes="Bold"/>
<Label
FontSize="14"
Text="{Binding CreatedDate}" />
</StackLayout>
<Label HorizontalOptions="FillAndExpand" Padding="0" HeightRequest="1" BackgroundColor="#D4D4D4"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</AbsoluteLayout>
Next button click event
private void NextClicked(object sender, EventArgs e)
{
MyCarousalView.ScrollTo((MyCarousalView.Position + 1) < _weekDaysList.Count ? MyCarousalView.Position + 1 : 0);
}
Carousel view binding:
List<List<Note>> _myList;
_myList=getData(); //based on service response
MyCarousalView.ItemsSource = null;
MyCarousalView.ItemsSource = _myList;
Screen recording with issue image
Screen video
Please letme know if any solution/workaround. Thanks

Xamarin - how to make background transparent?

I have a simple page with a black background. I would like to make it transparent so that the page below is visible but blurred.
Someone suggest :
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:NameSpace"
x:Class="NameSpace.MainPage"
BackgroundColor="Transparent"> </ContentPage>
Another one suggest:
<StackLayout VerticalOptions="End" HorizontalOptions="FillAndExpand" BackgroundColor="#80000000" Opacity="0.5" HeightRequest="160" Grid.Row="2" >
So both work on tablets but not on mobile devices. Could someone explain to me why and / or suggest me how to solve it? Thanks in advance
You could use AbsoluteLayout and set the transparent of the "float" view .
<AbsoluteLayout>
<!--below view-->
<StackLayout BackgroundColor="Red" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Button Text="111111" />
</StackLayout>
<!--float view-->
<StackLayout BackgroundColor="Gray" Opacity="0.5" AbsoluteLayout.LayoutBounds="0.5,0,1,0.5" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
</StackLayout>
</AbsoluteLayout>
Update
It is impossible to implement it if use Navigation . Because it is not enough to set the backgroundColor of Page . There are Rendering layers in the ContentPage .
As a workaround , we could simulate a navigation (open a new page) .
<AbsoluteLayout>
<!--below view-->
<StackLayout BackgroundColor="Red" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Button Text="open new page" Clicked="Button_Clicked_1"/>
</StackLayout>
<!--float view-->
<StackLayout x:Name="FloatView" BackgroundColor="Gray" Opacity="0.5" AbsoluteLayout.LayoutBounds="1,1,0.01,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label Text="this is a transparent view" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" />
<Button Text="Back" Clicked="Button_Clicked"/>
</StackLayout>
</AbsoluteLayout>
private void Button_Clicked_1(object sender, EventArgs e)
{
//show
Device.BeginInvokeOnMainThread(async () =>
{
var xPosition = 0;
var currentPosition = 0.9;
while (currentPosition >xPosition)
{
await Task.Delay(1);
currentPosition -= 0.04;
AbsoluteLayout.SetLayoutBounds(FloatView, new Rectangle(currentPosition,0, 1, 1));
}
});
}
private void Button_Clicked(object sender, EventArgs e)
{
//hide
Device.BeginInvokeOnMainThread(async () =>
{
AbsoluteLayout.SetLayoutBounds(FloatView, new Rectangle(1, 0, 0.01, 0.01));
});
}

Is there a way to simulate the clicking of a button?

I'm using this code to create something looking like a button:
<Grid Grid.Column="0" ColumnSpacing="0" Margin="0,0,10,0">
<Frame Grid.Column="0" CornerRadius="5" OutlineColor="Black">
<Label x:Name="faveIconLabel" Style="{StaticResource mediumIcon}" Margin="0,2,0,0" HorizontalOptions="Fill" FontFamily="FontAwesome" VerticalOptions="Center" VerticalTextAlignment="Center" />
</Frame>
<Frame Grid.Column="1" CornerRadius="5" OutlineColor="Black">
<Label x:Name="hiddenIconLabel" Style="{StaticResource mediumIcon}" Margin="0,2,0,0" HorizontalOptions="Fill" FontFamily="FontAwesome" VerticalOptions="Center" VerticalTextAlignment="Center" />
</Frame>
</Grid>
Is there a way that I can simulate the click event to make it look like something actually got pressed when they click on the label?
If you don't want to use a custom renderer, a simple way would be to change the background color in the handleClick, and then revert it back to its original color after a few milliseconds. For example,
private void handleClick(object sender, EventArgs e) {
var view = (View)sender;
view.BackgroundColor = Color.FromHex("#DD000000");
Device.StartTimer(
TimeSpan.FromMilliseconds(100),
() =>
{
// Revert it back to the original color, whatever it may be.
Device.BeginInvokeOnMainThread(() =>
{
view.BackgroundColor = Color.Transparent;
});
return false; // return false to prevent the timer from calling again
});
}
You can add a TapGestureRecognizer to virtually any VisualElement, including Label, Image, etc.
faveIconLabel.GestureRecognizers.Add(new TapGestureRecognizer((view) => OnLabelClicked()));

Resources