Questions about Picker in Xamarin.Forms

Asked

Viewed 192 times

0

I am creating a mobile application where the user can register x ingredients and later can register products choosing which of the registered ingredients such product uses. In this perspective:

Doubt 1: I’m trying to popular a Picker taking as reference the data table ingredients filled by the user, but I’m stuck at the time of typing the connection part and table adaptation... How can I do that?

Doubt 2: Picker allows the user to select only one option, it seems... Is there any way to select multiple options? Or maybe some replacement tool (in this case, you need to know how popular this tool with table data).

Picker class: https://pastebin.com/2iFZWbhs

public class PickerMVVMViewModel
{
    public List<listIng> listarIng { get; set; }

    public PickerMVVMViewModel()
    {
        listarIng = GetIngs().OrderBy(t => t.Value).ToList();
    }

    public List<listIng> GetIngs()
    {
        var ing = new List<listIng>()
        {   // essa parte supostamente receberia os dados da tabela, mas fiz esses itens para checar o funcionamento.
            new listIng(){Key = 1, Value= "Pão"},
            new listIng(){Key = 2, Value= "Carne"},
            new listIng(){Key = 3, Value= "Ovo"},
            new listIng(){Key = 4, Value= "Presunto"},
            new listIng(){Key = 5, Value= "Queijo"},
            new listIng(){Key = 6, Value= "Bacon"}
        };
        return ing;
    }
}


public class listIng
{
    public int Key { get; set; }
    public string Value { get; set; }
}  

XAML implementing the Picker: https://pastebin.com/pPj64c7t

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="OrganizeSe.cadProduto">

    <StackLayout Margin="10,10,10,0">
        <Entry  Placeholder="Insira o nome do produto"                
                FontSize="20"/>

        <Label Text="Selecione os ingredientes:"
               FontSize="20"
               TranslationX="5"
               TranslationY="15"/>

        <Picker Title="--Selecione--"
                ItemsSource="{Binding listarIng}"
                ItemDisplayBinding="{Binding Value}"/>
    </StackLayout>
</ContentPage> 

Content found in table: https://pastebin.com/v66JSJt

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NewIngPage : ContentPage
    {
        public NewIngPage ()
        {
            InitializeComponent ();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Ingredientes ing = new Ingredientes()
            {
                Nome = nameEntry.Text,
                Preco = precoEntry.Text,
                Qnt = qntEntry.Text
            };

            using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
            {
                conn.CreateTable<Ingredientes>();
                var numberOfRows = conn.Insert(ing);

                if (numberOfRows>0)                
                    DisplayAlert("Salvo!", "O ingrediente foi salvo com sucesso.", "Ok");
                else
                    DisplayAlert("Falha!", "O ingrediente NÃO foi salvo.", "Ok");
            }                        
        }
    }
  • 1

    Stop posting links and images to your code, include the relevant parts in your question

  • Doesn’t putting all the code make it easier to understand? After all, you are seeing as a whole the structure of what I am doing. Also for this reason, upgrading in Pastebin becomes convenient.

  • 1

    Try to be more specific in your question by researching, making your own attempts until you reach a concrete error. If we need to see ALL its structure to understand the problem is because your question is not yet clear and very broad.

  • That’s it, it’s not a mistake or a problem, it’s just something I want to do, but I don’t know how, and so I wanted to give an explanation of what I want and where I want so that someone can explain me more accurately, but I understand what you meant based on my previous posts. I will do as explained.

  • 1

    But this is not the purpose of this site, look for examples on the internet and do the exercise of understanding exactly what your question is. See that the crux of the matter has very little to do with Xamarin, android or the Picker component itself, it comes to how to make a query in Sqlite. Research on this and resolve this issue first.

No answers

Browser other questions tagged

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