如何在安装时在WiX中填充ComboBox?

Dan*_*Dan 5 combobox wix wix3

编辑:我已经更新了下面的代码,现在它可以运行了,感谢Rob的回答.

我发现了几个页面,显示如何做到这一点(http://www.cmcrossroads.com/content/view/13160/120/,http://www.mail-archive.com/wix-users@ lists.sourceforge.net/msg05103.html)并查看了WAI的源代码(http://wai.codeplex.com/),但无论我尝试什么,我似乎都无法在我的安装程序中使用它.如果有人能发现我做错了什么,我会非常感激.我对话的WiX片段如下所示:

<UI>
  <Dialog>

...snip...

    <Control Id="WebsiteName" Type="ComboBox" ComboList="yes" Sorted="yes" Property="IIS_WEBSITENAME" X="20" Y="73" Width="150" Height="17"/>

...snip...

    <!-- We want our custom action to fill in the WebsiteName ComboBox above
         however, if no ComboBox entries exist at compile time then the
         ComboBox table is not created in the MSI and we can't add to it in
         the custom action. So we have this hidden dummy list box to force
         the table to appear. -->
    <Control Id="DummyComboBox" Hidden="yes" Type="ComboBox" Sorted="yes" ComboList="yes" Property="DUMMYPROPERTY" X="65" Y="60" Width="150" Height="18">
      <ComboBox Property="DUMMYPROPERTY">
        <ListItem Text="Dummy" Value="Dummy"/>
      </ComboBox>
    </Control>
  </Dialog>
</UI>

<Property Id="DUMMYPROPERTY">Dummy</Property>
<Property Id="IIS_WEBSITENAME"/>
<CustomAction Id="FillWebsiteNameList" BinaryKey="WiXCustomAction.dll" DllEntry="FillWebsiteNameList" Execute="immediate" />
<InstallUISequence>
  <Custom Action="FillWebsiteNameList" After="CostFinalize"/>
</InstallUISequence>
Run Code Online (Sandbox Code Playgroud)

我的自定义操作代码是:

[CustomAction]
public static ActionResult FillWebsiteNameList(Session xiSession)
{
  xiSession.Log("Begin FillWebsiteNameList");

  xiSession.Log("Opening view");

  View lView = xiSession.Database.OpenView("SELECT * FROM ComboBox");
  lView.Execute();

  xiSession.Log("Creating directory entry");

  DirectoryEntry lIis = new DirectoryEntry("IIS://localhost/w3svc");

  xiSession.Log("Checking each child entry");

  int lIndex = 1;
  foreach (DirectoryEntry lEntry in lIis.Children)
  {
    if (lEntry.SchemaClassName == "IIsWebServer")
    {
      xiSession.Log("Found web server entry: " + lEntry.Name);

      string lWebsiteName = (string)lEntry.Properties["ServerComment"].Value;
      xiSession.Log("Website name: " + lWebsiteName);

      xiSession.Log("Creating record");
      Record lRecord = xiSession.Database.CreateRecord(4);

      xiSession.Log("Setting record details");
      lRecord.SetString(1, "IIS_WEBSITENAME");
      lRecord.SetInteger(2, lIndex);
      lRecord.SetString(3, lEntry.Name); // Use lWebsiteName only if you want to look up the site by name.
      lRecord.SetString(4, lWebsiteName);

      xiSession.Log("Adding record");
      lView.Modify(ViewModifyMode.InsertTemporary, lRecord);

      ++lIndex;
    }
  }

  xiSession.Log("Closing view");

  lView.Close();

  xiSession.Log("Return success");

  return ActionResult.Success;
}
Run Code Online (Sandbox Code Playgroud)

曾经有两个问题:

1)上述代码在运行自定义操作期间失败,"执行期间功能失败.数据库:表更新失败". - 这是因为索引问题导致代码尝试将字符串写入int列.

2)如果我换线

lRecord.SetString(2, lWebsiteName);
Run Code Online (Sandbox Code Playgroud)

lRecord.SetString(2, lEntry.Name);
Run Code Online (Sandbox Code Playgroud)

然后查看跟踪操作似乎成功,但是当安装程序运行时,组合框没有可供选择的条目.

如果我将组合框更改为具有硬编码值,一切正常,即使我硬编码相当于lWebsiteName.

Rob*_*ing 3

我不使用 DTF(对我​​来说都是自然的 C++ CustomActions),但 Record 是基于 1 的。您是否尝试过将所有 SetRecord() 调用移动一个索引?

另外,上面的 .wxs 代码似乎建议您使用“DUMMYPROPERTY”作为 ComboBox 的控件属性,而不是像 .cs 代码使用的“IIS_WEBSITENAME”。