Created
November 2, 2015 22:35
-
-
Save nodoid/f6d30cae317e0d396620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace mynamespace | |
{ | |
using Xamarin.Forms; | |
public class TopBar : View | |
{ | |
private string Title, LeftImage, RightImage; | |
Page currentPage; | |
public TopBar(string text = "", Page current = null, string leftImage = "", string rightImage = "") | |
{ | |
Title = text; | |
LeftImage = leftImage; | |
RightImage = rightImage; | |
currentPage = current; | |
} | |
public Grid CreateTopBar() | |
{ | |
var leftCell = new Image(); | |
if (!string.IsNullOrEmpty(LeftImage)) | |
{ | |
leftCell.Source = LeftImage; | |
leftCell.HeightRequest = leftCell.WidthRequest = 32; | |
leftCell.HorizontalOptions = LayoutOptions.StartAndExpand; | |
var gestLeft = new TapGestureRecognizer | |
{ | |
NumberOfTapsRequired = 1, | |
}; | |
gestLeft.Tapped += async delegate | |
{ | |
await currentPage.Navigation.PopAsync(); | |
}; | |
leftCell.GestureRecognizers.Add(gestLeft); | |
} | |
var rightCell = new Image(); | |
if (!string.IsNullOrEmpty(RightImage)) | |
{ | |
rightCell.Source = RightImage; | |
rightCell.HeightRequest = rightCell.WidthRequest = 32; | |
rightCell.HorizontalOptions = LayoutOptions.EndAndExpand; | |
} | |
dynamic title; | |
if (!string.IsNullOrEmpty(Title)) | |
{ | |
title = new Label | |
{ | |
Font = Font.SystemFontOfSize(22), | |
TextColor = Color.White, | |
XAlign = TextAlignment.Center, | |
YAlign = TextAlignment.Center, | |
MinimumWidthRequest = App.ScreenSize.Width - 64, | |
Text = Title | |
}; | |
} | |
else | |
{ | |
title = new Image | |
{ | |
Source = "logo.png", | |
}; | |
} | |
var grid = new Grid | |
{ | |
VerticalOptions = LayoutOptions.CenterAndExpand, | |
BackgroundColor = App.Self.Green, | |
HeightRequest = 52, | |
MinimumHeightRequest = 52, | |
WidthRequest = App.ScreenSize.Width, | |
}; | |
grid.Children.Add(leftCell, 0, 0); | |
grid.Children.Add(title, 1, 0); | |
grid.Children.Add(rightCell, 2, 0); | |
return grid; | |
} | |
} | |
} | |
Usage | |
var topbar = new TopBar("", this, "icoback.png").CreateTopBar(); | |
or | |
var topbar = new TopBar().CreateTopBar(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment