You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
458 lines
13 KiB
458 lines
13 KiB
using AuroraDesk.Presentation.ViewModels.Base;
|
|
using ReactiveUI;
|
|
using System.Collections.ObjectModel;
|
|
using System.Reactive;
|
|
|
|
namespace AuroraDesk.Presentation.ViewModels.Pages;
|
|
|
|
/// <summary>
|
|
/// 表单类型枚举
|
|
/// </summary>
|
|
public enum FormType
|
|
{
|
|
Basic, // 基础表单(垂直布局,全字段)
|
|
Simple, // 简洁表单(水平布局,标签和输入框一行)
|
|
Contact, // 联系表单(包含下拉框选择)
|
|
Feedback, // 反馈表单(包含复选框)
|
|
Register // 注册表单(两列布局)
|
|
}
|
|
|
|
/// <summary>
|
|
/// 表单页面 ViewModel
|
|
/// </summary>
|
|
public class FormPageViewModel : RoutableViewModel
|
|
{
|
|
private bool _isFormDialogOpen;
|
|
private FormType _currentFormType = FormType.Basic;
|
|
private string _formTitle = "填写表单";
|
|
private string _formDescription = "请填写以下信息";
|
|
private string _name = string.Empty;
|
|
private string _email = string.Empty;
|
|
private string _phone = string.Empty;
|
|
private string _address = string.Empty;
|
|
private string _remarks = string.Empty;
|
|
private string _submitResult = string.Empty;
|
|
private string _selectedCountry = string.Empty;
|
|
private string _selectedCity = string.Empty;
|
|
private bool _agreeTerms = false;
|
|
private bool _subscribeNewsletter = false;
|
|
private bool _receiveNotifications = false;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="hostScreen">宿主 Screen</param>
|
|
public FormPageViewModel(IScreen hostScreen) : base(hostScreen, "Form")
|
|
{
|
|
// 初始化下拉框选项
|
|
Countries = new ObservableCollection<string> { "中国", "美国", "日本", "韩国", "英国", "德国", "法国", "其他" };
|
|
Cities = new ObservableCollection<string> { "北京", "上海", "广州", "深圳", "杭州", "成都", "其他" };
|
|
|
|
ShowBasicFormCommand = ReactiveCommand.Create(() => ShowForm(FormType.Basic));
|
|
ShowSimpleFormCommand = ReactiveCommand.Create(() => ShowForm(FormType.Simple));
|
|
ShowContactFormCommand = ReactiveCommand.Create(() => ShowForm(FormType.Contact));
|
|
ShowFeedbackFormCommand = ReactiveCommand.Create(() => ShowForm(FormType.Feedback));
|
|
ShowRegisterFormCommand = ReactiveCommand.Create(() => ShowForm(FormType.Register));
|
|
SubmitFormCommand = ReactiveCommand.Create(SubmitForm);
|
|
CancelFormCommand = ReactiveCommand.Create(CancelForm);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否显示表单对话框
|
|
/// </summary>
|
|
public bool IsFormDialogOpen
|
|
{
|
|
get => _isFormDialogOpen;
|
|
set => this.RaiseAndSetIfChanged(ref _isFormDialogOpen, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前表单类型
|
|
/// </summary>
|
|
public FormType CurrentFormType
|
|
{
|
|
get => _currentFormType;
|
|
set => this.RaiseAndSetIfChanged(ref _currentFormType, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 表单标题
|
|
/// </summary>
|
|
public string FormTitle
|
|
{
|
|
get => _formTitle;
|
|
set => this.RaiseAndSetIfChanged(ref _formTitle, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 表单描述
|
|
/// </summary>
|
|
public string FormDescription
|
|
{
|
|
get => _formDescription;
|
|
set => this.RaiseAndSetIfChanged(ref _formDescription, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否显示电话字段
|
|
/// </summary>
|
|
public bool ShowPhoneField => CurrentFormType == FormType.Basic ||
|
|
CurrentFormType == FormType.Contact ||
|
|
CurrentFormType == FormType.Register;
|
|
|
|
/// <summary>
|
|
/// 是否显示地址字段
|
|
/// </summary>
|
|
public bool ShowAddressField => CurrentFormType == FormType.Basic ||
|
|
CurrentFormType == FormType.Register;
|
|
|
|
/// <summary>
|
|
/// 是否显示备注字段
|
|
/// </summary>
|
|
public bool ShowRemarksField => CurrentFormType == FormType.Basic ||
|
|
CurrentFormType == FormType.Feedback;
|
|
|
|
/// <summary>
|
|
/// 是否使用水平布局(标签和输入框在一行)
|
|
/// </summary>
|
|
public bool UseHorizontalLayout => CurrentFormType == FormType.Simple;
|
|
|
|
/// <summary>
|
|
/// 是否使用两列布局
|
|
/// </summary>
|
|
public bool UseTwoColumnLayout => CurrentFormType == FormType.Register;
|
|
|
|
/// <summary>
|
|
/// 是否显示下拉框字段
|
|
/// </summary>
|
|
public bool ShowComboBoxFields => CurrentFormType == FormType.Contact;
|
|
|
|
/// <summary>
|
|
/// 是否显示复选框字段
|
|
/// </summary>
|
|
public bool ShowCheckBoxFields => CurrentFormType == FormType.Feedback;
|
|
|
|
/// <summary>
|
|
/// 国家列表
|
|
/// </summary>
|
|
public ObservableCollection<string> Countries { get; }
|
|
|
|
/// <summary>
|
|
/// 城市列表
|
|
/// </summary>
|
|
public ObservableCollection<string> Cities { get; }
|
|
|
|
/// <summary>
|
|
/// 选中的国家
|
|
/// </summary>
|
|
public string SelectedCountry
|
|
{
|
|
get => _selectedCountry;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedCountry, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 选中的城市
|
|
/// </summary>
|
|
public string SelectedCity
|
|
{
|
|
get => _selectedCity;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedCity, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同意条款
|
|
/// </summary>
|
|
public bool AgreeTerms
|
|
{
|
|
get => _agreeTerms;
|
|
set => this.RaiseAndSetIfChanged(ref _agreeTerms, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 订阅 newsletter
|
|
/// </summary>
|
|
public bool SubscribeNewsletter
|
|
{
|
|
get => _subscribeNewsletter;
|
|
set => this.RaiseAndSetIfChanged(ref _subscribeNewsletter, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收通知
|
|
/// </summary>
|
|
public bool ReceiveNotifications
|
|
{
|
|
get => _receiveNotifications;
|
|
set => this.RaiseAndSetIfChanged(ref _receiveNotifications, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 姓名
|
|
/// </summary>
|
|
public string Name
|
|
{
|
|
get => _name;
|
|
set => this.RaiseAndSetIfChanged(ref _name, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 邮箱
|
|
/// </summary>
|
|
public string Email
|
|
{
|
|
get => _email;
|
|
set => this.RaiseAndSetIfChanged(ref _email, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 电话
|
|
/// </summary>
|
|
public string Phone
|
|
{
|
|
get => _phone;
|
|
set => this.RaiseAndSetIfChanged(ref _phone, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 地址
|
|
/// </summary>
|
|
public string Address
|
|
{
|
|
get => _address;
|
|
set => this.RaiseAndSetIfChanged(ref _address, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 备注
|
|
/// </summary>
|
|
public string Remarks
|
|
{
|
|
get => _remarks;
|
|
set => this.RaiseAndSetIfChanged(ref _remarks, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 提交结果
|
|
/// </summary>
|
|
public string SubmitResult
|
|
{
|
|
get => _submitResult;
|
|
set => this.RaiseAndSetIfChanged(ref _submitResult, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示基础表单命令
|
|
/// </summary>
|
|
public ReactiveCommand<Unit, Unit> ShowBasicFormCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 显示简洁表单命令
|
|
/// </summary>
|
|
public ReactiveCommand<Unit, Unit> ShowSimpleFormCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 显示联系表单命令
|
|
/// </summary>
|
|
public ReactiveCommand<Unit, Unit> ShowContactFormCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 显示反馈表单命令
|
|
/// </summary>
|
|
public ReactiveCommand<Unit, Unit> ShowFeedbackFormCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 显示注册表单命令
|
|
/// </summary>
|
|
public ReactiveCommand<Unit, Unit> ShowRegisterFormCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 提交表单命令
|
|
/// </summary>
|
|
public ReactiveCommand<Unit, Unit> SubmitFormCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 取消表单命令
|
|
/// </summary>
|
|
public ReactiveCommand<Unit, Unit> CancelFormCommand { get; }
|
|
|
|
private void ShowForm(FormType formType)
|
|
{
|
|
// 重置表单数据
|
|
Name = string.Empty;
|
|
Email = string.Empty;
|
|
Phone = string.Empty;
|
|
Address = string.Empty;
|
|
Remarks = string.Empty;
|
|
SelectedCountry = string.Empty;
|
|
SelectedCity = string.Empty;
|
|
AgreeTerms = false;
|
|
SubscribeNewsletter = false;
|
|
ReceiveNotifications = false;
|
|
SubmitResult = string.Empty;
|
|
|
|
// 设置表单类型
|
|
CurrentFormType = formType;
|
|
|
|
// 根据表单类型设置标题和描述
|
|
switch (formType)
|
|
{
|
|
case FormType.Basic:
|
|
FormTitle = "基础表单";
|
|
FormDescription = "垂直布局,标签在上输入框在下";
|
|
break;
|
|
case FormType.Simple:
|
|
FormTitle = "简洁表单";
|
|
FormDescription = "水平布局,标签和输入框在同一行";
|
|
break;
|
|
case FormType.Contact:
|
|
FormTitle = "联系表单";
|
|
FormDescription = "包含下拉框选择国家/城市";
|
|
break;
|
|
case FormType.Feedback:
|
|
FormTitle = "反馈表单";
|
|
FormDescription = "包含复选框选项";
|
|
break;
|
|
case FormType.Register:
|
|
FormTitle = "注册表单";
|
|
FormDescription = "两列布局,充分利用空间";
|
|
break;
|
|
}
|
|
|
|
// 触发所有属性更新
|
|
this.RaisePropertyChanged(nameof(ShowPhoneField));
|
|
this.RaisePropertyChanged(nameof(ShowAddressField));
|
|
this.RaisePropertyChanged(nameof(ShowRemarksField));
|
|
this.RaisePropertyChanged(nameof(UseHorizontalLayout));
|
|
this.RaisePropertyChanged(nameof(UseTwoColumnLayout));
|
|
this.RaisePropertyChanged(nameof(ShowComboBoxFields));
|
|
this.RaisePropertyChanged(nameof(ShowCheckBoxFields));
|
|
|
|
// 显示对话框
|
|
IsFormDialogOpen = true;
|
|
}
|
|
|
|
private void SubmitForm()
|
|
{
|
|
// 验证必填字段
|
|
if (string.IsNullOrWhiteSpace(Name))
|
|
{
|
|
SubmitResult = "错误:姓名不能为空";
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(Email))
|
|
{
|
|
SubmitResult = "错误:邮箱不能为空";
|
|
return;
|
|
}
|
|
|
|
// 简单的邮箱格式验证
|
|
if (!Email.Contains("@") || !Email.Contains("."))
|
|
{
|
|
SubmitResult = "错误:邮箱格式不正确";
|
|
return;
|
|
}
|
|
|
|
// 根据表单类型验证特定字段
|
|
if (CurrentFormType == FormType.Contact || CurrentFormType == FormType.Register)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Phone))
|
|
{
|
|
SubmitResult = "错误:电话不能为空";
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (CurrentFormType == FormType.Register)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Address))
|
|
{
|
|
SubmitResult = "错误:地址不能为空";
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (CurrentFormType == FormType.Feedback)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Remarks))
|
|
{
|
|
SubmitResult = "错误:反馈内容不能为空";
|
|
return;
|
|
}
|
|
if (!AgreeTerms)
|
|
{
|
|
SubmitResult = "错误:必须同意服务条款";
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (CurrentFormType == FormType.Contact)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(SelectedCountry))
|
|
{
|
|
SubmitResult = "错误:请选择国家";
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(SelectedCity))
|
|
{
|
|
SubmitResult = "错误:请选择城市";
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 关闭对话框
|
|
IsFormDialogOpen = false;
|
|
|
|
// 显示提交结果
|
|
var formTypeName = CurrentFormType switch
|
|
{
|
|
FormType.Basic => "基础表单",
|
|
FormType.Simple => "简洁表单",
|
|
FormType.Contact => "联系表单",
|
|
FormType.Feedback => "反馈表单",
|
|
FormType.Register => "注册表单",
|
|
_ => "表单"
|
|
};
|
|
|
|
var result = $"[{formTypeName}] 提交成功!\n姓名:{Name}\n邮箱:{Email}";
|
|
if (ShowPhoneField && !string.IsNullOrWhiteSpace(Phone))
|
|
{
|
|
result += $"\n电话:{Phone}";
|
|
}
|
|
if (ShowAddressField && !string.IsNullOrWhiteSpace(Address))
|
|
{
|
|
result += $"\n地址:{Address}";
|
|
}
|
|
if (ShowComboBoxFields)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(SelectedCountry))
|
|
{
|
|
result += $"\n国家:{SelectedCountry}";
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(SelectedCity))
|
|
{
|
|
result += $"\n城市:{SelectedCity}";
|
|
}
|
|
}
|
|
if (ShowRemarksField && !string.IsNullOrWhiteSpace(Remarks))
|
|
{
|
|
result += $"\n备注:{Remarks}";
|
|
}
|
|
if (ShowCheckBoxFields)
|
|
{
|
|
result += $"\n同意条款:{(AgreeTerms ? "是" : "否")}";
|
|
result += $"\n订阅邮件:{(SubscribeNewsletter ? "是" : "否")}";
|
|
result += $"\n接收通知:{(ReceiveNotifications ? "是" : "否")}";
|
|
}
|
|
|
|
SubmitResult = result;
|
|
}
|
|
|
|
private void CancelForm()
|
|
{
|
|
IsFormDialogOpen = false;
|
|
SubmitResult = "已取消提交";
|
|
}
|
|
}
|
|
|
|
|