【问题标题】:"XamlParseException was unhandled"“XamlParseException 未处理”
【发布时间】:2012-10-29 14:31:57
【问题描述】:

每次运行应用程序时都会出现此异常。我不知道它是什么

XamlParseException 未处理。

'调用与指定匹配的'chrm.MainWindow'类型的构造函数 绑定约束引发了异常。行号“3”和行位置“9”。

我的代码

GoogleChrome.cs

namespace chrm
{
class GoogleChrome
{
    public List<URL> URLs = new List<URL>();
    public IEnumerable<URL> GetHistory()
    {
        // Get Current Users App Data
        string documentsFolder = Environment.GetFolderPath
        (Environment.SpecialFolder.ApplicationData);
        string[] tempstr = documentsFolder.Split('\\');
        string tempstr1 = "";
        documentsFolder += "\\Google\\Chrome\\User Data\\Default";
        if (tempstr[tempstr.Length - 1] != "Local")
        {
            for (int i = 0; i < tempstr.Length - 1; i++)
            {
                tempstr1 += tempstr[i] + "\\";
            }
            documentsFolder = tempstr1 + "Local\\Google\\Chrome\\User Data\\Default";
        }


        // Check if directory exists
        if (Directory.Exists(documentsFolder))
        {
            return ExtractUserHistory(documentsFolder);

        }
        return null;
    }


    IEnumerable<URL> ExtractUserHistory(string folder)
    {
        // Get User history info
        DataTable historyDT = ExtractFromTable("urls", folder);

        // Get visit Time/Data info
        DataTable visitsDT = ExtractFromTable("visits",
        folder);

        // Loop each history entry
        foreach (DataRow row in historyDT.Rows)
        {

            // Obtain URL and Title strings
            string url = row["url"].ToString();
            string title = row["title"].ToString();

            // Create new Entry
            URL u = new URL(url.Replace('\'', ' '),
            title.Replace('\'', ' '),
            "Google Chrome");

            // Add entry to list
            URLs.Add(u);
        }
        // Clear URL History
        DeleteFromTable("urls", folder);
        DeleteFromTable("visits", folder);

        return URLs;
    }

    void DeleteFromTable(string table, string folder)
    {
        SQLiteConnection sql_con;
        SQLiteCommand sql_cmd;

        // FireFox database file
        string dbPath = folder + "\\History";

        // If file exists
        if (File.Exists(dbPath))
        {
            // Data connection
            sql_con = new SQLiteConnection("Data Source=" + dbPath +
            ";Version=3;New=False;Compress=True;");

            // Open the Conn
            sql_con.Open();

            // Delete Query
            string CommandText = "delete from " + table;

            // Create command
            sql_cmd = new SQLiteCommand(CommandText, sql_con);

            sql_cmd.ExecuteNonQuery();

            // Clean up
            sql_con.Close();
        }
    }

     DataTable ExtractFromTable(string table, string folder)
    {
        SQLiteConnection sql_con;
        SQLiteCommand sql_cmd;
        SQLiteDataAdapter DB;
        DataTable DT = new DataTable();

        // FireFox database file
        string dbPath = folder + "\\History";

        // If file exists
        if (File.Exists(dbPath))
        {
            // Data connection
            sql_con = new SQLiteConnection("Data Source=" + dbPath +
            ";Version=3;New=False;Compress=True;");

            // Open the Connection
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();

            // Select Query
            string CommandText = "select * from " + table;

            // Populate Data Table
            DB = new SQLiteDataAdapter(CommandText, sql_con);
            DB.Fill(DT);

            // Clean up
            sql_con.Close();
        }
        return DT;
    }
}
}

网址.cs

namespace chrm
{
class URL
{
    string url;
    string title;
    string browser;
    public URL(string url, string title, string browser)
    {
        this.url = url;
        this.title = title;
        this.browser = browser;
    }

    public string getData()
    {
        return browser + " - " + title + " - " + url;
    }
}
}

最后是 Mainwindow.xaml.cs

namespace chrm
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    GoogleChrome ch = new GoogleChrome();
    public MainWindow()
    {

        InitializeComponent();

        ch.GetHistory();
    }
 }
}

当我将调试放在 cs 文件中时.. 我看到它不在 DataTable ExtractFromTable(字符串表,字符串文件夹)中。所以我只在主窗口中得到错误。

现在该怎么办?

好的..当我捕捉到它给我的异常时

System.IO.FileLoadException:混合模式程序集是针对运行时版本“v2.0.50727”构建的,如果没有其他配置信息,则无法在 4.0 运行时中加载。\r\n at chrm.GoogleChrome.ExtractFromTable(String table , 字符串文件夹)\r\n 位于 D:\html5\chrm\chrm\GoogleChrome.cs: chrm.GoogleChrome.ExtractUserHistory(String 文件夹) 中:第 45 行\r\n 位于 D:\ 中的 chrm.GoogleChrome.GetHistory() html5\chrm\chrm\GoogleChrome.cs:line 35\r\n at chrm.MainWindow..ctor() in D:\html5\chrm\chrm\MainWindow.xaml.cs:line 33

是因为我使用的 dll 是 v2.0 .. 我的应用需要 4.0?

【问题讨论】:

  • 您的 MainWindow Xaml 似乎不正确。调试您的代码以查看代码流是否进入 ch.GetHistory()?如果确实是 Xaml 问题,那么您会在 InitializeComponent() 处得到异常。

标签: c# .net wpf


【解决方案1】:

根据您的IOException,我确实会说这是一个错误,因为您在尝试使用针对 .NET 2.0 构建的 DLL 时在 .NET 4.0 中进行编译

尝试在 app.config 文件的配置部分添加它。如果需要,您可以在您的主应用程序或使用上述代码的 DLL 中尝试:

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
</startup>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多