Implementing of viewModel handling for UI

This commit is contained in:
2025-08-19 16:50:53 +02:00
parent 86daaa5133
commit 2f053dfc71
8 changed files with 139 additions and 33 deletions

View File

@ -1,6 +1,6 @@
using Common.Library;
namespace AdventureWorks.EntityLayer.EntityClasses;
namespace AdventureWorks.EntityLayer;
public class User : EntityBase
{

View File

@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventureWorks.EntityLayer"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.Library", "Common.Library\Common.Library.csproj", "{FEDB6441-2564-4DD5-A3C9-1B2F0A761BB4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventureWorks.ViewModelLayer", "AdventureWorks.ViewModelLayer\AdventureWorks.ViewModelLayer.csproj", "{3E3C685B-CA66-4E12-83BC-B872BC5FD933}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -28,6 +30,10 @@ Global
{FEDB6441-2564-4DD5-A3C9-1B2F0A761BB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEDB6441-2564-4DD5-A3C9-1B2F0A761BB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEDB6441-2564-4DD5-A3C9-1B2F0A761BB4}.Release|Any CPU.Build.0 = Release|Any CPU
{3E3C685B-CA66-4E12-83BC-B872BC5FD933}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E3C685B-CA66-4E12-83BC-B872BC5FD933}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E3C685B-CA66-4E12-83BC-B872BC5FD933}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E3C685B-CA66-4E12-83BC-B872BC5FD933}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

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

View File

@ -4,13 +4,13 @@
x:Class="AdventureWorks.MAUI.Views.UserDetailView"
xmlns:partial="clr-namespace:AdventureWorks.MAUI.ViewsPartial"
xmlns:converters="clr-namespace:AdventureWorks.MAUI.Converters"
xmlns:vm="clr-namespace:AdventureWorks.EntityLayer.EntityClasses;assembly=AdventureWorks.EntityLayer"
x:DataType="vm:User"
xmlns:vm="clr-namespace:AdventureWorks.ViewModelLayer;assembly=AdventureWorks.ViewModelLayer"
x:DataType="vm:UserViewModel"
Title="User Information">
<ContentPage.Resources>
<converters:InvertedBoolConverter x:Key="invertedBoolean" />
<vm:User x:Key="viewModel"
<!--<vm:User x:Key="viewModel"
LoginId="JohnSmith123"
FirstName="John222"
LastName="Smith"
@ -24,11 +24,10 @@
IsEnrolledInHSA="False"
IsActive="True"
BirthDate="10-03-1975"
/>
/>-->
</ContentPage.Resources>
<Border Style="{StaticResource Border.Page}"
BindingContext="{StaticResource viewModel}" >
<Border Style="{StaticResource Border.Page}" >
<ScrollView>
<Grid RowDefinitions="Auto, Auto, Auto, Auto, Auto, Auto, Auto, Auto, Auto,Auto, Auto, Auto , Auto"
ColumnDefinitions="Auto, *"
@ -45,7 +44,7 @@
<VerticalStackLayout Grid.Row="1"
Grid.Column="1">
<Entry Text="{Binding LoginId}"
<Entry Text="{Binding UserObject.LoginId}"
Placeholder="Please use a combination of letters and numbers."/>
<Label FontSize="Micro"
Text="Please use a combination of letters and numbers." />
@ -54,17 +53,17 @@
Text="First Name" />
<Entry Grid.Row="2"
Grid.Column="1"
Text="{Binding FirstName}"/>
Text="{Binding UserObject.FirstName}"/>
<Label Grid.Row="3"
Text="Last Name" />
<Entry Grid.Row="3"
Grid.Column="1"
Text="{Binding LastName}"/>
Text="{Binding UserObject.LastName}"/>
<Label Grid.Row="4"
Text="Email Address" />
<Entry Grid.Row="4"
Grid.Column="1"
Text="{Binding Email}"/>
Text="{Binding UserObject.Email}"/>
<Label Grid.Row="5" Text="Is Enrolled ?"/>
<FlexLayout Grid.Row="5"
@ -73,19 +72,19 @@
Direction="Row">
<HorizontalStackLayout>
<Label Text="401k?"/>
<CheckBox IsChecked="{Binding IsEnrolledIn401k}"/>
<CheckBox IsChecked="{Binding UserObject.IsEnrolledIn401k}"/>
</HorizontalStackLayout>
<HorizontalStackLayout >
<Label Text="Flex Time?"/>
<CheckBox x:Name="FlexTime" IsChecked="{Binding IsEnrolledInFlexTime}"/>
<CheckBox x:Name="FlexTime" IsChecked="{Binding UserObject.IsEnrolledInFlexTime}"/>
</HorizontalStackLayout>
<HorizontalStackLayout >
<Label Text="Health Care?"/>
<CheckBox IsChecked="{Binding IsEnrolledInHealthCare}"/>
<CheckBox IsChecked="{Binding UserObject.IsEnrolledInHealthCare}"/>
</HorizontalStackLayout>
<HorizontalStackLayout >
<Label Text="Health Savings Account?"/>
<CheckBox IsChecked="{Binding IsEnrolledInHSA}"/>
<CheckBox IsChecked="{Binding UserObject.IsEnrolledInHSA}"/>
</HorizontalStackLayout>
</FlexLayout>
@ -93,7 +92,7 @@
Text="Still Employed"/>
<Switch Grid.Row="6"
Grid.Column="1"
IsToggled="{Binding IsActive}"/>
IsToggled="{Binding UserObject.IsActive}"/>
<Label Text="Employee Type"
Grid.Row="7"/>
<FlexLayout Grid.Row="7"
@ -103,12 +102,12 @@
<HorizontalStackLayout>
<Label Text="Full-Time"/>
<RadioButton x:Name="FullTime"
IsChecked="{Binding IsFullTime}"
IsChecked="{Binding UserObject.IsFullTime}"
GroupName="EmployeeType"/>
</HorizontalStackLayout>
<HorizontalStackLayout>
<Label Text="Part-Time"/>
<RadioButton GroupName="EmployeeType" IsChecked="{Binding IsFullTime, Converter={StaticResource invertedBoolean}}"/>
<RadioButton GroupName="EmployeeType" IsChecked="{Binding UserObject.IsFullTime, Converter={StaticResource invertedBoolean}}"/>
</HorizontalStackLayout>
</FlexLayout>
@ -116,14 +115,14 @@
Grid.Row="8"/>
<DatePicker Grid.Row="8"
Grid.Column="1"
Date="{Binding BirthDate}"
Date="{Binding UserObject.BirthDate}"
HorizontalOptions="Start"/>
<Label Text="Start Time"
Grid.Row="9"/>
<TimePicker Grid.Row="9"
Grid.Column="1"
Time="{Binding StartTime}"
IsEnabled="{Binding IsFullTime, Converter={StaticResource invertedBoolean}}"/>
Time="{Binding UserObject.StartTime}"
IsEnabled="{Binding UserObject.IsFullTime, Converter={StaticResource invertedBoolean}}"/>
<Label Text="Phone"
Grid.Row="10"/>
@ -133,11 +132,11 @@
Direction="Row">
<HorizontalStackLayout>
<Entry MinimumWidthRequest="120"
Text="{Binding Phone}"/>
Text="{Binding UserObject.Phone}"/>
</HorizontalStackLayout>
<HorizontalStackLayout>
<Picker ItemsSource="{StaticResource phoneTypes}"
SelectedItem="{Binding PhoneType}"/>
<Picker ItemsSource="{StaticResource UserObject.phoneTypes}"
SelectedItem="{Binding UserObject.PhoneType}"/>
</HorizontalStackLayout>
</FlexLayout>

