在WPF应用程序中集成帮助

19

如何将本地(非在线)帮助集成到WPF应用程序中?这更像是一本手册,但我希望以某种方式将其集成。

编辑:刚刚发现http://wordtoxaml.codeplex.com,我会尝试使用它。它可以将Word文档转换为XAML,然后我可以在WPF中显示。

编辑2:我找到了一个可行的解决方案:在Word中编写手册,另存为XPS格式,并使用https://web.archive.org/web/20111116005415/http://www.umutluoglu.com/english/post/2008/12/20/Showing-XPS-Documents-with-DocumentViewer-Control-in-WPF.aspx显示它。


我必须创建它,但我更喜欢使用HTML。 - L-Four
你的编辑2链接有问题。 - crh225
2个回答

26

我们使用RoboHelp并生成chm文件,有时也称为HTML帮助文件。.NET Framework的Help类有一个方法ShowHelp,您可以调用它,并传递chm文件和要显示的主题。您可以按主题标题、ID等方式进行显示。我们使用主题标题进行显示,因此调用看起来像这样:

System.Windows.Forms.Help.ShowHelp(null, "Help/ExiaProcess.chm", HelpNavigator.Topic, helpTopic);

接下来,您可以创建一个名为HelpProvider的类,该类创建了一个名为HelpTopic的附加属性。这使您可以将HelpTopic属性附加到任何FrameworkElement上。该类还使用静态构造函数将内置的F1帮助命令挂钩到命令处理程序上,以从源中检索附加属性并打开帮助文档。

using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;

/// <summary>
/// Provider class for online help.  
/// </summary>
public class HelpProvider
{
    #region Fields

    /// <summary>
    /// Help topic dependency property. 
    /// </summary>
    /// <remarks>This property can be attached to an object such as a form or a textbox, and 
    /// can be retrieved when the user presses F1 and used to display context sensitive help.</remarks>
    public static readonly DependencyProperty HelpTopicProperty = 
        DependencyProperty.RegisterAttached("HelpTopic", typeof(string), typeof(HelpProvider));

    #endregion Fields

    #region Constructors

    /// <summary>
    /// Static constructor that adds a command binding to Application.Help, binding it to 
    /// the CanExecute and Executed methods of this class. 
    /// </summary>
    /// <remarks>With this in place, when the user presses F1 our help will be invoked.</remarks>
    static HelpProvider()
    {
        CommandManager.RegisterClassCommandBinding(
            typeof(FrameworkElement),
            new CommandBinding(
                ApplicationCommands.Help,
                new ExecutedRoutedEventHandler(ShowHelpExecuted),
                new CanExecuteRoutedEventHandler(ShowHelpCanExecute)));
    }

    #endregion Constructors

    #region Methods

    /// <summary>
    /// Getter for <see cref="HelpTopicProperty"/>. Get a help topic that's attached to an object. 
    /// </summary>
    /// <param name="obj">The object that the help topic is attached to.</param>
    /// <returns>The help topic.</returns>
    public static string GetHelpTopic(DependencyObject obj)
    {
        return (string)obj.GetValue(HelpTopicProperty);
    }

    /// <summary>
    /// Setter for <see cref="HelpTopicProperty"/>. Attach a help topic value to an object. 
    /// </summary>
    /// <param name="obj">The object to which to attach the help topic.</param>
    /// <param name="value">The value of the help topic.</param>
    public static void SetHelpTopic(DependencyObject obj, string value)
    {
        obj.SetValue(HelpTopicProperty, value);
    }

    /// <summary>
    /// Show help table of contents. 
    /// </summary>
    public static void ShowHelpTableOfContents()
    {
        System.Windows.Forms.Help.ShowHelp(null, "Help/ExiaProcess.chm", HelpNavigator.TableOfContents);
    }

