.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,919 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
builder.Services.AddSingleton<MainPageViewModel>();
public class MainPageViewModel : BaseViewModel
{
private Plato currentPlato;
public Plato CurrentPlato
{
get => currentPlato;
set
{
if (currentPlato != value)
{
currentPlato = value;
OnPropertyChanged();
}
}
}
public ObservableCollection<Plato> PlatoList { get; }
public Plato PlatoSeleccionado { get; set; }
public List<Plato> Platos { get; private set; }
public ICommand SelectionChanged { get; private set; }
public MainPageViewModel(PlatoService platoService)
{
PlatoList = new ObservableCollection<Plato>(platoService.GetPlatos());
SelectionChanged = new Command(
async () =>
{
if (CurrentPlato != null)
await Shell.Current.GoToAsync(nameof(DetallePage), true, new Dictionary<string, object> { { "Plato", CurrentPlato } });
});
}
}
DetallePage
[QueryProperty(nameof(Plato), "Plato")]
public class DetallePageViewModel : BaseViewModel
{
private Plato plato;
public Plato Plato
{
get => plato;
set
{
plato = value;
OnPropertyChanged();
}
}
public ICommand BackCommand { get; private set; }
public DetallePageViewModel()
{
BackCommand = new Command(
async () => await Shell.Current.Navigation.PopAsync());
}
}
<VerticalStackLayout>
<CollectionView ItemsSource="{Binding PlatoList}" SelectionMode="Single"
SelectionChangedCommand="{Binding SelectionChanged}"
SelectedItem="{Binding CurrentPlato}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Plato">
<VerticalStackLayout Spacing="10" Margin="10" Padding="10" BackgroundColor="Transparent">
<Label FontSize="Medium" Text="{Binding Nombre}" />
<Label FontSize="Medium" Text="{Binding Precio, StringFormat='{0:C}'}" />
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage(MainPageViewModel mainPageViewModel)
{
InitializeComponent();
this.BindingContext = mainPageViewModel;
}
}
AppShell.xaml.cs
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute(nameof(DetallePage), typeof(DetallePage));
}