簡體   English   中英

System.Windows.Markup.XamlParseException 調用具有可變參數的函數時

[英]System.Windows.Markup.XamlParseException when calling a function with variable parameters

我的 WPF 應用程序有問題。 運行它時,它會以 System.Windows.Markup.XamlParseException 中斷,給出:

Additional information: 'The invocation of the constructor on type 'myproject_csharp.MainWindow'
that matches the specified binding constraints threw an exception.' Line number '7' and line
position '9'.

在這個位置,我從一個需要變量參數列表的外部 DLL 中調用一個函數:

int pos = 0;
res = myfunc_init(ref pos,__arglist(several parameters here));

這個函數本身被定義為

[DllImportAttribute("my.dll", EntryPoint = "myfunc_init_api", CharSet = CharSet.Ansi, CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern uint myfunc_init(ref int var, __arglist);

據我所知,一切都是正確的。 那么這個異常是從哪里來的呢? 變量參數列表有什么問題嗎?

需要注意的一件事:在 IDE 中,該函數被標記為錯誤,並帶有“方法沒有重載需要 2 個參數”,但隨后 int 編譯沒有錯誤。

謝謝!

我會嘗試在構造函數之外調用這個函數。 在引導程序中在 MainWindow 外部調用它或在構造函數之后觸發它或在后台調用它,如:

在構造函數調用中:

Task.Run(() => Init());

進而

private static void Init() {
    try {
        int pos = 0;
        res = myfunc_init(ref pos,__arglist(several parameters here));

        //Call OnPropertyChanged() for relevant UI Elements

    } catch(Exception ex) {
        //Do error handling outside the constructor...
    }
}

如果您這樣做,XAML 將加載並且您可以單獨處理錯誤。

暫無
暫無

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

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