    /// <summary>
    /// Show a help topic in the online CHM style help. 
    /// </summary>
    /// <param name="helpTopic">The help topic to show. This must match exactly with the name 
    /// of one of the help topic's .htm files, without the .htm extention and with spaces instead of underscores
    /// in the name. For instance, to display the help topic "This_is_my_topic.htm", pass the string "This is my topic".</param>
    /// <remarks>You can also pass in the help topic with the underscore replacement already done. You can also 
    /// add the .htm extension. 
    /// Certain characters other than spaces are replaced by underscores in RoboHelp help topic names. 
    /// This method does not yet account for all those replacements, so if you really need to find a help topic
    /// with one or more of those characters, do the underscore replacement before passing the topic.</remarks>
    public static void ShowHelpTopic(string helpTopic)
    {
        // Strip off trailing period.
        if (helpTopic.IndexOf(".") == helpTopic.Length - 1)
            helpTopic = helpTopic.Substring(0, helpTopic.Length - 1);

        helpTopic = helpTopic.Replace(" ", "_").Replace("\\", "_").Replace("/", "_").Replace(":", "_").Replace("*", "_").Replace("?", "_").Replace("\"", "_").Replace(">", "_").Replace("<", "_").Replace("|", "_") + (helpTopic.IndexOf(".htm") == -1 ? ".htm" : "");
        System.Windows.Forms.Help.ShowHelp(null, "Help/ExiaProcess.chm", HelpNavigator.Topic, helpTopic);
    }

    /// <summary>
    /// Whether the F1 help command can execute. 
    /// </summary>
    private static void ShowHelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        FrameworkElement senderElement = sender as FrameworkElement;

        if (HelpProvider.GetHelpTopic(senderElement) != null)
            e.CanExecute = true;
    }

    /// <summary>
    /// Execute the F1 help command. 
    /// </summary>
    /// <remarks>Calls ShowHelpTopic to show the help topic attached to the framework element that's the 
    /// source of the call.</remarks>
    private static void ShowHelpExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        ShowHelpTopic(HelpProvider.GetHelpTopic(sender as FrameworkElement));
    }

    #endregion Methods
}

有了这个设置,你可以像这样从代码中调用帮助文档:

private void HelpButton_Click(object sender, RoutedEventArgs e)
{
    Help.HelpProvider.ShowHelpTopic("License Key Dialog");
}

更好的是,现在您可以像这样将帮助附加到 UI 中的任何 FrameworkElement,

<Window name="MainWin"
    ...
    ...
    xmlns:help="clr-namespace:ExiaProcess.UI.Help"
    ...
    ...
    help:HelpProvider.HelpTopic="Welcome to YourApp" />      
    ...
    ...
    <TextBox help:HelpProvider.HelpTopic="Bug Title" />
    ...
    ...
    <ComboBox help:HelpProvider.HelpTopic="User Drop Down"/>
    ...

现在当用户在Windows或任何元素上按F1时,他们将获得上下文相关的帮助。


2
对于其他阅读此文的人,我建议使用Nigel的类和Microsoft HTML Help Workshop创建帮助文件。这非常容易且是一个不错的特性。观看 此视频 以了解如何创建.chm文件。谢谢Nigel。 - Jordan Carroll
附加属性HelpTopicProperty的名称应为HelpTopic,而不是HelpString。 - slfan
2
这个答案不是针对WPF而是Windows Forms吗? - Chris McCowan

5

我有类似的需求,但我只需要将F1键与我们现有的帮助代码连接起来。

最终我从大约5个不同的StackOverflow页面中提取了一些混合内容,因此我在这里发布以便其他人有类似的需求时能够使用。

在我的MainWindow.xaml中,我添加了一个KeyBinding到inputBindings中,将F1键连接到一个ICommand:

<Window.InputBindings>
    (other bindings here...)
    <KeyBinding Key="F1" Command="{Binding Path=ShowHelpCommand}"/>
</Window.InputBindings>

然后在我的MainWindowViewModel.cs中,我添加了这个ICommand,它调用了我现有的Help代码。

    private ICommand _showHelpCommand;
    public ICommand ShowHelpCommand
    {
        get
        {
            return _showHelpCommand ??
                   (_showHelpCommand = new RelayCommand(p => DisplayCREHelp(), p => true));
        }
    }

希望这能帮助到遇到相似问题的人。

我很感兴趣,但我不确定我理解你的意思。RelayCommand()DisplayCREHelp()是什么? - InteXX
@InteXX:RelayCommand() 是一个 .Net 对象,DisplayCREHelp() 是我想在按下该窗体上的 F1 键时调用的方法名称。基本上,您可以使用 ICommand 和 RelayCommand 将键盘上的功能键与应用程序中的方法连接起来。 - CodeChops

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接