C# '在与指定绑定约束匹配的类型上调用构造函数抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15306233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:23:23  来源:igfitidea点击:

'The invocation of the constructor on type that matches the specified binding constraints threw an exception

c#xamlexception-handling

提问by Cistoran

So I'm trying to build a program that accesses Etsy's API and so far all I am trying to do is make a call to it using OAuth and it throws this exception.

因此,我正在尝试构建一个访问 Etsy 的 API 的程序,到目前为止,我所做的只是使用 OAuth 调用它并抛出此异常。

'The invocation of the constructor on type 'testEtsy.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.

'在匹配指定绑定约束的类型'testEtsy.MainWindow'上调用构造函数抛出异常。行号“3”和行位置“9”。

Here is my XAML

这是我的 XAML

<Window x:Class="testEtsy.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid></Grid>

And here is my code in the MainWindow.cs

这是我在 MainWindow.cs 中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace testEtsy
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }
        string[] orderNumbers = System.IO.File.ReadAllLines(@"F:\ordernumbers.txt");

        public static void getOAuthKey()
        {

            string ConsumerKey = "q6uqzk27z2yw4tl5s4qerdtp";
            string ConsumerSecret = "tkjz2mu4x1";
            OAuth.Manager m = new OAuth.Manager();
            m["consumer_key"] = ConsumerKey;
            m["consumer_secret"] = ConsumerSecret;
            OAuth.OAuthResponse requestToken =
                m.AcquireRequestToken("https://openapi.etsy.com/v2/oauth/request_token?scope=transactions_r", "POST");
        }

    }
}

Any help would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by JaredPar

The most likely of the exception is the following field initializer

最有可能的异常是以下字段初始值设定项

string[] orderNumbers = System.IO.File.ReadAllLines(@"F:\ordernumbers.txt");

This code will run as a part of the MainWindowconstructor. If an exception occurs while reading the file this will propragate out of the constructor and cause initialization to fail. The most likely cause is that this file doesn't exist or is inaccessible

此代码将作为MainWindow构造函数的一部分运行。如果在读取文件时发生异常,这将传播出构造函数并导致初始化失败。最可能的原因是该文件不存在或无法访问

回答by Daniel Davis

The target of your project may not be the same as the target of a third party library. Just as a quick test, change your Platform target (under Project -> Properties -> Build) to x86. If that works, check that any external libs you have are 64 bit otherwise just build everything x86 instead of "Any CPU". For me the culprit was WebsocketSharp, for you looks like the OAuth library?

您项目的目标可能与第三方库的目标不同。作为快速测试,将您的平台目标(在项目 -> 属性 -> 构建下)更改为 x86。如果可行,请检查您拥有的任何外部库是否为 64 位,否则只需构建所有 x86 而不是“任何 CPU”。对我来说,罪魁祸首是 WebsocketSharp,因为你看起来像 OAuth 库?