在WPF应用程序中集成帮助

时间:2011-02-25 11:06:11

标签: wpf

在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显示

3 个答案:

答案 0 :(得分:13)

我们使用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("HelpString", 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"/>
    ...

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

答案 1 :(得分:2)

拥有look at this post,这就是您要找的东西吗?

有许多类型的帮助:手动,上下文敏感,......

答案 2 :(得分:2)

我有类似的需求,除了我只需要将F1键连接到现有的帮助代码。

我最终拉出了大约5个不同的StackOverflow页面,所以我把它放在这里以防其他人有类似需求。

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

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

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

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

我希望这可以帮助任何有类似问题的人,我不认为这是值得的。欢呼声。