如何为VS 2008中内置的WPF应用程序附加帮助

问题描述

|| 我的应用程序有一个chm,我想将其附加到应用程序中,即当用户按F1键时,该项目的附加帮助就会打开。     

解决方法

        我不知道WPF中有任何内置支持来显示CHM文件。我要做的是添加一个InputGesture以将F1击键连接到Application.Help命令,并在Windows CommandBindings中为Application.Help命令添加一个处理程序。这是一个示例代码:
<Window x:Class=\"WpfTestApp.MainWindow\"
    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
    xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
    Title=\"MainWindow\" Height=\"350\" Width=\"525\" >
<Window.InputBindings>
    <KeyBinding Command=\"Help\" Key=\"F1\"/>
</Window.InputBindings>
<Window.CommandBindings>
    <CommandBinding Command=\"ApplicationCommands.Help\" Executed=\"HelpExecuted\" />
</Window.CommandBindings>
<Grid>

</Grid>
这是处理程序代码:
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void HelpExecuted(object sender,ExecutedRoutedEventArgs e)
    {
        System.Diagnostics.Process.Start(@\"C:\\MyProjectPath\\HelpFile.chm\");
    }


}
    ,        在WPF中使用F1帮助(CHM格式)     ,        基于这种方法,我执行了以下操作,以便可以利用通过RelayCommand管理帮助的OnlineHelpViewModel。当按下F1时,使用这种方法,将在viewmodel上调用RelayCommand,就像ia?按钮已被按下。换句话说,我们将F1绑定到RelayCommand。 本示例使用GalaSoft MvvmLight。
DependencyProperty on the MainWindow

public static DependencyProperty HelpCommandProperty =  DependencyProperty.Register(\"HelpCommand\",typeof(RelayCommand<string>),typeof(WindowExt),new PropertyMetadata(null));

    public RelayCommand<string> HelpCommand
    {
        get
        {
            return (RelayCommand<string>)GetValue(HelpCommandProperty);
        }
        set
        {
            SetValue(HelpCommandProperty,value);
        }
    }
OK,保存命令 现在在窗口加载事件或您喜欢的地方:
...
        Binding b2 = new Binding();
        b2.Source = ViewModelLocator.OnlineHelpViewModelStatic;
        b2.Path = new PropertyPath(\"ShowApplicationHelpCommand\");
        b2.Mode = BindingMode.OneWay;
        this.SetBinding(HelpCommandProperty,b2);


        var kb = new KeyBinding();
        kb.Key = Key.F1;
        kb.Command = HelpCommand;
        this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Help,HelpCommand_Executed));
单击确定,将SOURCE视图模型上的命令绑定到此窗口。 然后在此窗口上为命令处理程序(也许可以以某种方式内联)
private void HelpCommand_Executed(object sender,ExecutedRoutedEventArgs e)
    {
        this.HelpCommand.Execute(HelpContextGuid);
    }
现在您可以在任何地方调用OnlineHelpViewModel上的单个帮助命令,并且依赖于它,它也可以任意复杂。请注意,传递了DP HelpContextGuid-由命令决定如何处理它,但是RelayCommmand 需要一个参数 命令本身看起来像(在SOURCE Viewmodel上)
 ...
 ShowApplicationHelpCommand = new RelayCommand<string>(
            (h) => { ShowApplicationHelp(h); },(h) => CanShowApplicationHelpCommand);

 ...
它调用的方法就是显示帮助所需的一切, 就我而言,我创建了一个RadWindow,依此类推,并使用BackSpin软件HelpLoader用XamlHelp填充了它。帮助文件是使用Twister4Word从Word生成的。 所有这些都是我的应用程序所特有的,因此您可能会做其他事情来制作帮助窗口。这是构造函数:
   public MigratorHelpWindow()
    {
        // create local resources for desingn mode,so Blend can see the viewmodels
        if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        {
            App.CreateStaticResourcesForDesigner(this);
        }

        InitializeComponent();

        if (Application.Current.MainWindow != null)
        {
             var thm =  ThemeManager.FromName(Application.Current.FindResource(\"TelerikGlobalTheme\").ToString() ?? \"Office_Blue\");
            StyleManager.SetTheme(this,thm);
        }

        // window configuration
        MaxHeight = SystemParameters.WorkArea.Height;
        MaxWidth = SystemParameters.WorkArea.Width;

        Binding b = new Binding();
        b.Source = ViewModelLocator.OnlineHelpViewModelStatic;
        b.Path = new PropertyPath(\"ApplicationHelpFileName\");
        b.Mode = BindingMode.OneWay;

        this.SetBinding(ApplicationHelpFileNameProperty,b);

        if (String.IsNullOrEmpty(ApplicationHelpFileName))
        {
            UiHelpers.ShowError(\"No help file is available\",true);
            return;
        }


   // LOAD YOUR HELP HERE OR WHATEVER
   // LOAD YOUR HELP HERE OR WHATEVER
   // LOAD YOUR HELP HERE OR WHATEVER


        HelpLoader.Load(ApplicationHelpFileName);
        HelpLoader.Default.Owner = this;
        HelpLoader.Default.HelpLayout = HelpLayout.Standard;
        HelpLoader.Default.TocContainer = _mTOC;
        HelpLoader.Default.IndexContainer = _mIndex;
        HelpLoader.Default.TopicContainer = _mTopic;
        HelpLoader.Default.SearchContainer = _mSearch;
        HelpLoader.Default.FavoritesContainer = _mFavorites;
    }
您可以在此处找到BackSpin帮助创作工具 http://www.backspinsoftware.com/site/Default.aspx 它从Word文档生成已编译的帮助。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...