Skip to content

Instantly share code, notes, and snippets.

@pkozak2
Created November 30, 2020 14:18
Show Gist options
  • Save pkozak2/90a27d547148482eb1ba1c8f1a75ae62 to your computer and use it in GitHub Desktop.
Save pkozak2/90a27d547148482eb1ba1c8f1a75ae62 to your computer and use it in GitHub Desktop.
ContentProperty
<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="MyApp.Controls.LabeledView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentView.Content>
<StackLayout>
<Label x:Name="label" />
<ContentView x:Name="mainContent" />
</StackLayout>
</ContentView.Content>
</ContentView>
using System.Runtime.CompilerServices;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MyApp.Controls
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[ContentProperty(nameof(MainContent))]
public partial class LabeledView : ContentView
{
public static readonly BindableProperty MainContentProperty
= BindableProperty.Create(nameof(MainContent), typeof(View), typeof(LabeledView), default(View));
public static readonly BindableProperty LabelTextProperty
= BindableProperty.Create(nameof(LabelText), typeof(string), typeof(LabeledView), default(string));
public static readonly BindableProperty LabelHorizontalOptionsProperty
= BindableProperty.Create(nameof(LabelHorizontalOptions), typeof(LayoutOptions), typeof(LabeledView), LayoutOptions.Fill);
public static readonly BindableProperty LabelColorProperty
= BindableProperty.Create(nameof(LabelColor), typeof(Color), typeof(LabeledView), default(Color));
public static readonly BindableProperty LabelSizeProperty
= BindableProperty.Create(nameof(LabelSize), typeof(NamedSize), typeof(LabeledView), default(NamedSize));
public View MainContent
{
get => (View)GetValue(MainContentProperty);
set => SetValue(MainContentProperty, value);
}
public string LabelText
{
get => (string)GetValue(LabelTextProperty);
set => SetValue(LabelTextProperty, value);
}
public LayoutOptions LabelHorizontalOptions
{
get => (LayoutOptions)GetValue(LabelHorizontalOptionsProperty);
set => SetValue(LabelHorizontalOptionsProperty, value);
}
public Color LabelColor
{
get => (Color)GetValue(LabelColorProperty);
set => SetValue(LabelColorProperty, value);
}
public NamedSize LabelSize
{
get => (NamedSize)GetValue(LabelSizeProperty);
set => SetValue(LabelSizeProperty, value);
}
public LabeledView()
{
InitializeComponent();
label.HorizontalOptions = HorizontalOptions;
label.TextColor = LabelColor;
label.FontSize = GetFontSize();
}
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == nameof(MainContent))
{
mainContent.Content = MainContent;
}
if (propertyName == nameof(LabelText))
{
label.Text = LabelText;
}
if (propertyName == nameof(LabelHorizontalOptions))
{
label.HorizontalOptions = LabelHorizontalOptions;
}
if (propertyName == nameof(LabelColor))
{
label.TextColor = LabelColor;
}
if(propertyName == nameof(LabelSize))
{
label.FontSize = GetFontSize();
}
}
private double GetFontSize()
{
FontSizeConverter myFontSizeConverter = new FontSizeConverter();
return (double)myFontSizeConverter.ConvertFromInvariantString(LabelSize.ToString());
}
}
}
<ContentPage
x:Class="MyPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:MyApp.Controls"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Shell.NavBarIsVisible="False"
mc:Ignorable="d">
<ContentPage.Content>
<StackLayout Padding="10,0,10,0" VerticalOptions="Center">
<controls:LabeledView
LabelColor="Red"
LabelHorizontalOptions="Start"
LabelText="testowy label">
<Entry />
</controls:LabeledView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment