Pass multiple attributes to a Command Parameter in Xamarin Forms

Asked

Viewed 168 times

0

I have a function that is a List, I have a button and I wanted to pass 2 parameters from this list to this button to redirect to the next screen, but I do not know how to do this, because Commandparameter only accepts one value.

    public void MontaGrid(List<FormularioCompartilhado> forms)
    {
        foreach (var item in forms)
        {
            var frame = new Frame() { BorderColor = Color.Silver };
            var framePorcent = new Frame() { BackgroundColor = Color.FromHex("eeeeee"), BorderColor = Color.Silver };
            var stacklayout = new StackLayout() { Margin = new Thickness(0, 0, 0, 0), Padding = 10, BackgroundColor = Color.FromHex("eeeeee") };
            var stacklayoutFramePorcent = new StackLayout() { };
            var grid = new Grid() { };

            var labelNome = new Label() { Text = "Nome: ", FontAttributes = FontAttributes.Bold, FontSize = 16 };
            var labelEmpresa = new Label() { Text = "Empresa: ", FontAttributes = FontAttributes.Bold, FontSize = 16 };
            var labelResult = new Label() { Text = "Resultado: ", FontAttributes = FontAttributes.Bold, FontSize = 16 };
            var labelResp = new Label() { Text = "Respondido: ", FontAttributes = FontAttributes.Bold, FontSize = 16 };

            var listaNome = new Label() { Text = $"{(item.Titulo != null ? item.Titulo : item.Hash )} ({ item.FormularioXEmpresa.Formulario.Nome })" };
            var listaEmpresa = new Label() { Text = item.FormularioXEmpresa.Empresas.Nome };
            var listaPorcentResult = new Label() { Text = item.Atingimento.ToString() + "%" };
            var listaPorcentResp = new Label() { Text = item.Respondido.ToString() + "%" };

            if (item.Permitirresponder == true)
            {
                var btnResponder = new Button()
                {
                    Text = "Responder",
                    TextColor = Color.White,
                    BackgroundColor = Color.FromHex("17A2B8"),
                    CommandParameter = item.Hash, item.FormularioXEmpresa.Id;
                };
                btnResponder.Clicked += BtnRespondeForm_Clicked;

                grid.Children.Add(labelResp, 0, 0);
                grid.Children.Add(listaPorcentResp, 0, 1);
                grid.Children.Add(btnResponder, 0, 2);
            }

            framePorcent.Content = grid;
            stacklayoutFramePorcent.Children.Add(framePorcent);

            stacklayout.Children.Add(labelNome);
            stacklayout.Children.Add(listaNome);
            stacklayout.Children.Add(labelEmpresa);
            stacklayout.Children.Add(listaEmpresa);
            stacklayout.Children.Add(stacklayoutFramePorcent);

            frame.Content = stacklayout;
            BlocosLayout.Children.Add(frame);
        }
    }

No btnResponder I want to pass the command for that boot method:

    private async void BtnRespondeForm_Clicked(object sender, EventArgs e)
    {
        var formularioCompartilhado = (FormularioCompartilhado)(sender as Button).CommandParameter;

        await Navigation.PushAsync(new ResponderFormularioPage(
            formularioCompartilhado.FormularioXEmpresa.Id,
            formularioCompartilhado.Hash, 
            IsOnline,true));
    }

1 answer

0


I would strongly recommend a MVVM and bindings to do this kind of operation. But, for example, you can pass the entire object in the CommandParameter, just as you are trying to recover in the event.

Behold:

var btnResponder = new Button()
    {
        Text = "Responder",
        TextColor = Color.White,
        BackgroundColor = Color.FromHex("17A2B8"),
        CommandParameter = item
    };

Inside Event Handler you recover the properties of the object you are interested in, as you are already doing:

var formularioCompartilhado = (FormularioCompartilhado)(sender as Button).CommandParameter;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.