| 1 | namespace WixToolset.Samples.EmbeddedUI |
| 2 | { |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Configuration; |
| 6 | using System.Threading; |
| 7 | using System.Windows; |
| 8 | using System.Windows.Threading; |
| 9 | using WixToolset.Dtf.WindowsInstaller; |
| 10 | using Application = System.Windows.Application; |
| 11 | |
| 12 | public class SampleEmbeddedUI : IEmbeddedUI |
| 13 | { |
| 14 | private bool isMaintenance; |
| 15 | private Thread appThread; |
| 16 | private Application app; |
| 17 | private SetupWizard setupWizard; |
| 18 | private ManualResetEvent installStartEvent; |
| 19 | private ManualResetEvent installExitEvent; |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Initializes the embedded UI. |
| 23 | /// </summary> |
| 24 | /// <param name="session">Handle to the installer which can be used to get and set properties. |
| 25 | /// The handle is only valid for the duration of this method call.</param> |
| 26 | /// <param name="resourcePath">Path to the directory that contains all the files from the MsiEmbeddedUI table.</param> |
| 27 | /// <param name="internalUILevel">On entry, contains the current UI level for the installation. After this |
| 28 | /// method returns, the installer resets the UI level to the returned value of this parameter.</param> |
| 29 | /// <returns>True if the embedded UI was successfully initialized; false if the installation |
| 30 | /// should continue without the embedded UI.</returns> |
| 31 | /// <exception cref="InstallCanceledException">The installation was canceled by the user.</exception> |
| 32 | /// <exception cref="InstallerException">The embedded UI failed to initialize and |
| 33 | /// causes the installation to fail.</exception> |
| 34 | public bool Initialize(Session session, string resourcePath, ref InstallUIOptions internalUILevel) |
| 35 | { |
| 36 | if (session != null) |
| 37 | { |
| 38 | if ((internalUILevel & InstallUIOptions.Full) != InstallUIOptions.Full) |
| 39 | { |
| 40 | // Don't show custom UI when the UI level is set to basic. |
| 41 | return false; |
| 42 | |
| 43 | // An embedded UI could display an alternate dialog sequence for reduced or |
| 44 | // basic modes, but it's not implemented here. We'll just fall back to the |
| 45 | // built-in MSI basic UI. |
| 46 | } |
| 47 | |
| 48 | if (String.Equals(session["REMOVE"], "All", StringComparison.OrdinalIgnoreCase)) |
| 49 | { |
| 50 | // Don't show custom UI when uninstall was specified on the command line. |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | this.isMaintenance = session.EvaluateCondition("Installed"); |
| 55 | } |
| 56 | |
| 57 | // Start the setup wizard on a separate thread. |
| 58 | this.installStartEvent = new ManualResetEvent(false); |
| 59 | this.installExitEvent = new ManualResetEvent(false); |
| 60 | this.appThread = new Thread(this.Run); |
| 61 | this.appThread.SetApartmentState(ApartmentState.STA); |
| 62 | this.appThread.Start(); |
| 63 | |
| 64 | // Wait for the setup wizard to either kickoff the install or prematurely exit. |
| 65 | int waitResult = WaitHandle.WaitAny(new WaitHandle[] { this.installStartEvent, this.installExitEvent }); |
| 66 | if (waitResult == 1) |
| 67 | { |
| 68 | // The setup wizard set the exit event instead of the start event. Cancel the installation. |
| 69 | throw new InstallCanceledException(); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | switch (this.setupWizard.Operation) |
| 74 | { |
| 75 | case SetupOperationType.Repair: |
| 76 | session["REINSTALL"] = "ALL"; |
| 77 | break; |
| 78 | case SetupOperationType.Uninstall: |
| 79 | session["REMOVE"] = "ALL"; |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | // Start the installation with a silenced internal UI. |
| 84 | // This "embedded external UI" will handle message types except for source resolution. |
| 85 | internalUILevel = InstallUIOptions.NoChange | InstallUIOptions.SourceResolutionOnly; |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Processes information and progress messages sent to the user interface. |
| 92 | /// </summary> |
| 93 | /// <param name="messageType">Message type.</param> |
| 94 | /// <param name="messageRecord">Record that contains message data.</param> |
| 95 | /// <param name="buttons">Message box buttons.</param> |
| 96 | /// <param name="icon">Message box icon.</param> |
| 97 | /// <param name="defaultButton">Message box default button.</param> |
| 98 | /// <returns>Result of processing the message.</returns> |
| 99 | public MessageResult ProcessMessage(InstallMessage messageType, Record messageRecord, |
| 100 | MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultButton) |
| 101 | { |
| 102 | // Synchronously send the message to the setup wizard window on its thread. |
| 103 | object result = this.setupWizard.Dispatcher.Invoke(DispatcherPriority.Send, |
| 104 | new Func<MessageResult>(delegate() |
| 105 | { |
| 106 | return this.setupWizard.ProcessMessage(messageType, messageRecord, buttons, icon, defaultButton); |
| 107 | })); |
| 108 | return (MessageResult) result; |
| 109 | } |
| 110 | |
| 111 | /// <summary> |
| 112 | /// Shuts down the embedded UI at the end of the installation. |
| 113 | /// </summary> |
| 114 | /// <remarks> |
| 115 | /// If the installation was canceled during initialization, this method will not be called. |
| 116 | /// If the installation was canceled or failed at any later point, this method will be called at the end. |
| 117 | /// </remarks> |
| 118 | public void Shutdown() |
| 119 | { |
| 120 | // Wait for the user to exit the setup wizard. |
| 121 | this.setupWizard.Dispatcher.BeginInvoke(DispatcherPriority.Normal, |
| 122 | new Action(delegate() |
| 123 | { |
| 124 | this.setupWizard.EnableExit(); |
| 125 | })); |
| 126 | this.appThread.Join(); |
| 127 | } |
| 128 | |
| 129 | /// <summary> |
| 130 | /// Creates the setup wizard and runs the application thread. |
| 131 | /// </summary> |
| 132 | private void Run() |
| 133 | { |
| 134 | this.app = new Application(); |
| 135 | this.setupWizard = new SetupWizard(this.installStartEvent, this.isMaintenance); |
| 136 | this.setupWizard.InitializeComponent(); |
| 137 | this.app.Run(this.setupWizard); |
| 138 | this.installExitEvent.Set(); |
| 139 | } |
| 140 | } |
| 141 | } |