Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions PCL.Core/Utils/Exts/EnumerableExtensions.cs
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 Plain Craft Launcher 2/Controls/MyMsg/Commands/MyMsgBoxCommand.cs
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();
}
}
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();
}
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 Plain Craft Launcher 2/Controls/MyMsg/Models/ButtonContext.cs
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 Plain Craft Launcher 2/Controls/MyMsg/Models/MyMsgInputData.cs
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 Plain Craft Launcher 2/Controls/MyMsg/Models/MyMsgTextData.cs
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; }
}
15 changes: 14 additions & 1 deletion Plain Craft Launcher 2/Controls/MyMsg/MyMsgInput.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using PCL.Controls.MyMsg.Commands;
using PCL.Controls.MyMsg.Models;
using PCL.Controls.MyMsg.ViewModels;
using PCL.Core.UI.Controls;

namespace PCL;
Expand All @@ -10,7 +14,16 @@ public partial class MyMsgInput
{
private readonly ModMain.MyMsgBoxConverter myConverter;
private readonly int uuid = ModBase.GetUuid();


public MyMsgInput(MyMsgInputData data)
{
var command = new MyMsgInputButtonCommand();
command.Exited += Close;
DataContext = new MyMsgInputViewModel(data, command);
var buttonCommand = new MyMsgInputButtonCommand();

}

public MyMsgInput(ModMain.MyMsgBoxConverter converter)
{
try
Expand Down
2 changes: 1 addition & 1 deletion Plain Craft Launcher 2/Controls/MyMsg/MyMsgText.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" FontSize="23" TextTrimming="None" Foreground="{DynamicResource ColorBrush2}"
HorizontalAlignment="Left" Name="LabTitle" Margin="7,-1,70,9" Text="测试标题文本"
HorizontalAlignment="Left" Name="LabTitle" Margin="7,-1,70,9" Text="{Binding Title}"
VerticalAlignment="Top" SnapsToDevicePixels="False" UseLayoutRounding="False" />
<Rectangle x:Name="ShapeLine" Grid.Row="1" Height="2" Fill="{Binding Foreground, ElementName=LabTitle}" />
<my:MyScrollViewer Grid.Row="3" VerticalAlignment="Top" x:Name="PanCaption" Margin="0,0,0,17"
Expand Down
13 changes: 13 additions & 0 deletions Plain Craft Launcher 2/Controls/MyMsg/MyMsgText.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using PCL.Controls.MyMsg.Models;
using PCL.Controls.MyMsg.ViewModels;
using PCL.Core.UI.Controls;

namespace PCL;
Expand All @@ -11,6 +14,16 @@ public partial class MyMsgText
private readonly ModMain.MyMsgBoxConverter myConverter;
private readonly int uuid = ModBase.GetUuid();

public MyMsgText(MyMsgTextData data)
{
data.Exited += Close;

DataContext = new MyMsgTextViewModel(data);
InitializeComponent();
ShapeLine.StrokeThickness = ModBase.GetWPFSize(1d);
Loaded += Load;
Comment on lines +21 to +24

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid using the legacy loader for data-backed dialogs

In the MyMsgTextData constructor path, myConverter is never assigned, but subscribing to the shared Load handler makes the loaded dialog execute Opacity = 0 and then dereference myConverter.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 👍 / 👎.

}

public MyMsgText(ModMain.MyMsgBoxConverter converter)
{
try
Expand Down
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;
}
}
Loading
Loading