Local help with disconnected OOB Silverlight applications…

Yes, displaying an HTML Help, .CHM, file…

A person asked me how to do this and before spending time explaining the approach, I’ll just show some sample code for now. If there’s any question/comment, I’ll add more text:

<UserControl x:Class="HelpSystemTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

   <Grid>
      <toolkit:BusyIndicator Name="BusyDownloadingHelpFile" BusyContent="Downloading Help File">
         <StackPanel>
            <Button Content="Install" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Install" VerticalAlignment="Top" Width="75" Click="Install_Click" />
            <Button Content="Launch Help" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="LaunchHelp" />
         </StackPanel>
      </toolkit:BusyIndicator>
   </Grid>
</UserControl>

public partial class MainPage : UserControl {
   public MainPage() {
      InitializeComponent();
      if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
         DownloadHelpFile();
   }

   string _helpFileFolder = null;
   const string _helpFileName = "HelpFile.chm";
   string _helpFilePath = null;

   private void LaunchHelp(object sender, RoutedEventArgs e0) {
      if (!Application.Current.IsRunningOutOfBrowser
            || !Application.Current.HasElevatedPermissions
            || !AutomationFactory.IsAvailable) {
         MessageBox.Show("The Windows help system cannot be launched in the current context");
         return;
      }
      DownloadHelpFile(() => {
         using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
            shell.Exec("HH.EXE \"" + _helpFilePath + "\"");
      });
   }

   void DownloadHelpFile(Action actionWhenHelpFileOnDisk = null) {
      try {
         if (_helpFileFolder == null) {
            _helpFileFolder = IoPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyCompany");
            _helpFilePath = IoPath.Combine(_helpFileFolder, _helpFileName);
         }

         if (Directory.Exists(_helpFileFolder)) {
            if (File.Exists(_helpFilePath)) {
               if (actionWhenHelpFileOnDisk != null)
                  actionWhenHelpFileOnDisk();
               return;
            }
         } else
            Directory.CreateDirectory(_helpFileFolder);
         if (actionWhenHelpFileOnDisk != null) // We're NOT silently downloading in the background...
            BusyDownloadingHelpFile.IsBusy = true;
         var wc = new WebClient();
         wc.OpenReadCompleted += (s, e) => {
            using (var toStream = new FileStream(_helpFilePath, FileMode.CreateNew)) {
               e.Result.CopyTo(toStream);
            }
            BusyDownloadingHelpFile.IsBusy = false;
            actionWhenHelpFileOnDisk();
         };
         wc.OpenReadAsync(new Uri(_helpFileName, UriKind.Relative));
      } catch (Exception ex) {
         BusyDownloadingHelpFile.IsBusy = false;
         MessageBox.Show(ex.Message + ex.StackTrace, "Exception while storing help file locally", MessageBoxButton.OK);
      }
   }

   private void Install_Click(object sender, RoutedEventArgs e) {
      if (Application.Current.InstallState == InstallState.NotInstalled)
         Application.Current.Install();
   }
}
Advertisements
This entry was posted in Computers and Internet and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s