Estou criando um aplicativo móvel em que o usuário possa cadastrar x ingredientes e posteriormente possa cadastrar produtos escolhendo quais dos ingredientes cadastrados tal produto usa. Nesta perspectiva:
Dúvida 1: Estou tentando popular um Picker tendo como referência os dados da tabela ingredientes preenchida pelo usuário, mas estou travado na hora de digitar a parte de conexão e adaptação da table... Como posso fazer isso?
Dúvida 2: O Picker permite ao usuário selecionar apenas uma opção, ao que parece... Há forma de fazer selecionar múltiplas opções? Ou talvez alguma ferramenta substituta (nesse caso, precisaria de saber como popular essa tal ferramenta com os dados da tabela).
Classe Picker: 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 que implementa o 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 que se encontra a table: https://pastebin.com/v66JJSJt
[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");
}
}
}