From 8b7afd50a4f57078ffcbadc872a12725e5061d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 21 Aug 2025 22:08:06 +0200 Subject: [PATCH] Finalized Maui-series for vs development --- AdventureWorks.MAUI/AppShell.xaml.cs | 1 + .../ProductViewModelCommands.cs | 39 +++++++++++++++++++ .../CommandClasses/UserViewModelCommands.cs | 20 +++++++++- .../Views/ProductDetailView.xaml | 4 +- .../Views/ProductDetailView.xaml.cs | 23 +++++++++-- .../Views/ProductListView.xaml | 5 ++- .../Views/UserDetailView.xaml.cs | 5 ++- AdventureWorks.MAUI/Views/UserListView.xaml | 6 ++- .../ViewModelClasses/ProductViewModel.cs | 3 +- .../ViewModelClasses/UserViewModel.cs | 2 +- 10 files changed, 95 insertions(+), 13 deletions(-) diff --git a/AdventureWorks.MAUI/AppShell.xaml.cs b/AdventureWorks.MAUI/AppShell.xaml.cs index 7e4e4d3..3d7b441 100644 --- a/AdventureWorks.MAUI/AppShell.xaml.cs +++ b/AdventureWorks.MAUI/AppShell.xaml.cs @@ -8,6 +8,7 @@ // Register routes for navigation Routing.RegisterRoute(nameof(Views.UserDetailView), typeof(Views.UserDetailView)); + Routing.RegisterRoute(nameof(Views.ProductDetailView), typeof(Views.ProductDetailView)); } } diff --git a/AdventureWorks.MAUI/CommandClasses/ProductViewModelCommands.cs b/AdventureWorks.MAUI/CommandClasses/ProductViewModelCommands.cs index e8b42cc..d173c93 100644 --- a/AdventureWorks.MAUI/CommandClasses/ProductViewModelCommands.cs +++ b/AdventureWorks.MAUI/CommandClasses/ProductViewModelCommands.cs @@ -1,6 +1,7 @@ using AdventureWorks.EntityLayer; using AdventureWorks.ViewModelLayer.ViewModelClasses; using Common.Library; +using System.Windows.Input; namespace AdventureWorks.MAUI.CommandClasses { @@ -14,6 +15,44 @@ namespace AdventureWorks.MAUI.CommandClasses { } #endregion + + public ICommand SaveCommand { get; private set; } + public ICommand EditCommand { get; private set; } + + private bool _IsSaveCommandEnabled = true; + public bool IsSaveCommandEnabled + { + get { return _IsSaveCommandEnabled; } + set + { + _IsSaveCommandEnabled = value; + RaisePropertyChanged(nameof(IsSaveCommandEnabled)); + } + } + + public override void Init() + { + base.Init(); + SaveCommand = new Command(async () => await SaveAsync(), () => IsSaveCommandEnabled); + EditCommand = new Command(async (id) => await EditAsync(id), (id) => id > 0); + } + + public async Task SaveAsync() + { + var ret = base.Save(); + if (ret) + { + await Shell.Current.GoToAsync(".."); + } + + return ret; + } + + protected async Task EditAsync(int id) + { + await Shell.Current.GoToAsync($"{nameof(Views.ProductDetailView)}?id={id}"); + } + public void LoadProducts() { Get(); diff --git a/AdventureWorks.MAUI/CommandClasses/UserViewModelCommands.cs b/AdventureWorks.MAUI/CommandClasses/UserViewModelCommands.cs index 78d693b..932a10d 100644 --- a/AdventureWorks.MAUI/CommandClasses/UserViewModelCommands.cs +++ b/AdventureWorks.MAUI/CommandClasses/UserViewModelCommands.cs @@ -38,16 +38,34 @@ public class UserViewModelCommands : UserViewModel #region Commands public ICommand SaveCommand { get; private set; } + public ICommand EditCommand { get; private set; } #endregion #region Init Method public override void Init() { base.Init(); - SaveCommand = new Command(() => Save(), () => IsSaveCommandEnabled); + SaveCommand = new Command(async () => SaveAsync(), () => IsSaveCommandEnabled); + EditCommand = new Command(async (id) => await EditAsync(id), (id) => id > 0); } #endregion + protected async Task EditAsync(int id) + { + await Shell.Current.GoToAsync($"{nameof(Views.UserDetailView)}?id={id}"); + } + + public async Task SaveAsync() + { + var ret = base.Save(); + if (ret) + { + await Shell.Current.GoToAsync(".."); + } + + return ret; + } + } diff --git a/AdventureWorks.MAUI/Views/ProductDetailView.xaml b/AdventureWorks.MAUI/Views/ProductDetailView.xaml index 63bb122..ed4bbdf 100644 --- a/AdventureWorks.MAUI/Views/ProductDetailView.xaml +++ b/AdventureWorks.MAUI/Views/ProductDetailView.xaml @@ -1,8 +1,10 @@ diff --git a/AdventureWorks.MAUI/Views/ProductDetailView.xaml.cs b/AdventureWorks.MAUI/Views/ProductDetailView.xaml.cs index 3bcca9d..629e55b 100644 --- a/AdventureWorks.MAUI/Views/ProductDetailView.xaml.cs +++ b/AdventureWorks.MAUI/Views/ProductDetailView.xaml.cs @@ -1,9 +1,24 @@ +using AdventureWorks.MAUI.CommandClasses; namespace AdventureWorks.MAUI.Views; +[QueryProperty(nameof(ProductId), "id")] public partial class ProductDetailView : ContentPage { - public ProductDetailView() - { - InitializeComponent(); - } + public ProductDetailView(ProductViewModelCommands viewModel) + { + InitializeComponent(); + ViewModel = viewModel; + } + + public ProductViewModelCommands ViewModel { get; set; } + public int ProductId { get; set; } + + protected override void OnAppearing() + { + base.OnAppearing(); + BindingContext = ViewModel; + //ViewModel.GetProductCategories(); + //ViewModel.GetProductModels(); + ViewModel.Get(ProductId); + } } \ No newline at end of file diff --git a/AdventureWorks.MAUI/Views/ProductListView.xaml b/AdventureWorks.MAUI/Views/ProductListView.xaml index 1bcdf04..3b9558c 100644 --- a/AdventureWorks.MAUI/Views/ProductListView.xaml +++ b/AdventureWorks.MAUI/Views/ProductListView.xaml @@ -6,6 +6,7 @@ xmlns:vm="clr-namespace:AdventureWorks.MAUI.CommandClasses" xmlns:model="clr-namespace:AdventureWorks.EntityLayer;assembly=AdventureWorks.EntityLayer" x:DataType="vm:ProductViewModelCommands" + x:Name="ProductListPage" Title="ProductListView"> @@ -36,7 +37,9 @@