编辑
2025-10-04
C#
00
编辑
2025-10-04
C#
00

本示例展示了如何在WinForms应用程序中使用ScottPlot 5.0创建一个带有十字光标的交互式图表。我们将绘制一个正弦波,并添加一个跟随鼠标移动的十字光标。

准备工作

首先,确保你已经安装了必要的NuGet包:

  • ScottPlot (5.0.x)
  • ScottPlot.WinForms (5.0.x)

完整代码

以下是完整的C#代码,包含详细注释:

C#
public partial class Form1 : Form { private FormsPlot formsPlot1; private Crosshair crosshair; private ScottPlot.Plottables.Text text; public Form1() { InitializeComponent(); SetupPlot(); } private void SetupPlot() { // 创建 FormsPlot 控件 formsPlot1 = new FormsPlot(); formsPlot1.Plot.Font.Set("SimSun"); formsPlot1.Dock = DockStyle.Fill; this.Controls.Add(formsPlot1); // 生成示例数据(正弦波) int pointCount = 1000; double[] dataX = new double[pointCount]; double[] dataY = new double[pointCount]; for (int i = 0; i < pointCount; i++) { dataX[i] = i; dataY[i] = Math.Sin(i * 0.05); } // 添加散点图 var scatter = formsPlot1.Plot.Add.Scatter(dataX, dataY); scatter.Color = Colors.Blue; scatter.MarkerSize = 0; // 不显示点标记,只显示线 // 设置轴标签 formsPlot1.Plot.XLabel("X轴"); formsPlot1.Plot.YLabel("Y轴"); formsPlot1.Plot.Title("带十字光标的正弦波图"); // 创建十字光标 crosshair = formsPlot1.Plot.Add.Crosshair(0, 0); crosshair.MarkerColor = Colors.Red; crosshair.LineWidth = 1; crosshair.LinePattern = LinePattern.Dashed; // 创建一个文本注释来显示光标位置 text = formsPlot1.Plot.Add.Text("", 10, 10); text.Alignment = Alignment.UpperLeft; text.FontSize = 14; // 添加鼠标移动事件处理器 formsPlot1.MouseMove += FormsPlot1_MouseMove; } private void FormsPlot1_MouseMove(object sender, MouseEventArgs e) { // 获取鼠标在图表坐标系中的位置 var d = formsPlot1.Plot.GetCoordinates(e.X, e.Y); // 更新十字光标位置 crosshair.X = d.X; crosshair.Y = d.Y; // 更新文本注释 text.LabelText = $"X: {d.X:F2}, Y: {d.Y:F2}"; // 请求重绘 formsPlot1.Refresh(); } }

image.png

编辑
2025-10-04
C#
00

Titanium Web Proxy是一个强大的开源.NET库,用于截获、查看和修改HTTP/HTTPS流量。本文将详细介绍如何使用Titanium Web Proxy来截获系统中的所有HTTP通信,并提供多个实用示例。

安装Titanium Web Proxy

首先,通过NuGet包管理器安装Titanium Web Proxy:

C#
Install-Package Titanium.Web.Proxy

image.png

或者在你的项目文件中添加以下引用:

XML
<PackageReference Include="Titanium.Web.Proxy" Version="3.1.1397" />
编辑
2025-10-04
C#
00

FluentValidation是一个流行的.NET验证库,它提供了一种优雅的方式来定义强类型的验证规则。它的流畅接口使得创建复杂的验证逻辑变得简单直观。本文将深入探讨FluentValidation的各种特性和用法,并提供丰富的示例来说明如何在实际项目中应用这些概念。

安装和基本设置

首先,让我们通过NuGet包管理器安装FluentValidation:

Markdown
dotnet add package FluentValidation

image.png

对于ASP.NET Core项目,你可能还想安装集成包:

Markdown
dotnet add package FluentValidation.AspNetCore

创建验证器

让我们从一个简单的例子开始,创建一个用户模型和相应的验证器:

C#
public class User { public int Id { get; set; } public string Username { get; set; } public string Email { get; set; } public DateTime DateOfBirth { get; set; } } public class UserValidator : AbstractValidator<User> { public UserValidator() { RuleFor(user => user.Username).NotEmpty().Length(3, 20); RuleFor(user => user.Email).NotEmpty().EmailAddress(); RuleFor(user => user.DateOfBirth).NotEmpty().LessThan(DateTime.Today); } }
编辑
2025-10-04
C#
00

Moq作为C#中最流行的模拟框架之一,其强大的功能不仅限于单元测试。本文将探讨Moq在实际应用开发中的一些创新用法,展示如何利用其灵活性来解决各种编程挑战。

原型开发和快速验证

在项目初期,我们经常需要快速验证想法或构建原型。Moq可以帮助我们模拟尚未实现的组件或服务,使得我们能够专注于核心功能的开发。

C#
public interface IWeatherService { Task<int> GetTemperature(string city); }
C#
// 定义 WeatherApp 类 public class WeatherApp { private readonly IWeatherService _weatherService; public WeatherApp(IWeatherService weatherService) { _weatherService = weatherService; } public async Task<string> GetWeatherReport(string city) { try { int temperature = await _weatherService.GetTemperature(city); return $"The current temperature in {city} is {temperature}°C."; } catch (Exception ex) { return $"Error getting weather for {city}: {ex.Message}"; } } }
C#
static async Task Main(string[] args) { var mockWeatherService = new Mock<IWeatherService>(); mockWeatherService.Setup(s => s.GetTemperature(It.IsAny<string>())) .ReturnsAsync((string city) => city.GetHashCode() % 40); // 模拟温度 var app = new WeatherApp(mockWeatherService.Object); // 使用模拟的服务测试 WeatherApp string report = await app.GetWeatherReport("New York"); Console.WriteLine(report); report = await app.GetWeatherReport("London"); Console.WriteLine(report); }

image.png