View File

@ -1,5 +1,4 @@
using AdventureWorks.EntityLayer.EntityClasses;
using AdventureWorks.ViewModelLayer;
namespace AdventureWorks.MAUI.Views;
public partial class UserDetailView : ContentPage
@ -7,20 +6,20 @@ public partial class UserDetailView : ContentPage
public UserDetailView()
{
InitializeComponent();
UserObject = (User)this.Resources["viewModel"];
UserObject.LoginId = "jkzxck0q94375";
}
public User UserObject { get; set; }
public UserViewModel ViewModel { get; set; }
protected override void OnAppearing()
{
base.OnAppearing();
UserObject.LoginId = "PeterPiper384";
UserObject.FirstName = "Peter";
UserObject.LastName = "Piper";
UserObject.Email = "peter@piper.com";
ViewModel = new();
BindingContext = ViewModel;
ViewModel.Get(1); // Assuming you want to get the user with ID 1
}
private void SaveButton_Clicked(object sender, EventArgs e)
{

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AdventureWorks.EntityLayer\AdventureWorks.EntityLayer.csproj" />
<ProjectReference Include="..\Common.Library\Common.Library.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,81 @@
using AdventureWorks.EntityLayer;
using Common.Library;
using System.Collections.ObjectModel;
namespace AdventureWorks.ViewModelLayer;
public class UserViewModel : ViewModelBase
{
#region Private Variables
private User? _UserObject = new();
#endregion
#region public Properties
public User? UserObject
{
get { return _UserObject; }
set
{
_UserObject = value;
RaisePropertyChanged(nameof(UserObject));
}
}
#endregion
#region Get Method
public ObservableCollection<User> Get()
{
return new();
}
#endregion
#region Get(id) Method
public User? Get(int id)
{
try
{
UserObject = new User
{
UserId = id,
LoginId = "SallyJones615",
FirstName = "Sally",
LastName = "Jones",
Email = "sally@jones.com",
Password = "password123",
Phone = "555-1234",
PhoneType = "Mobile",
IsFullTime = true,
IsEnrolledIn401k = false,
IsEnrolledInFlexTime = false,
IsEnrolledInHealthCare = true,
IsEnrolledInHSA = false,
IsActive = true,
BirthDate = Convert.ToDateTime("1975-11-04"),
StartTime = new TimeSpan(8, 0, 0)
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}");
}
return UserObject;
}
#endregion
#region Save Method
public virtual bool Save()
{
System.Diagnostics.Debugger.Break();
return true;
}
#endregion
}

View File

@ -0,0 +1,6 @@

namespace Common.Library;
public class ViewModelBase : CommonBase
{
}