Using dependency injection and viewmodel etc

This commit is contained in:
2025-08-20 12:08:46 +02:00
parent 2f053dfc71
commit 1649eaa992
17 changed files with 1143 additions and 31 deletions

View File

@ -65,6 +65,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AdventureWorks.DataLayer\AdventureWorks.DataLayer.csproj" />
<ProjectReference Include="..\AdventureWorks.EntityLayer\AdventureWorks.EntityLayer.csproj" />
<ProjectReference Include="..\AdventureWorks.ViewModelLayer\AdventureWorks.ViewModelLayer.csproj" />
<ProjectReference Include="..\Common.Library\Common.Library.csproj" />

View File

@ -5,11 +5,6 @@
x:Class="AdventureWorks.MAUI.App">
<Application.Resources>
<ResourceDictionary>
<x:Array x:Key="phoneTypes" Type="{x:Type x:String}">
<x:String>Home</x:String>
<x:String>Mobile</x:String>
<x:String>Other</x:String>
</x:Array>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />

View File

@ -1,4 +1,9 @@
using Microsoft.Extensions.Logging;
using AdventureWorks.DataLayer;
using AdventureWorks.EntityLayer;
using AdventureWorks.MAUI.Views;
using AdventureWorks.ViewModelLayer;
using Common.Library;
using Microsoft.Extensions.Logging;
//#if Windows
using Microsoft.Maui.LifecycleEvents;
@ -18,7 +23,12 @@ namespace AdventureWorks.MAUI
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
//DI Services
builder.Services.AddScoped<IRepository<User>, UserRepository>();
builder.Services.AddScoped<IRepository<EntityLayer.Color>, ColorRepository>();
builder.Services.AddScoped<IRepository<PhoneType>, PhoneTypeRepository>();
builder.Services.AddScoped<UserViewModel>();
builder.Services.AddScoped<UserDetailView>();
#if WINDOWS
SetWindowOptions(builder);
SetWindowHandlers();

View File

@ -135,7 +135,7 @@
Text="{Binding UserObject.Phone}"/>
</HorizontalStackLayout>
<HorizontalStackLayout>
<Picker ItemsSource="{StaticResource UserObject.phoneTypes}"
<Picker ItemsSource="{Binding PhoneTypesList}"
SelectedItem="{Binding UserObject.PhoneType}"/>
</HorizontalStackLayout>
</FlexLayout>

View File

@ -3,10 +3,10 @@ namespace AdventureWorks.MAUI.Views;
public partial class UserDetailView : ContentPage
{
public UserDetailView()
public UserDetailView(UserViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
}
public UserViewModel ViewModel { get; set; }
@ -15,10 +15,10 @@ public partial class UserDetailView : ContentPage
{
base.OnAppearing();
ViewModel = new();
BindingContext = ViewModel;
ViewModel.GetPhoneTypes();
ViewModel.Get(1); // Assuming you want to get the user with ID 1
}
private void SaveButton_Clicked(object sender, EventArgs e)