-
Notifications
You must be signed in to change notification settings - Fork 114
feat(mymsgbox): 实现部分控件的 MVVM 设计 #3201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,23 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace PCL.Core.Utils.Exts; | ||
|
|
||
| public static class EnumerableExtensions | ||
| { | ||
| /// <summary> | ||
| /// 对目标集合进行枚举,并传递该元素在集合中的位置 | ||
| /// </summary> | ||
| /// <param name="collection">要枚举的集合</param> | ||
| /// <param name="handle">处理函数</param> | ||
| /// <typeparam name="T">类型</typeparam> | ||
| public static void ForEachIndexed<T>(this IEnumerable<T> collection, Action<T, int> handle) | ||
| { | ||
| var i = 0; | ||
| foreach (var element in collection) | ||
| { | ||
| handle.Invoke(element ,i); | ||
| i++; | ||
| } | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
Plain Craft Launcher 2/Controls/MyMsg/Commands/MyMsgBoxCommand.cs
This file contains hidden or 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,39 @@ | ||
| using System.Windows.Input; | ||
| using PCL.Controls.MyMsg.Models; | ||
| using PCL.Core.Utils.Exts; | ||
|
|
||
| namespace PCL.Controls.MyMsg.Commands; | ||
|
|
||
| public class MyMsgBoxCommand: ICommand | ||
| { | ||
| public MyMsgBoxCommand(ButtonContext[] contexts) | ||
| { | ||
| contexts.ForEachIndexed((value, index) => OperateMap[index] = value); | ||
| } | ||
|
|
||
| public event Action? Exited; | ||
|
|
||
| public event Action<int>? Clicked; | ||
|
|
||
| public event EventHandler? CanExecuteChanged; | ||
|
|
||
| private Dictionary<int, ButtonContext> OperateMap { get; set; } = []; | ||
|
|
||
| public string GetButtonTextById(int id) => | ||
| OperateMap.FirstOrDefault(key => key.Key == id).Value.ButtonName; | ||
|
|
||
| public bool CanExecute(object? parameter) => | ||
| parameter is not null && OperateMap.ContainsKey((int)parameter); | ||
|
|
||
| public void Execute(object? parameter) | ||
| { | ||
|
|
||
| if(parameter is null | ||
| || !OperateMap.TryGetValue((int)parameter, out var op)) return; | ||
|
|
||
| Clicked?.Invoke((int)parameter); | ||
|
|
||
| op.Operation.Invoke(parameter); | ||
| if(op.ExitWhenClick) Exited?.Invoke(); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
Plain Craft Launcher 2/Controls/MyMsg/Commands/MyMsgInputButtonCommand.cs
This file contains hidden or 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,14 @@ | ||
| using System.Windows.Input; | ||
|
|
||
| namespace PCL.Controls.MyMsg.Commands; | ||
|
|
||
| public class MyMsgInputButtonCommand: ICommand | ||
| { | ||
| public event Action? Exited; | ||
|
|
||
| public event EventHandler? CanExecuteChanged; | ||
|
|
||
| public bool CanExecute(object? parameter) => true; | ||
|
|
||
| public void Execute(object? parameter) => Exited?.Invoke(); | ||
| } |
30 changes: 30 additions & 0 deletions
30
Plain Craft Launcher 2/Controls/MyMsg/Commands/MyMsgInputCommand.cs
This file contains hidden or 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,30 @@ | ||
| using System.Windows.Input; | ||
| using FluentValidation; | ||
|
|
||
| namespace PCL.Controls.MyMsg.Commands; | ||
|
|
||
| public class MyMsgInputCommand: ICommand | ||
| { | ||
| public List<IValidator<string>> ValidateRules { get; } = []; | ||
| public event Action<string>? Error; | ||
|
|
||
| public event Action? Success; | ||
|
|
||
| public event EventHandler? CanExecuteChanged; | ||
|
|
||
| public void Execute(object? parameter) | ||
| { | ||
| var userInput = parameter?.ToString()!; | ||
| foreach (var rule in ValidateRules) | ||
| { | ||
| var result = rule.Validate(userInput); | ||
| if(result.IsValid) continue; | ||
| Error?.Invoke(string.Join(Environment.NewLine, result.Errors.Select(e => e.ErrorMessage))); | ||
| return; | ||
| } | ||
| Success?.Invoke(); | ||
| } | ||
|
|
||
| public bool CanExecute(object? parameter) => true; | ||
|
|
||
| } |
8 changes: 8 additions & 0 deletions
8
Plain Craft Launcher 2/Controls/MyMsg/Models/ButtonContext.cs
This file contains hidden or 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,8 @@ | ||
| namespace PCL.Controls.MyMsg.Models; | ||
|
|
||
| public class ButtonContext | ||
| { | ||
| public required string ButtonName { get; set; } | ||
| public bool ExitWhenClick { get; set; } | ||
| public Action<object> Operation { get; set; } | ||
| } |
45 changes: 45 additions & 0 deletions
45
Plain Craft Launcher 2/Controls/MyMsg/Models/MyMsgInputData.cs
This file contains hidden or 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,45 @@ | ||
| using System.Windows.Input; | ||
| using FluentValidation; | ||
| using PCL.Controls.MyMsg.Commands; | ||
|
|
||
| namespace PCL.Controls.MyMsg.Models; | ||
|
|
||
| public class MyMsgInputData | ||
| { | ||
|
|
||
| public string? UserInput { get; set; } | ||
|
|
||
| public string? Description { get; set; } | ||
|
|
||
| public bool NoError { get; set; } | ||
|
|
||
| public ICommand Command; | ||
|
|
||
| public MyMsgInputData(string description) | ||
| { | ||
| Description = description; | ||
| var command = new MyMsgInputCommand(); | ||
| command.Error += _ => NoError = false; | ||
| command.Success += () => NoError = true; | ||
| Command = command; | ||
| } | ||
|
|
||
| public MyMsgInputData(List<IValidator<string>> rules) | ||
| { | ||
| var command = new MyMsgInputCommand(); | ||
| command.Error += _ => NoError = false; | ||
| command.Success += () => NoError = true; | ||
| rules.ForEach(v => command.ValidateRules.Add(v)); | ||
| Command = command; | ||
| } | ||
|
|
||
| public MyMsgInputData(string description, List<IValidator<string>> rules) | ||
| { | ||
| Description = description; | ||
| var command = new MyMsgInputCommand(); | ||
| command.Error += _ => NoError = false; | ||
| command.Success += () => NoError = true; | ||
| rules.ForEach(v => command.ValidateRules.Add(v)); | ||
| Command = command; | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
Plain Craft Launcher 2/Controls/MyMsg/Models/MyMsgTextData.cs
This file contains hidden or 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,50 @@ | ||
| using System.Windows.Input; | ||
| using PCL.Controls.MyMsg.Commands; | ||
|
|
||
| // 非常感谢龙猫送来的工作量,非常感谢!!! | ||
|
|
||
| namespace PCL.Controls.MyMsg.Models; | ||
|
|
||
| public class MyMsgTextData | ||
| { | ||
| /// <summary> | ||
| /// 退出事件 | ||
| /// </summary> | ||
| public event Action? Exited; | ||
| /// <summary> | ||
| /// 窗口消息 | ||
| /// </summary> | ||
| public string Message { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 标题 | ||
| /// </summary> | ||
| public string Title { get; set; } = "提示"; | ||
|
|
||
| public MyMsgTextData(string message) : this(message, | ||
| new ButtonContext | ||
| { | ||
| ButtonName = "确定", ExitWhenClick = true, Operation = static _ => { } | ||
| }, | ||
| new ButtonContext | ||
| { | ||
| ButtonName = "取消", ExitWhenClick = true, Operation = static _ => { } | ||
| } | ||
| ){} | ||
|
|
||
| public MyMsgTextData(string message, params ButtonContext[] buttons) | ||
| { | ||
| Message = message; | ||
| var cmd =new MyMsgBoxCommand(buttons); | ||
| cmd.Exited += () => Exited?.Invoke(); | ||
| cmd.Clicked += value => Result = value; | ||
| Command = cmd; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 执行器 | ||
| /// </summary> | ||
| public ICommand Command { get; set; } | ||
|
|
||
| public int Result { get; set; } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
28 changes: 28 additions & 0 deletions
28
Plain Craft Launcher 2/Controls/MyMsg/ViewModels/MyMsgInputViewModel.cs
This file contains hidden or 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,28 @@ | ||
| using System.Windows.Input; | ||
| using PCL.Controls.MyMsg.Commands; | ||
| using PCL.Controls.MyMsg.Models; | ||
|
|
||
| namespace PCL.Controls.MyMsg.ViewModels; | ||
|
|
||
| public class MyMsgInputViewModel: ViewModelBase | ||
| { | ||
| public string ErrorDescription | ||
| { | ||
| get; | ||
| set | ||
| { | ||
| if (field == value) return; | ||
| SetProperty(value, ref field); | ||
| } | ||
| } = ""; | ||
|
|
||
| public ICommand ButtonCommand { get; set; } | ||
| public ICommand ValidateCommand { get; set; } | ||
|
|
||
| public MyMsgInputViewModel(MyMsgInputData data, ICommand buttonCommand) | ||
| { | ||
| ((MyMsgInputCommand)data.Command).Error += value => ErrorDescription = value; | ||
| ButtonCommand = buttonCommand; | ||
| ValidateCommand = data.Command; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
MyMsgTextDataconstructor path,myConverteris never assigned, but subscribing to the sharedLoadhandler makes the loaded dialog executeOpacity = 0and then dereferencemyConverter.IsWarn. The exception is caught after the opacity reset, so any data-backed dialog that does construct becomes invisible and never starts its show animation; use a loader that reads from the new data/view model or initialize the fields it depends on.Useful? React with 👍 / 👎.