'在与指定绑定约束匹配的类型上调用构造函数会引发异常

Cis*_*ran 4 c# xaml exception-handling

所以我正在尝试构建一个访问Etsy API的程序,到目前为止,我要做的就是使用OAuth调用它,它会抛出此异常.

'对匹配指定绑定约束的类型'testEtsy.MainWindow'的构造函数的调用引发了异常.行号"3"和行位置"9".

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我在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");
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

Jar*_*Par 11

最可能的例外是以下字段初始值设定项

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

此代码将作为MainWindow构造函数的一部分运行.如果在读取文件时发生异常,则会从构造函数中进行预处理并导致初始化失败.最可能的原因是此文件不存在或无法访问

  • 您应该在“Window_Loaded”等事件中移动此类代码。它仍然会失败,但你会看到真正的异常。 (2认同)