2011-05-27 56 views

回答

6

我不知道任何在WPF內置支持顯示CHM文件。我所做的是添加一個InputGesture,將F1按鍵連接到Application.Help命令,並在Windows CommandBindings中爲Application.Help Command添加一個處理程序。下面是一個示例代碼:

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


} 
0

基於這種方法,我做了以下這樣我就可以採取OnlineHelpViewModel的優勢,我有那通過RelayCommand管理幫助。當按F1時,通過這種方法,視圖模型上的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的地方,你想:這在SOURCE視圖模型到這個窗口綁定命令

... 
     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)); 

確定。

然後在此窗口的命令處理程序(這也許可以內嵌在某種程度上)

private void HelpCommand_Executed(object sender, ExecutedRoutedEventArgs e) 
    { 
     this.HelpCommand.Execute(HelpContextGuid); 
    } 

,你現在可以OnlineHelpViewModel從任何地方撥打一個help命令,它可以任意複雜,過於依賴。需要注意的是DP HelpContextGuid傳遞 - 它是由命令來決定如何處理它,但RelayCommmand <串>想要一個說法

本身看起來像(在源視圖模型)

... 
ShowApplicationHelpCommand = new RelayCommand<string>(
      (h) => { ShowApplicationHelp(h); }, 
      (h) => CanShowApplicationHelpCommand); 

... 
命令做

和它調用的方法是無論如何顯示幫助,

在我的情況下,我創建了一個RadWindow等,並使用BackSpin Software HelpLoader與XamlHelp填充它。該幫助文件是由Word與Twister4Word生成的。 所有這些都對我的應用程序特別重要,所以你可能會做一些其他的事情來製作幫助窗口。下面是構造函數:

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

您可以在這裏找到

http://www.backspinsoftware.com/site/Default.aspx

它能夠從Word文檔編譯的幫助下旋幫助創作工具。