Интеграция справки в приложение WPF

Каковы возможные подходы к интеграции локальной (не онлайновой) справки в приложение WPF? Это было бы больше похоже на руководство, но я бы хотел как-то интегрировать его.

РЕДАКТИРОВАТЬ: только что нашел http://wordtoxaml.codeplex.com/, я попробую это. Он преобразует документ Word в xaml, который я могу отображать в WPF.

РЕДАКТИРОВАТЬ 2: Я нашел рабочее решение: написать руководство в слово, сохранить как 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 ответа

Решение

Мы используем RoboHelp и генерируем файл chm, иногда называемый файлом справки HTML..NET Framework Help у класса есть метод ShowHelp что вы звоните, передавая файл CHM и тему, которую вы хотите отобразить. Вы можете настроить отображение по заголовку темы, идентификатору и т. Д. Мы отображаем, используя заголовок темы, поэтому вызов выглядит следующим образом:

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");
}

Что еще приятнее, теперь вы можете прикрепить справку к любому 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 на окнах или любом другом элементе, он получает контекстную помощь.

У меня была похожая потребность, за исключением того, что мне нужно было только подключить клавишу F1 к нашему существующему коду помощи.

Я закончил тем, что вытащил микс из примерно 5 разных страниц Stackru, поэтому я выкладываю его здесь на тот случай, если кому-то еще понадобится подобное.

В моем MainWindow.xaml я добавил KeyBinding в inputBindings, чтобы связать 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));
        }
    }

Я надеюсь, что это поможет кому-то с подобной проблемой, и я не думаю, что это стоило того, чтобы это сделать самостоятельно. Приветствия.

Посмотрите на этот пост, это то, что вы ищете?

Существует много видов помощи: ручная, контекстно-зависимая, ...

Другие вопросы по тегам