-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Leo.Wpf.App | ||
{ | ||
public interface ICustomerEditorWindowService | ||
{ | ||
bool? ShowDialog(string customerId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Leo.Wpf.App.ViewModels; | ||
using Leo.Wpf.App.Views; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Windows; | ||
|
||
namespace Leo.Wpf.App.Services | ||
{ | ||
internal sealed class CustomerEditorWindowService(IServiceProvider _services) : ICustomerEditorWindowService | ||
{ | ||
public bool? ShowDialog(string customerId) | ||
{ | ||
var viewModel = _services.GetRequiredService<CustomerEditorViewModel>(); | ||
_ = viewModel.LoadSeletedCustomerAsync(customerId); | ||
var window = new CustomerEditorWindow(viewModel) | ||
{ | ||
Owner = Application.Current.MainWindow, | ||
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner | ||
}; | ||
return window.ShowDialog(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using AutoMapper; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using CommunityToolkit.Mvvm.Messaging; | ||
using Leo.Data.Domain.Dtos; | ||
using Leo.UI; | ||
using Leo.Wpf.App.Messages; | ||
|
||
namespace Leo.Wpf.App.ViewModels | ||
{ | ||
public partial class CustomerEditorViewModel : ObservableObject | ||
{ | ||
private CustomerViewModel? _seletedCustomer; | ||
|
||
private readonly ICustomerService _customerService; | ||
private readonly IMapper _mapper; | ||
private readonly IMessenger _messenger; | ||
|
||
public CustomerEditorViewModel( | ||
ICustomerService customerService, | ||
IMapper mapper, | ||
IMessenger messenger) | ||
{ | ||
_customerService = customerService; | ||
_mapper = mapper; | ||
_messenger = messenger; | ||
SaveCommand = new AsyncRelayCommand(SaveAsync, () => SeletedCustomer != null); | ||
} | ||
|
||
public CustomerViewModel? SeletedCustomer | ||
{ | ||
get { return _seletedCustomer; } | ||
set | ||
{ | ||
if (_seletedCustomer != value) | ||
{ | ||
SetProperty(ref _seletedCustomer, value); | ||
SaveCommand.NotifyCanExecuteChanged(); | ||
} | ||
} | ||
} | ||
|
||
public IRelayCommand SaveCommand { get; init; } | ||
|
||
public event Action? CloseAction; | ||
|
||
public async Task LoadSeletedCustomerAsync(string customerId) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(customerId, nameof(customerId)); | ||
|
||
var dto = await _customerService.GetAsync(Guid.Parse(customerId)); | ||
var customerViewModel = _mapper.Map<CustomerViewModel>(dto); | ||
if (customerViewModel != null) | ||
{ | ||
SeletedCustomer = customerViewModel; | ||
} | ||
} | ||
|
||
private async Task SaveAsync() | ||
{ | ||
if (SeletedCustomer != null) | ||
{ | ||
var dto = _mapper.Map<CustomerDto>(this); | ||
await _customerService.UpdateAsync(dto); | ||
_messenger.Send(new CustomerCreatedMessage(SeletedCustomer.Id.ToString())); | ||
} | ||
Close(); | ||
} | ||
|
||
[RelayCommand] | ||
private void Close() => CloseAction?.Invoke(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<Window x:Class="Leo.Wpf.App.Views.CustomerEditorWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:viewModel="clr-namespace:Leo.Wpf.App.ViewModels" | ||
xmlns:infra="clr-namespace:Leo.Wpf.App.Infrastructure" | ||
mc:Ignorable="d" | ||
d:DataContext="{d:DesignInstance viewModel:CustomerEditorViewModel}" | ||
Title="CustomerEditorWindow" | ||
WindowStyle="ToolWindow" | ||
ResizeMode="NoResize" | ||
SizeToContent="WidthAndHeight" | ||
WindowStartupLocation="CenterOwner"> | ||
<Window.Resources> | ||
<Style x:Key="BaseStyle"> | ||
<Setter Property="Control.MinWidth" Value="150" /> | ||
<Setter Property="Control.Margin" Value="2,0,5,0" /> | ||
<Setter Property="Control.VerticalContentAlignment" Value="Center" /> | ||
<Setter Property="Control.Margin" Value="0,1,0,1" /> | ||
</Style> | ||
<Style TargetType="Label"> | ||
<Setter Property="MinWidth" Value="80"/> | ||
<Setter Property="Background" Value="Gainsboro"/> | ||
<Setter Property="HorizontalContentAlignment" Value="Right"/> | ||
<Setter Property="Margin" Value="0,1,2,1" /> | ||
</Style> | ||
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}"> | ||
</Style> | ||
<Style TargetType="ComboBox" BasedOn="{StaticResource BaseStyle}"> | ||
</Style> | ||
<Style TargetType="DatePickerTextBox" BasedOn="{StaticResource BaseStyle}"> | ||
</Style> | ||
<Style TargetType="Button"> | ||
<Setter Property="Margin" Value="0,5,5,0" /> | ||
<Setter Property="MinWidth" Value="80" /> | ||
<Setter Property="MinHeight" Value="30" /> | ||
</Style> | ||
</Window.Resources> | ||
<Grid Margin="5,5,5,5"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
<StackPanel Grid.Row="0" Orientation="Horizontal"> | ||
<Label Content="宝宝姓名: " Target="{Binding ElementName=txtName}" /> | ||
<TextBox x:Name="txtName" Text="{Binding SeletedCustomer.Name}" /> | ||
</StackPanel> | ||
<StackPanel Grid.Row="1" Orientation="Horizontal"> | ||
<Label Content="出生日期: " /> | ||
<DatePickerTextBox x:Name="txtBirthday" Text="{Binding SeletedCustomer.Birthday, StringFormat=yyyy-MM-dd}"/> | ||
</StackPanel> | ||
<StackPanel Grid.Row="2" Orientation="Horizontal"> | ||
<Label Content="性别: " /> | ||
<ComboBox x:Name="txtGender" | ||
ItemsSource="{x:Static infra:Constants.Genders}" | ||
SelectedValuePath="Key" | ||
DisplayMemberPath="Value" | ||
SelectedValue="{Binding SeletedCustomer.Gender}"/> | ||
</StackPanel> | ||
<StackPanel Grid.Row="3" Orientation="Horizontal"> | ||
<Label Content="手机: " /> | ||
<TextBox x:Name="txtPhone" Text="{Binding SeletedCustomer.Phone}" /> | ||
</StackPanel> | ||
<StackPanel Grid.Row="4" Orientation="Horizontal"> | ||
<Label Content="会员卡号: " /> | ||
<TextBox x:Name="txtCarNo" Text="{Binding SeletedCustomer.CardNo}" /> | ||
</StackPanel> | ||
<StackPanel Grid.Row="5" Orientation="Horizontal" FlowDirection="RightToLeft"> | ||
<Button x:Name="btnOK" | ||
Command="{Binding SaveCommand}" | ||
Content="保存" | ||
IsDefault="True" /> | ||
<Button x:Name="btnCancel" | ||
Command="{Binding CloseCommand}" | ||
Content="取消" | ||
IsCancel="True" /> | ||
</StackPanel> | ||
</Grid> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Leo.Wpf.App.ViewModels; | ||
using System.Windows; | ||
|
||
namespace Leo.Wpf.App.Views | ||
{ | ||
/// <summary> | ||
/// Interaction logic for CustomerEditorWindow.xaml | ||
/// </summary> | ||
public partial class CustomerEditorWindow : Window | ||
{ | ||
public CustomerEditorWindow(CustomerEditorViewModel viewModel) | ||
{ | ||
InitializeComponent(); | ||
DataContext = viewModel; | ||
viewModel.CloseAction += () => Close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters