Hola estoy tratando de hacer un custome tool bar para Xamarin Forms , pero no logro ver mi clase renderizada. Alguien sabe que estoy haciendo mal? Me da este error en el Image Source
System.InvalidCastException: Specified cast is not valid.
Como pueden ver tampo veo el texto aunque ya existe
Este es mi implementation class:
public class MainPage : ContentPage
{
StackLayout mainLayout = new StackLayout();
Button btn1;
Label lbl1;
MainViewModel viewModel;
ToolBarCustome tbcustome = new ToolBarCustome();
public MainPage()
{
BindingContext = viewModel = new MainViewModel();
viewModel.Title = "Main Page";
Title = viewModel.Title;
tbcustome.TextProp = Title;
tbcustome.ImageProp = FileImageSource.FromFile("ino50.png");
SetUpUI();
}
private void SetUpUI()
{
NavigationPage.SetTitleView(this, tbcustome);
btn1 = new Button();
lbl1 = new Label();
btn1.Text = "click me 💀";
lbl1.HorizontalOptions = LayoutOptions.Center;
lbl1.Text = "Hi, I'm hardcoded, that is a bad practice";
btn1.Clicked += GoBack;
mainLayout.Children.Add(lbl1);
mainLayout.Children.Add(btn1);
mainLayout.Padding = 30;
this.Content = mainLayout;
}
private void GoBack(object sender, EventArgs e)
{
Navigation.PopAsync();
}
}
Y aqui donde defino mi tool bar:
public class ToolBarCustome : ContentView
{
public static BindableProperty TitleTextProp =
BindableProperty.Create
(
nameof(TextProp),
typeof(string),
typeof(ToolBarCustome),
defaultValue: string.Empty,
defaultBindingMode: BindingMode.OneWay,
propertyChanged: TitleTextPropChanged
);
private static void TitleTextPropChanged(BindableObject bindable,
object oldValue, object newValue)
{
var control = (ToolBarCustome)bindable;
control.TextProp = newValue?.ToString();
}
public string TextProp
{
get { return base.GetValue(TitleTextProp)?.ToString(); }
set { base.SetValue(TitleTextProp, value); }
}
public static readonly BindableProperty ImageSourceProperty =
BindableProperty.Create(
nameof(ImageProp),
typeof(ImageSource),
typeof(ToolBarCustome),
defaultValue: null,
defaultBindingMode: BindingMode.OneWay
);
public ImageSource ImageProp
{
get { return (string)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
Image image = new Image();
Label label = new Label();
StackLayout layout = new StackLayout();
public ToolBarCustome()
{
layout.Orientation = StackOrientation.Horizontal;
image.Source = ImageProp;
label.Text = TextProp;
layout.Children.Add(image);
layout.Children.Add(label);
}
}