整理控件库

This commit is contained in:
GG Z
2025-05-05 17:04:06 +08:00
parent 74532b77be
commit 3eaad7566e
283 changed files with 2156 additions and 17846 deletions

View File

@@ -1,10 +1,11 @@
using WPFluent.Controls;
using System.Windows.Controls;
using WPFluent.Controls;
namespace WPFluent.Extensions;
public static class ContentDialogServiceExtensions
@@ -14,7 +15,7 @@ public static class ContentDialogServiceExtensions
/// </summary>
/// <returns>Result of the life cycle of the <see cref="ContentDialog"/>.</returns>
public static Task<ContentDialogResult> ShowAlertAsync(
this IContentDialogService dialogService,
this ContentDialogService dialogService,
string title,
string message,
string closeButtonText,
@@ -37,8 +38,8 @@ public static class ContentDialogServiceExtensions
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Result of the life cycle of the <see cref="ContentDialog"/>.</returns>
public static Task<ContentDialogResult> ShowSimpleDialogAsync(
this IContentDialogService dialogService,
SimpleContentDialogCreateOptions options,
this ContentDialogService dialogService,
SimpleContentDialogOptions options,
CancellationToken cancellationToken = default)
{
var dialog = new ContentDialog()

View File

@@ -1,32 +0,0 @@
using System.Windows.Controls;
namespace WPFluent.Extensions;
/// <summary>
/// Set of extensions for <see cref="System.Windows.Controls.Frame"/>.
/// </summary>
public static class FrameExtensions
{
/// <summary>
/// Cleans <see cref="Frame"/> journal.
/// </summary>
/// <param name="frame">Selected frame.</param>
public static void CleanNavigation(this Frame frame)
{
while(frame.CanGoBack)
{
frame.RemoveBackEntry();
}
}
/// <summary>
/// Gets <see cref="FrameworkElement.DataContext"/> from <see cref="Frame"/>.
/// </summary>
/// <param name="frame">Selected frame.</param>
/// <returns>DataContext of currently active element, otherwise <see langword="null"/>.</returns>
public static object? GetDataContext(this Frame frame)
{ return frame.Content is not FrameworkElement element ? null : element.DataContext; }
}

View File

@@ -5,41 +5,7 @@ namespace WPFluent.Extensions;
public static class MessageExtensions
{
private static readonly IContentDialogService ContentDialogService = new ContentDialogService();
private static readonly SnackbarService SnackbarService = new();
public static void ShowCaution(this SnackbarPresenter SnackbarPresenter, string title, string message)
{
SnackbarService.SetSnackbarPresenter(SnackbarPresenter);
SnackbarService.Show(
title,
message,
ControlAppearance.Caution,
new SymbolIcon(SymbolRegular.Question16),
SnackbarService.DefaultTimeOut);
}
public static void ShowDanger(this SnackbarPresenter SnackbarPresenter, string title, string message)
{
SnackbarService.SetSnackbarPresenter(SnackbarPresenter);
SnackbarService.Show(
title,
message,
ControlAppearance.Danger,
new SymbolIcon(SymbolRegular.Alert12),
SnackbarService.DefaultTimeOut);
}
public static void ShowInfo(this SnackbarPresenter SnackbarPresenter, string title, string message)
{
SnackbarService.SetSnackbarPresenter(SnackbarPresenter);
SnackbarService.Show(
title,
message,
ControlAppearance.Info,
new SymbolIcon(SymbolRegular.Info12),
SnackbarService.DefaultTimeOut);
}
private static readonly ContentDialogService ContentDialogService = new ContentDialogService();
public static async void ShowMessage(string content = "内容...", string title = "消息")
{
@@ -70,7 +36,7 @@ public static class MessageExtensions
string closeButtonText = "关闭")
{
return await ContentDialogService.ShowSimpleDialogAsync(
new SimpleContentDialogCreateOptions()
new SimpleContentDialogOptions()
{
Title = title,
Content = content,

View File

@@ -1,32 +0,0 @@
using WPFluent.Controls;
namespace WPFluent.Extensions;
/// <summary>
/// Extensions for the <see cref="INavigationService"/>.
/// </summary>
public static class NavigationServiceExtensions
{
/// <summary>
/// Sets the pane display mode of the navigation service.
/// </summary>
/// <param name="navigationService">The navigation service.</param>
/// <param name="paneDisplayMode">The pane display mode.</param>
/// <returns>Same <see cref="INavigationService"/> so multiple calls can be chained.</returns>
public static INavigationService SetPaneDisplayMode(
this INavigationService navigationService,
NavigationViewPaneDisplayMode paneDisplayMode)
{
INavigationView? navigationControl = navigationService.GetNavigationControl();
if(navigationControl is not null)
{
navigationControl.PaneDisplayMode = paneDisplayMode;
}
return navigationService;
}
}

View File

@@ -1,93 +0,0 @@
using WPFluent.Controls;
namespace WPFluent.Extensions;
/// <summary>
/// Extensions for the <see cref="ISnackbarService"/>.
/// </summary>
public static class SnackbarServiceExtensions
{
/// <summary>
/// Shows the snackbar. If it is already visible, firstly hides it for a moment, changes its content, and then shows
/// it again.
/// </summary>
/// <param name="snackbarService">The <see cref="ISnackbarService"/>.</param>
/// <param name="title">Name displayed on top of snackbar.</param>
/// <param name="message">Message inside the snackbar.</param>
public static void Show(this ISnackbarService snackbarService, string title, string message)
{ snackbarService.Show(title, message, ControlAppearance.Secondary, null, snackbarService.DefaultTimeOut); }
/// <summary>
/// Shows the snackbar. If it is already visible, firstly hides it for a moment, changes its content, and then shows
/// it again.
/// </summary>
/// <param name="snackbarService">The <see cref="ISnackbarService"/>.</param>
/// <param name="title">Name displayed on top of snackbar.</param>
/// <param name="message">Message inside the snackbar.</param>
/// <param name="Appearance">Display style.</param>
public static void Show(
this ISnackbarService snackbarService,
string title,
string message,
ControlAppearance appearance)
{ snackbarService.Show(title, message, appearance, null, snackbarService.DefaultTimeOut); }
/// <summary>
/// Shows the snackbar. If it is already visible, firstly hides it for a moment, changes its content, and then shows
/// it again.
/// </summary>
/// <param name="snackbarService">The <see cref="ISnackbarService"/>.</param>
/// <param name="title">Name displayed on top of snackbar.</param>
/// <param name="message">Message inside the snackbar.</param>
/// <param name="icon">Additional icon on the left.</param>
public static void Show(this ISnackbarService snackbarService, string title, string message, IconElement icon)
{ snackbarService.Show(title, message, ControlAppearance.Secondary, icon, snackbarService.DefaultTimeOut); }
/// <summary>
/// Shows the snackbar. If it is already visible, firstly hides it for a moment, changes its content, and then shows
/// it again.
/// </summary>
/// <param name="snackbarService">The <see cref="ISnackbarService"/>.</param>
/// <param name="title">Name displayed on top of snackbar.</param>
/// <param name="message">Message inside the snackbar.</param>
/// <param name="timeout">The time after which the snackbar should disappear.</param>
public static void Show(this ISnackbarService snackbarService, string title, string message, TimeSpan timeout)
{ snackbarService.Show(title, message, ControlAppearance.Secondary, null, timeout); }
/// <summary>
/// Shows the snackbar. If it is already visible, firstly hides it for a moment, changes its content, and then shows
/// it again.
/// </summary>
/// <param name="snackbarService">The <see cref="ISnackbarService"/>.</param>
/// <param name="title">Name displayed on top of snackbar.</param>
/// <param name="message">Message inside the snackbar.</param>
/// <param name="Appearance">Display style.</param>
/// <param name="timeout">The time after which the snackbar should disappear.</param>
public static void Show(
this ISnackbarService snackbarService,
string title,
string message,
ControlAppearance appearance,
TimeSpan timeout)
{ snackbarService.Show(title, message, appearance, null, timeout); }
/// <summary>
/// Shows the snackbar. If it is already visible, firstly hides it for a moment, changes its content, and then shows
/// it again.
/// </summary>
/// <param name="snackbarService">The <see cref="ISnackbarService"/>.</param>
/// <param name="title">Name displayed on top of snackbar.</param>
/// <param name="message">Message inside the snackbar.</param>
/// <param name="icon">Additional icon on the left.</param>
/// <param name="timeout">The time after which the snackbar should disappear.</param>
public static void Show(
this ISnackbarService snackbarService,
string title,
string message,
IconElement icon,
TimeSpan timeout)
{ snackbarService.Show(title, message, ControlAppearance.Secondary, icon, timeout); }
}

View File

@@ -9,13 +9,12 @@ namespace WPFluent.Extensions;
public static class StringExtensions
{
/// <summary>
/// Returns a value indicating whether a specified string occurs within this string, using the specified comparison
/// rules.
/// 使用指定的比较规则,返回指定字符串是否出现在此字符串中的值。
/// </summary>
/// <param name="source">Source string.</param>
/// <param name="value">The string to seek.</param>
/// <param name="comparison">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns>true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.</returns>
/// <param name="source">源字符串。</param>
/// <param name="value">要查找的字符串。</param>
/// <param name="comparison">枚举值之一,用于指定比较中使用的规则。</param>
/// <returns>如果参数 value 出现在该字符串中,或 value 为空字符串 ("") 则为 true否则为 false</returns>
[Pure]
public static bool Contains(this string source, string value, StringComparison comparison)
{ return source.IndexOf(value, comparison) >= 0; }

View File

@@ -11,11 +11,10 @@ namespace WPFluent.Extensions;
public static class TextBlockFontTypographyExtensions
{
/// <summary>
/// Converts the typography type enumeration to the name of the resource that represents it.
/// typography type 枚举转换为表示它的资源的名称。
/// </summary>
/// <returns>
/// Name of the resource matching the <see cref="FontTypography"/>. <see cref="ArgumentOutOfRangeException"/>
/// otherwise.
/// <see cref="FontTypography"/> 匹配的资源名称。否则<see cref="ArgumentOutOfRangeException"/>
/// </returns>
public static string ToResourceValue(this FontTypography typography)
{

View File

@@ -5,15 +5,13 @@ using WPFluent.Controls;
namespace WPFluent.Extensions;
/// <summary>
/// Extension that converts the text color type enumeration to the name of the resource that represents it.
/// </summary>
public static class TextColorExtensions
{
/// <summary>
/// Converts the text color type enumeration to the name of the resource that represents it.
/// 将文本颜色类型枚举转换为代表该类型的资源名称。
/// </summary>
/// <returns>Name of the resource matching the <see cref="TextColor"/>. <see cref="ArgumentOutOfRangeException"/> otherwise.</returns>
/// <returns> <see cref="TextColor"/> 匹配的资源名称,否则<see cref="ArgumentOutOfRangeException"/> </returns>
public static string ToResourceValue(this TextColor textColor)
{
return textColor switch

View File

@@ -1,57 +0,0 @@
namespace WPFluent.Extensions;
/// <summary>
/// Extensions for <see cref="Uri"/> class.
/// </summary>
public static class UriExtensions
{
/// <summary>
/// Append provided segments to the <see cref="Uri"/>.
/// </summary>
public static Uri Append(this Uri uri, params string[] segments)
{
if(!uri.IsAbsoluteUri)
{
return uri; // or throw?
}
return new Uri(
segments.Aggregate(
uri.AbsoluteUri,
(current, path) => $"{current.TrimEnd('/').TrimEnd('\\')}/{path.TrimStart('/').TrimStart('\\')}"));
}
/// <summary>
/// Append new <see cref="Uri"/> to the <see cref="Uri"/>.
/// </summary>
public static Uri Append(this Uri uri, Uri value)
{
return new Uri(
$"{uri.ToString().TrimEnd('/').TrimEnd('\\')}/{value.ToString().TrimStart('/').TrimStart('\\')}",
UriKind.RelativeOrAbsolute);
}
/// <summary>
/// Determines whether the end of <see cref="Uri"/> is equal to provided value.
/// </summary>
public static bool EndsWith(this Uri uri, string value) { return uri.ToString().EndsWith(value); }
/// <summary>
/// Removes last segment of the <see cref="Uri"/>.
/// </summary>
public static Uri TrimLastSegment(this Uri uri)
{
if(uri.Segments.Length < 2)
{
return uri;
}
var uriLastSegmentLength = uri.Segments[^1].Length;
var uriOriginalString = uri.ToString();
return new Uri(uriOriginalString[..^uriLastSegmentLength], UriKind.RelativeOrAbsolute);
}
}

View File

@@ -9,17 +9,17 @@ namespace WPFluent.Extensions
public static T FindEqualElement<T>(DependencyObject source, DependencyObject element)
where T : DependencyObject
{
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++)
{
var child = VisualTreeHelper.GetChild(source, i);
if(child != null && child is T && child == element)
if (child != null && child is T t && child == element)
{
return (T)child;
return t;
}
else
{
var childOfChild = FindVisualChild<T>(child);
if(childOfChild != null)
if (childOfChild != null)
{
return childOfChild;
}
@@ -37,16 +37,16 @@ namespace WPFluent.Extensions
public static T FindParent<T>(this DependencyObject i_dp) where T : DependencyObject
{
var dobj = VisualTreeHelper.GetParent(i_dp);
if(dobj != null)
if (dobj != null)
{
if(dobj is T t)
if (dobj is T t)
{
return t;
}
else
{
dobj = FindParent<T>(dobj);
if(dobj is not null and T)
if (dobj is not null and T)
{
return (T)dobj;
}
@@ -58,16 +58,16 @@ namespace WPFluent.Extensions
public static T FindParent<T>(this DependencyObject i_dp, string elementName) where T : DependencyObject
{
var dobj = VisualTreeHelper.GetParent(i_dp);
if(dobj != null)
if (dobj != null)
{
if(dobj is T && ((System.Windows.FrameworkElement)(dobj)).Name.Equals(elementName))
if (dobj is T && ((System.Windows.FrameworkElement)(dobj)).Name.Equals(elementName))
{
return (T)dobj;
}
else
{
dobj = FindParent<T>(dobj);
if(dobj is not null and T)
if (dobj is not null and T)
{
return (T)dobj;
}
@@ -84,15 +84,15 @@ namespace WPFluent.Extensions
/// <returns></returns>
public static T FindVisualChild<T>(this DependencyObject obj) where T : DependencyObject
{
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if(child is not null and T)
if (child is not null and T)
return (T)child;
else
{
var childOfChild = FindVisualChild<T>(child);
if(childOfChild != null)
if (childOfChild != null)
return childOfChild;
}
}
@@ -107,17 +107,17 @@ namespace WPFluent.Extensions
/// <returns></returns>
public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject
{
if(depObj != null)
if (depObj != null)
{
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if(child is not null and T)
if (child is not null and T)
{
yield return (T)child;
}
foreach(var childOfChild in FindVisualChildren<T>(child))
foreach (var childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
@@ -136,14 +136,14 @@ namespace WPFluent.Extensions
try
{
List<T> TList = [];
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if(child is not null and T)
if (child is not null and T)
{
TList.Add((T)child);
var childOfChildren = FindVisualChildrenEx<T>(child);
if(childOfChildren != null)
if (childOfChildren != null)
{
TList.AddRange(childOfChildren);
}
@@ -151,14 +151,15 @@ namespace WPFluent.Extensions
else
{
var childOfChildren = FindVisualChildrenEx<T>(child);
if(childOfChildren != null)
if (childOfChildren != null)
{
TList.AddRange(childOfChildren);
}
}
}
return TList;
} catch
}
catch
{
return null;
}
@@ -174,21 +175,21 @@ namespace WPFluent.Extensions
public static childItem FindVisualElement<childItem>(this DependencyObject obj, string elementName)
where childItem : DependencyObject
{
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if(child != null &&
if (child != null &&
child is childItem &&
((System.Windows.FrameworkElement)(child)).Name.Equals(elementName))
return (childItem)child;
else
{
IEnumerator j = FindVisualChildren<childItem>(child).GetEnumerator();
while(j.MoveNext())
while (j.MoveNext())
{
var childOfChild = (childItem)j.Current;
if(childOfChild != null && !(childOfChild as FrameworkElement).Name.Equals(elementName))
if (childOfChild != null && !(childOfChild as FrameworkElement).Name.Equals(elementName))
{
FindVisualElement<childItem>(childOfChild, elementName);
}
@@ -212,11 +213,11 @@ namespace WPFluent.Extensions
{
DependencyProperty prop = null;
if(type != null)
if (type != null)
{
var fieldInfo = type.GetField($"{propertyName}Property", BindingFlags.Static | BindingFlags.Public);
if(fieldInfo != null)
if (fieldInfo != null)
{
prop = fieldInfo.GetValue(null) as DependencyProperty;
}
@@ -235,7 +236,7 @@ namespace WPFluent.Extensions
{
DependencyProperty prop = null;
if(o != null)
if (o != null)
{
prop = GetDependencyProperty(o.GetType(), propertyName);
}
@@ -251,33 +252,35 @@ namespace WPFluent.Extensions
/// </summary>
public static object GetPropertyValue(this object obj, string path)
{
if(obj == null)
if (obj == null)
return string.Empty;
var flag = !string.IsNullOrEmpty(path);
object result;
if(flag)
if (flag)
{
var property = obj.GetType().GetProperty(path);
var flag2 = property != null;
if(flag2)
if (flag2)
{
try
{
result = property.GetValue(obj, null);
return result;
} catch
}
catch
{
try
{
var type = obj.GetType();
var mInfo = type.GetMethod($"get_{path}");
if(mInfo != null)
if (mInfo != null)
{
result = mInfo.Invoke(obj, null);
return result;
}
} catch
}
catch
{
}
}
@@ -306,18 +309,18 @@ namespace WPFluent.Extensions
/// <returns></returns>
public static bool SetIfDefault<T>(this DependencyObject o, DependencyProperty property, T value)
{
if(o == null)
if (o == null)
throw new ArgumentNullException(nameof(o), "DependencyObject cannot be null");
if(property == null)
if (property == null)
throw new ArgumentNullException(nameof(property), "DependencyProperty cannot be null");
if(!property.PropertyType.IsAssignableFrom(typeof(T)))
if (!property.PropertyType.IsAssignableFrom(typeof(T)))
{
throw new ArgumentException(
$"Expected {property.Name} to be of type {typeof(T).Name} but was {property.PropertyType}");
}
if(DependencyPropertyHelper.GetValueSource(o, property).BaseValueSource == BaseValueSource.Default)
if (DependencyPropertyHelper.GetValueSource(o, property).BaseValueSource == BaseValueSource.Default)
{
o.SetValue(property, value);