簡體   English   中英

當我使用區分大小寫的語言時,如何解決COM中不區分大小寫的沖突名稱

[英]How to solve case insensitive conflict name in COM while I using a case sensitive language

這個問題是本文的擴展。

在同一情況下,我通過其ProgID創建了WMP ActiveX實例。

protected const string WMP_PROG_ID = "WMPlayer.OCX.7";

private dynamic _wmp;

protected virtual bool init(){
    try{
        _wmp = Activator.CreateInstance(Type.GetTypeFromProgID(WMP_PROG_ID));
    }
    catch{ return false; }
    return connectEvent();
}

根據MSDN文檔,WMPlayer對象中有一個Error事件和一個error屬性。 因此,我嘗試以這種方式附加事件。

protected bool connectEvent(){
    try{
        _wmp.PlayStateChange += new StateHandler(_wmp_PlayStateChange);
        //_wmp.Error += new Action(_wmp_ErrorEvent);
    }
    catch { return false; }
    return true;
}

protected void _wmp_PlayStateChange(WMPlayerState state){
    //do something I like
}

protected void _wmp_ErrorEvent(){
    //do some error handling
}

如果我保持//_wmp.Error += new Action(_wmp_ErrorEvent)注釋,則不會發生編譯錯誤,並且PlayStateChange效果很好。

但是,如果刪除注釋標記,則會出現運行時異常。 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: can not apply operator "+=" between 'System.__ComObject' and 'System.Action'

似乎兩個“錯誤”是沖突的,因為COM不區分大小寫。 我該如何解決? 我的目標是不使用AxWindowsMediaPlayer附加到“錯誤”事件。

我遇到了一個與您非常相似的問題,但是我遇到的是Size 強類型的ActiveX控件重新定義了Size因此當表單設計人員想要在表單上調整Control Size ,我需要將其強制轉換回Control

((Control)this.axLEAD1).Size = new System.Drawing.Size(298, 240);

如果可以獲得com對象的強類型類(通過添加引用或使用tlbimp.exe ),則可以將其轉換為強類型com對象而不是__ComObject ,並使其使用正確的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM