1

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.

introducir la descripción de la imagen aquí

Como pueden ver tampo veo el texto aunque ya existe

introducir la descripción de la imagen aquí

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);                       
    }
}

1 respuesta 1

0

Ok asi lo resolvi, de momento utilice un xaml.cs

Parece ser que me faltaba arreglar el binding, utilizando el x:Name , hasta donde he encontrado el x:Name no esta disponible como tal en puro .cs , podemos usar el Id prop, pero no he logrado conseguirlo hasta con el perfomance del xaml.

introducir la descripción de la imagen aquí

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    x:Class="animations.Views.CustomViews.ToolBarCustomeXaml"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentView.Content>
        <StackLayout Orientation="Horizontal">
            <Label x:Name="Lable1" />
            <Image x:Name="Image1" />
        </StackLayout>
    </ContentView.Content>
</ContentView>

Y Aqui la implementacion del .cs

 public partial class ToolBarCustomeXaml : ContentView
{
    public string TitleText
    {
        get
        {
            return base.GetValue(TitleTextProperty)?.ToString();
        }

        set
        {
            base.SetValue(TitleTextProperty, value);
        }
    }
    public static readonly BindableProperty TitleTextProperty = BindableProperty.Create(nameof(TitleText),
      typeof(string),
      typeof(ToolBarCustomeXaml),
      defaultValue: string.Empty,
      defaultBindingMode: BindingMode.OneWay,
      propertyChanged: TitleTextPropertyChanged);

    private static void TitleTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (ToolBarCustomeXaml)bindable;
        control.Lable1.Text = newValue?.ToString(); //Label1 es parte del xaml es el x:Name
    }

    public ImageSource ImageSource
    {
        get { return GetValue(ImageSourceProperty) as ImageSource; }
        set { SetValue(ImageSourceProperty, value); }
    }
    public static readonly BindableProperty ImageSourceProperty = BindableProperty.Create(nameof(ImageSource),
      typeof(ImageSource),
      typeof(ToolBarCustomeXaml),
      defaultValue: null,
      defaultBindingMode: BindingMode.OneWay,
      propertyChanged: ImageSourcePropertyChanged);

    private static void ImageSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (ToolBarCustomeXaml)bindable;
        control.Image1.Source = (ImageSource)newValue; //Image1 es parte del xaml
    }

    public ToolBarCustomeXaml()
    {
        InitializeComponent();
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.