Skip to content

Instantly share code, notes, and snippets.

@robinmanuelthiel
Created February 16, 2017 14:36
Show Gist options
  • Save robinmanuelthiel/c9eddda587dd59c8a6984a986f8fd691 to your computer and use it in GitHub Desktop.
Save robinmanuelthiel/c9eddda587dd59c8a6984a986f8fd691 to your computer and use it in GitHub Desktop.
Custom Xamarin.Forms Menu Item
<?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:ZeissCantinaMenuSample"
x:Class="ZeissCantinaMenuSample.ZeissCantinaMenuSamplePage">
<local:MenuItem x:Name="TestMenuItem" Text="Hallo" Image="AlertIcon.png" Clicked="TestMenuItem_Clicked" />
</ContentPage>
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ZeissCantinaMenuSample.MenuItem">
<ContentView.Content>
<Grid>
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="Handle_Tapped"/>
</Grid.GestureRecognizers>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image HeightRequest="50" Grid.Row="1" Source="{Binding Image}" />
<Label Grid.Row="2" x:Name="MenuText" HorizontalOptions="Center" Text="{Binding Text}" />
</Grid>
</ContentView.Content>
</ContentView>
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace ZeissCantinaMenuSample
{
public partial class MenuItem : ContentView
{
public static readonly BindableProperty ImageProperty = BindableProperty.Create(
propertyName: "Image",
returnType: typeof(ImageSource),
declaringType: typeof(MenuItem),
defaultValue: null);
public static readonly BindableProperty TextProperty = BindableProperty.Create(
propertyName: "Text",
returnType: typeof(string),
declaringType: typeof(MenuItem),
defaultValue: null);
public ImageSource Image
{
get { return (ImageSource)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public event EventHandler Clicked;
public MenuItem()
{
InitializeComponent();
BindingContext = this;
}
void Handle_Tapped(object sender, System.EventArgs e)
{
Clicked(this, e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment