简单的 WPF 帮助






4.88/5 (11投票s)
为你的 WPF 应用程序添加简单的 HelpProvider 功能从未如此简单。
如果你像我一样,喜欢你的应用程序提供上下文相关的帮助,你可能已经尝试过 ApplicationCommands.Help
命令。为了简化将帮助集成到你的应用程序中,我编写了以下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using form = System.Windows.Forms;
using System.Windows.Media;
namespace HelpProvider
{
/// <summary>
/// This class provides the ability to easily attach Help functionality
/// to Framework elements. To use it, you need to
/// add a reference to the HelpProvider in your XAML. The FilenameProperty
/// is used to specify the name of the helpfile, and the KeywordProperty specifies
/// the keyword to be used with the search.
/// </summary>
/// <remarks>
/// The FilenameProperty can be at a higher level of the visual tree than
/// the KeywordProperty, so you don't need to set the filename each time.
/// </remarks>
public static class Help
{
/// <summary>
/// Initialize a new instance of <see cref="Help"/>.
/// </summary>
static Help()
{
// Rather than having to manually associate the Help command, let's take care
// of this here.
CommandManager.RegisterClassCommandBinding(typeof(FrameworkElement),
new CommandBinding(ApplicationCommands.Help,
new ExecutedRoutedEventHandler(Executed),
new CanExecuteRoutedEventHandler(CanExecute)));
}
#region Filename
/// <summary>
/// Filename Attached Dependency Property
/// </summary>
public static readonly DependencyProperty FilenameProperty =
DependencyProperty.RegisterAttached("Filename", typeof(string), typeof(Help));
/// <summary>
/// Gets the Filename property.
/// </summary>
public static string GetFilename(DependencyObject d)
{
return (string)d.GetValue(FilenameProperty);
}
/// <summary>
/// Sets the Filename property.
/// </summary>
public static void SetFilename(DependencyObject d, string value)
{
d.SetValue(FilenameProperty, value);
}
#endregion
#region Keyword
/// <summary>
/// Keyword Attached Dependency Property
/// </summary>
public static readonly DependencyProperty KeywordProperty =
DependencyProperty.RegisterAttached("Keyword", typeof(string), typeof(Help));
/// <summary>
/// Gets the Keyword property.
/// </summary>
public static string GetKeyword(DependencyObject d)
{
return (string)d.GetValue(KeywordProperty);
}
/// <summary>
/// Sets the Keyword property.
/// </summary>
public static void SetKeyword(DependencyObject d, string value)
{
d.SetValue(KeywordProperty, value);
}
#endregion
#region Helpers
private static void CanExecute(object sender, CanExecuteRoutedEventArgs args)
{
FrameworkElement el = sender as FrameworkElement;
if (el != null)
{
string fileName = FindFilename(el);
if (!string.IsNullOrEmpty(fileName))
args.CanExecute = true;
}
}
private static void Executed(object sender, ExecutedRoutedEventArgs args)
{
// Call ShowHelp.
DependencyObject parent = args.OriginalSource as DependencyObject;
string keyword = GetKeyword(parent);
if (!string.IsNullOrEmpty(keyword))
{
form.Help.ShowHelp(null, FindFilename(parent), keyword);
}
else
{
form.Help.ShowHelp(null, FindFilename(parent));
}
}
private static string FindFilename(DependencyObject sender)
{
if (sender != null)
{
string fileName = GetFilename(sender);
if (!string.IsNullOrEmpty(fileName))
return fileName;
return FindFilename(VisualTreeHelper.GetParent(sender));
}
return null;
}
#endregion
}
}
使用起来非常简单,在你的 XAML 中设置 Filename
,并为你的 FrameworkElement
项目添加任何你需要搜索的关键词。这种方法的优势在于,你可以根据需要将 UI 的不同部分绑定到不同的帮助文件。
<Window
x:Class="HelpSample.Window1"
xmlns="<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>"
xmlns:x="<a href="http://schemas.microsoft.com/winfx/2006/xaml">
http://schemas.microsoft.com/winfx/2006/xaml</a>"
Title="Window1" Height="300" Width="300"
xmlns:help="clr-namespace:HelpProvider;assembly=HelpProvider"
help:Help.Filename="MyHelpfile.chm"
>
希望这能像帮助我一样帮助到你。