【问题标题】:System.Windows.Markup.XamlParseException when calling a function with variable parameters调用带有可变参数的函数时出现 System.Windows.Markup.XamlParseException
【发布时间】:2019-12-12 06:23:38
【问题描述】:

我的 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 编译没有错误。

谢谢!

【问题讨论】:

标签: c# .net wpf


【解决方案1】:

我会尝试在构造函数之外调用这个函数。在引导程序中在 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 将加载,然后您可以单独处理错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 2021-11-30
    • 2023-03-03
    • 2021-08-31
    • 2013-11-10
    • 2020-06-05
    相关资源
    最近更新 更多