Hi,
I dabbled with Xamarin in the past, about 4 years ago, but for the sake of this post, lets say I'm a total newb, which I essentially am.
I am trying to create a page in which I want the same set of views rendered multiple times. So I decided to create a composite control. Mine is very simple, pretty much just a grouping of a label, a progressbar, a button, and another label. I named the control FractionProgressbar. Probably not the best name for it, but let's worry about that another time.
I seem to be missing some vital piece of fundamental knowledge. My composite control has three BindableProperty-members: Label, Total and Current. The only one I manage to get working is Label, Total and Current is always 0 (their default value) no matter what I set them to in the MainPage.xaml or MainPage.xaml.cs.
If anyone has any insight here, Id be incredibly grateful!
I've searched for information about this on the forum but either I can't find it, or I found it but didn't realize it pertained to my issue.
P.S. I am not allowed to post links to I have replaced the schema links in the xaml-files with [LINK TO SCHEMA]...
FractionProgressbar.xaml.cs
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace EcoEater.Controls
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class FractionProgressbar : ContentView
{
public static readonly BindableProperty LabelProperty = BindableProperty.Create(
propertyName: "Label",
returnType: typeof(string),
declaringType: typeof(FractionProgressbar),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay
);
public static readonly BindableProperty TotalProperty = BindableProperty.Create(
propertyName: "Total",
returnType: typeof(int),
declaringType: typeof(FractionProgressbar),
defaultValue: 0,
defaultBindingMode: BindingMode.TwoWay
);
public static readonly BindableProperty CurrentProperty = BindableProperty.Create(
propertyName: "Current",
returnType: typeof(int),
declaringType: typeof(FractionProgressbar),
defaultValue: 0,
defaultBindingMode: BindingMode.TwoWay
);
public string Label
{
get
{
return (string)GetValue(LabelProperty);
}
set
{
SetValue(LabelProperty, value);
}
}
public int Total
{
get
{
return (int)GetValue(TotalProperty);
}
set
{
SetValue(TotalProperty, value);
}
}
public int Current
{
get
{
return (int)GetValue(CurrentProperty);
}
set
{
SetValue(CurrentProperty, value);
}
}
public double Percentage
{
get
{
return (double)Current / (double)Total;
}
}
public string FractionLabel
{
get
{
return $"{Current} out of {Total}";
}
}
public FractionProgressbar ()
{
InitializeComponent ();
BindingContext = this;
}
}
}
FractionProgressbar.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="[LINK TO SCHEMA]"
xmlns:x="[LINK TO SCHEMA]"
x:Class="EcoEater.Controls.FractionProgressbar">
<ContentView.Content>
<StackLayout>
<Label Text="{Binding Label}" />
<ProgressBar Progress="{Binding Percentage}" />
<Button x:Name="plusButton"
Text="+"
FontSize="Medium"
FontAttributes="Bold" />
<Label Text="{Binding FractionLabel}" />
</StackLayout>
</ContentView.Content>
</ContentView>
And the way I use the control is in my MainPage
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="[LINK TO SCHEMA]"
xmlns:x="[LINK TO SCHEMA]"
xmlns:views="clr-namespace:EcoEater.Views"
xmlns:controls="clr-namespace:EcoEater.Controls"
x:Class="EcoEater.Views.MainPage">
<StackLayout>
<controls:FractionProgressbar
Label="Test"
Total="14"
Current="6"/>
</StackLayout>
</ContentPage>
Mainpage.xaml.cs
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace EcoEater.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
public MainPage(User user)
{
InitializeComponent();
BindingContext = this;
}
}
}
Result
The label is correct ("Test") however both the progressbar and the last label are both zeroed, even though I set Total to 14 and Current to 6 when I consume the control.
Tried posting an image of the result, apparently I am not allowed to : (