I am creating a mobile application in which the user can register x ingredients and later can register products choosing which of the registered ingredients that product uses. In this perspective:
Doubt 1 : I am trying to popular a Picker with reference to the data table of the ingredients filled by the user, but I am stuck at the time of typing the connection part and adaptation of the table ... How can I do that?
Doubt 2 : The Picker allows the user to select only one option, it seems ... Is there a way to make multiple selections? Or maybe some substitute tool (in that case, you would need to know how popular that tool is with the table data).
Picker class: link
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 that implements Picker: link
<?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 at table: link
[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");
}
}
}