Files
ShrlAlgoToolkit/Melskin/Controls/Hyperlink.xaml.cs

82 lines
2.6 KiB
C#
Raw Normal View History

2025-12-23 21:35:54 +08:00
using System.Diagnostics;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
2025-08-12 23:08:54 +08:00
2026-01-02 17:30:41 +08:00
namespace Melskin.Controls;
2025-08-12 23:08:54 +08:00
/// <summary>
/// A hyperlink button.
/// </summary>
public class Hyperlink : ButtonBase
{
#region Properties
2025-12-23 21:35:54 +08:00
/// <summary>
/// 已经浏览过的依赖属性。
/// </summary>
public bool IsVisited
{
get { return (bool)GetValue(IsVisitedProperty); }
// 外部无法直接赋值,只能读取
private set { SetValue(IsVisitedPropertyKey, value); }
}
/// <summary>
/// 定义私有的 Key这是“写入权限”的钥匙只有在这个类内部才能拿到
/// </summary>
2026-02-12 21:29:00 +08:00
public static readonly DependencyProperty IsVisitedProperty;
2025-12-23 21:35:54 +08:00
/// <summary>
/// 表示已经浏览过的依赖属性。
/// </summary>
2026-02-12 21:29:00 +08:00
public static readonly DependencyPropertyKey IsVisitedPropertyKey;
2025-12-23 21:35:54 +08:00
/// <summary>
/// 外部调用此方法来将超链接标记为已访问。
/// </summary>
public void MarkAsVisited()
{
SetValue(IsVisitedPropertyKey, true);
}
2025-08-24 13:49:55 +08:00
/// <summary>
/// 表示超链接地址的依赖属性。
/// </summary>
2025-12-23 21:35:54 +08:00
public static readonly DependencyProperty NavigateUriProperty =
DependencyProperty.Register(nameof(NavigateUri), typeof(string), typeof(Hyperlink), new PropertyMetadata(string.Empty));
2025-08-12 23:08:54 +08:00
/// <summary>
/// Gets or sets the uri of hyperlinks.
/// </summary>
2025-12-23 21:35:54 +08:00
public string NavigateUri { get => (string)GetValue(NavigateUriProperty);
set => SetValue(NavigateUriProperty, value);
2025-08-12 23:08:54 +08:00
}
#endregion
#region Constructors
static Hyperlink()
2026-02-12 21:29:00 +08:00
{
IsVisitedPropertyKey = DependencyProperty.RegisterReadOnly(nameof(IsVisited), typeof(bool), typeof(Hyperlink), new PropertyMetadata(false));
IsVisitedProperty = IsVisitedPropertyKey.DependencyProperty;
DefaultStyleKeyProperty.OverrideMetadata(typeof(Hyperlink), new FrameworkPropertyMetadata(typeof(Hyperlink)));
}
2025-08-12 23:08:54 +08:00
#endregion
2025-12-23 21:35:54 +08:00
/// <summary>
/// 点击后的事件
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
2026-01-02 16:37:37 +08:00
if (string.IsNullOrEmpty(NavigateUri)) return;
try
2025-12-23 21:35:54 +08:00
{
2026-01-02 16:37:37 +08:00
// 1. 打开网页
Process.Start(new ProcessStartInfo(NavigateUri) { UseShellExecute = true });
2025-12-23 21:35:54 +08:00
2026-01-02 16:37:37 +08:00
// 2. 改变自身状态(这就是你作为控件的“记忆”)
SetValue(IsVisitedPropertyKey, true);
}
catch
{
// 忽略无效链接异常
2025-12-23 21:35:54 +08:00
}
}
2025-08-12 23:08:54 +08:00
}