编组c#从DLL到WPF的形式

本文关键字:WPF DLL 编组 | 更新日期: 2023-09-27 17:54:10

是否可以从DLL"编组"到WPF表单?例如,我喜欢更新Listbox。哪一种方法更好?

以前(当我在一个应用程序中拥有所有东西时),我定义了:

MyMarshalDataToForm(FormActions.AddItemToListBox, "Handle obtained to my device:");

,

private enum FormActions
{
    AddItemToListBox,
    DisableInputReportBufferSize,
    EnableGetInputReportInterruptTransfer,
    EnableInputReportBufferSize,
    EnableSendOutputReportInterrupt,
    ScrollToBottomOfListBox,
    SetInputReportBufferSize
}

,

private void MyMarshalDataToForm(FormActions action, String textToDisplay)
{
    try
    {
        object[] args = { action, textToDisplay };
        //  The AccessForm routine contains the code that accesses the form.
        MarshalDataToForm marshalDataToFormDelegate = AccessForm;
        //  Execute AccessForm, passing the parameters in args.
        Dispatcher.Invoke(marshalDataToFormDelegate, args);
    }
    catch (Exception ex)
    {
        DisplayException(Name, ex);
        throw;
    }
}

很抱歉在我的第一篇文章中有这么少的信息。现在我想在。dll中留下Mashal数据,并在主表单中更新(例如)ListBox。怎么做呢?

编组c#从DLL到WPF的形式

如果没有一个好的,最小的完整的代码示例来清楚地说明您的场景,就不可能知道最好的方法是什么。

然而,像这样分离主程序和DLL之间的关注点的一种常见方法是,DLL声明一个主程序可以订阅的event

例如在DLL中:

public delegate void FormActionHandler(FormActions action, string textToDisplay);
public event FormActionHandler FormAction;
private void MyMarshalDataToForm(FormActions action, String textToDisplay)
{
    try
    {
        object[] args = { action, textToDisplay };
        //  The AccessForm routine contains the code that accesses the form.
        MarshalDataToForm marshalDataToFormDelegate = FormAction;
        //  Execute AccessForm, passing the parameters in args.
        if (marshalDataToFormDelegate != null)
        {
            Dispatcher.Invoke(marshalDataToFormDelegate, args);
        }
    }
    catch (Exception ex)
    {
        DisplayException(Name, ex);
        throw;
    }
}

然后在主程序中,假定声明了AccessForm()方法,您将拥有如下代码:

myDllObject.FormAction += AccessForm;

这样,当DLL需要调用AccessForm()方法时,它实际上不需要知道该方法本身。相反,它使用主程序已经创建的委托来调用该方法,该方法已订阅到FormAction事件。


注意,上面使用了一个非标准的事件签名。这是完全合法的,但是如果你想遵循。net标准的基于EventHandler<T>的约定,你也可以这样做。它涉及更多的代码,但具有更易于识别的事件模式的优点。

例如:

class FormActionEventArgs : EventArgs
{
    public FormActions Action { get; private set; }
    public string TextToDisplay { get; private set; }
    public FormActionEventArgs(FormActions action, string textToDisplay)
    {
        Action = action;
        TextToDisplay = textToDisplay;
    }
}
public event EventHandler<FormActionEventArgs> FormAction;
private void MyMarshalDataToForm(FormActions action, String textToDisplay)
{
    try
    {
        FormActionEventArgs args = new FormActionEventArgs(action, textToDisplay);
        //  The AccessForm routine contains the code that accesses the form.
        EventHandler<FormActionEventArgs> marshalDataToFormDelegate = FormAction;
        //  Execute AccessForm, passing the parameters in args.
        if (marshalDataToFormDelegate != null)
        {
            Dispatcher.Invoke(marshalDataToFormDelegate, this, args);
        }
    }
    catch (Exception ex)
    {
        DisplayException(Name, ex);
        throw;
    }
}

那么你的主程序将像这样订阅:

myDllObject.FormAction += (sender, e) =>
{
    AccessForm(e.Action, e.TextToDisplay);
};