Finalized Maui-series for vs development

This commit is contained in:
2025-08-21 22:08:06 +02:00
parent 543b9c2aaf
commit 8b7afd50a4
10 changed files with 95 additions and 13 deletions

View File

@ -8,6 +8,7 @@
// Register routes for navigation
Routing.RegisterRoute(nameof(Views.UserDetailView), typeof(Views.UserDetailView));
Routing.RegisterRoute(nameof(Views.ProductDetailView), typeof(Views.ProductDetailView));
}
}

View File

@ -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<int>(async (id) => await EditAsync(id), (id) => id > 0);
}
public async Task<bool> 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();

View File

@ -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<int>(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<bool> SaveAsync()
{
var ret = base.Save();
if (ret)
{
await Shell.Current.GoToAsync("..");
}
return ret;
}
}

View File

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:partial="clr-namespace:AdventureWorks.MAUI.ViewsPartial"
xmlns:vm="clr-namespace:AdventureWorks.MAUI.CommandClasses"
x:Class="AdventureWorks.MAUI.Views.ProductDetailView"
xmlns:partial="clr-namespace:AdventureWorks.MAUI.ViewsPartial"
x:DataType="vm:ProductViewModelCommands"
Title="Product Information">
<Border Style="{StaticResource Border.Page}">

View File

@ -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);
}
}

View File

@ -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">
<Border Style="{StaticResource Screen.Border}">
<Grid>
@ -36,7 +37,9 @@
<Label Text="{Binding ListPrice, StringFormat='{0:C}'}" />
</HorizontalStackLayout>
<HorizontalStackLayout>
<Button Text="Edit" />
<Button Text="Edit"
CommandParameter="{Binding ProductID}"
Command="{Binding Source={x:Reference ProductListPage}, Path=BindingContext.EditCommand}"/>
<Button Text="Delete" />
</HorizontalStackLayout>
</VerticalStackLayout>

View File

@ -1,7 +1,7 @@
using AdventureWorks.MAUI.CommandClasses;
namespace AdventureWorks.MAUI.Views;
[QueryProperty(nameof(UserId), "id")]
public partial class UserDetailView : ContentPage
{
public UserDetailView(UserViewModelCommands viewModel)
@ -11,6 +11,7 @@ public partial class UserDetailView : ContentPage
}
public UserViewModelCommands ViewModel { get; set; }
public int UserId { get; set; }
protected override void OnAppearing()
{
@ -20,7 +21,7 @@ public partial class UserDetailView : ContentPage
ViewModel.GetPhoneTypes();
ViewModel.Get(1); // Assuming you want to get the user with ID 1
ViewModel.Get(UserId);
}
}

View File

@ -6,7 +6,7 @@
xmlns:model="clr-namespace:AdventureWorks.EntityLayer;assembly=AdventureWorks.EntityLayer"
x:Class="AdventureWorks.MAUI.Views.UserListView"
x:DataType="vm:UserViewModelCommands"
x:Name="UserListPage"
Title="User List">
<Border Style="{StaticResource Screen.Border}">
@ -35,7 +35,9 @@
<Label Text="{Binding Email}" />
</HorizontalStackLayout>
<HorizontalStackLayout>
<Button Text="Edit" />
<Button Text="Edit"
CommandParameter="{Binding UserId}"
Command="{Binding Source={x:Reference UserListPage}, Path=BindingContext.EditCommand}" />
<Button Text="Delete" />
</HorizontalStackLayout>
</VerticalStackLayout>