English 中文(简体)
推出由WIX安装器与WPF撰写的“习俗行动”:Fail on STAThread 问题
原标题:Launching a CustomAction UI written with WPF from a WIX installer: Fail on STAThread issue
  • 时间:2012-01-13 18:48:58
  •  标签:
  • wpf
  • wix

My WIX installer launches an immediate custom action. The custom action starts a WPF dialog prompting the user for a BD connection parameters (I basically wrote the new connection DB prompter dialog in WPF, to get me a connection string that the custom action can inject in the installed application s configuration file). The WIX code is fairly simple to figure out, and I know I execute the custom action just fine - I put there a MessageBox and a MmsiBreak on the call to my custom action method. I get there without a problem. When the custom action instantiates my WPF dialog window, I get an InvaliOperationException: "The calling thread must be STA, because many UI components require this".

The same code runs fine when I put it in a standard WPF application, because VisualStudio generates boiler plate code with Main() that has a STAThreadAttribute on it. I can t tack that attribute on the msiexec caller, and if I try to set the thread apartment state in my custom action, it fails:

Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

不应为过去2.0年的框架而努力。

我想要在这里做些什么? 我对一些要点表示赞赏。


EDIT

我甚至试图用自己眼光来管理方言,例如,这部法典就是这样:

// Static class members
static ManualResetEvent _done = new ManualResetEvent(false);
static ActionResult _caResult;
static Session _session;
static Thread _worker;

[CustomAction]
static public ActionResult PromptForDB(Session session)
{
    _session = session;
    _worker = new Thread(WorkerThread);
    _worker.Start();
    _done.WaitOne();
    return _caResult;
}

[STAThread]
static void WorkerThread()
{
    try
    {
        Prompter wnd = new Prompter();
        if (!(bool)wnd.ShowDialog())
        {
            _caResult = ActionResult.SkipRemainingActions;
        }
        else
        {
            // Harvest our properties (omitted from this post)
            _caResult = ActionResult.Success;
        }
        catch (Exception ex)
        {
            _caResult = ActionResult.Failure;
            _session.Log("Error: " + ex.Message);
        }
        finally
        {
            _done.Set();
        }
    }

这也不可行。

最佳回答

在开始你的新路面之前,建造了以下公寓:

_worker.SetApartmentState(ApartmentState.STA);

See this:
The calling thread must be STA, because many UI components require this in WPF

问题回答

暂无回答




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签