Created
November 30, 2020 14:18
-
-
Save pkozak2/90a27d547148482eb1ba1c8f1a75ae62 to your computer and use it in GitHub Desktop.
ContentProperty
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
<?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> |
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
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()); | |
} | |
} | |
} |
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
<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