VRCMelonAssistant/VRCMelonAssistant/Classes/HyperlinkExtensions.cs
2021-05-17 23:15:38 +02:00

50 lines
1.6 KiB
C#

using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
namespace VRCMelonAssistant
{
public static class HyperlinkExtensions
{
public static bool GetIsExternal(DependencyObject obj)
{
return (bool)obj.GetValue(IsExternalProperty);
}
public static void SetIsExternal(DependencyObject obj, bool value)
{
obj.SetValue(IsExternalProperty, value);
}
public static readonly DependencyProperty IsExternalProperty = DependencyProperty.RegisterAttached("IsExternal", typeof(bool), typeof(HyperlinkExtensions), new UIPropertyMetadata(false, OnIsExternalChanged));
private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var hyperlink = sender as Hyperlink;
if ((bool)args.NewValue)
hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
else
hyperlink.RequestNavigate -= Hyperlink_RequestNavigate;
}
public static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
public static void NoAwait(this Task task)
{
task.ContinueWith(t =>
{
if (t.IsFaulted)
{
Utils.ShowErrorMessageBox("Exception in free-floating task", t.Exception);
}
});
}
}
}