2016-08-18 43 views
0

我希望能夠從對話框中選擇文件(而不是目錄)。單擊按鈕後從自定義操作調用OpenFileDialog

爲此,我創建了一個調用「OpenFileDialog」的自定義操作。 我打電話從一個按鈕控件的自定義操作,它好像它得到完全卡住(我不得不殺了微星的過程中,爲了繼續)

這裏是我的自定義操作:

[CustomAction] 
     public static ActionResult BrowseForLicenseFile(Session session) 
     { 
      try 
      { 
       session.Log("Begin BrowseForLicenseFile"); 

       session["LICENSEFILE"] = string.Empty; 

       var dialog = new OpenFileDialog 
       { 
        Filter = "License File (license.dat)|license.dat", 
        InitialDirectory = @"C:\", 
        Title = "Select License File: license.dat" 
       }; 

       if (dialog.ShowDialog() == DialogResult.OK) 
       { 
        session["LICENSEFILE"] = dialog.FileName; 
        session.Log("setting property LICENSEFILE={0}", dialog.FileName); 
       } 
       else 
        return ActionResult.Failure; 
      } 
      catch (Exception ex) 
      { 
       session.Log("ERROR in custom action BrowseForLicenseFile {0}", ex.ToString()); 
       return ActionResult.Failure; 
      } 

      return ActionResult.Success; 
     } 

按鈕的代碼(我甚至嘗試評論最後兩個「發佈」線和只有第一個離開:

<Control Id="BrowseLicenseFile" Type="PushButton" Width="75" Height="17" X="274" Y="105" Text="{\VSI_MS_Sans_Serif13.0_0_0}Browse"> 
      <Publish Event="DoAction" Value="BrowseForLicenseFileCA" Order="1" >1</Publish> 
      <Publish Property="LICENSEFILE" Value="[LICENSEFILE]" Order="2">1</Publish> 
      <Publish Event="DoAction" Value="CopyLicenseFileToCommonDirCA" Order="3">1</Publish> 
     </Control> 

這裏是自定義操作是如何宣稱:

<CustomAction Id="BrowseForLicenseFileCA" BinaryKey="ServerInstallerCustomActions.CA" DllEntry="BrowseForLicenseFile" Execute="immediate" Return="check" /> 
    <CustomAction Id="CopyLicenseFileToCommonDirCA" BinaryKey="ServerInstallerCustomActions.CA" DllEntry="CopyLicenseFileToCommonDir" Execute="immediate" Return="check" /> 
<Binary Id="ServerInstallerCustomActions.CA" SourceFile="$(var.Server.TargetDir)\ServerInstallerCustomActions.CA.dll" /> 

屬性被聲明爲空:

<Property Id="LICENSEFILE" /> 

我從具有管理員權限的命令提示符下運行MSI。

希望如果有人能夠找出爲什麼這不起作用。

+1

你試過嗎? http://stackoverflow.com/questions/37137579/file-browse-dialog-in-wix-installer – sttaq

+0

謝謝!它現在有效。我不知道我不得不從一個單獨的線程調用OpenFileDialog ....但是現在我沒有得到設置到編輯控件中的選定值... – Dafna

+0

是否將'session'對象傳遞給線程? – sttaq

回答

0

我把答案給別人使用:

[CustomAction] 
      public static ActionResult BrowseForLicenseFile(Session session) 
      { 
       try 
       { 
        Log(session, "Begin BrowseForLicenseFile"); 

        session["LICENSEFILE"] = string.Empty; 

        var task = new Thread(() => GetFile(session)); 
        task.SetApartmentState(ApartmentState.STA); 
        task.Start(); 
        task.Join(); 
        Log(session, "End OpenFileChooser Custom Action"); 

       } 
       catch (Exception ex) 
       { 
        Log(session, "ERROR in custom action BrowseForLicenseFile {0}", ex.ToString()); 

        return ActionResult.Failure; 
       } 

       return ActionResult.Success; 
      } 
    private static void GetFile(Session session) 
    { 
     var dialog = new OpenFileDialog 
     { 
      Filter = "License File (license.dat)|license.dat", 
      InitialDirectory = @"C:\", 
      Title = "Select License File: license.dat" 
     }; 

     if (dialog.ShowDialog() == DialogResult.OK) 
     { 
      session["LICENSEFILE"] = dialog.FileName; 
      Log(session, "setting property LICENSEFILE={0}", dialog.FileName); 
     } 
    } 
相關問題