“XamlParseException未处理”

时间:2012-10-29 06:02:11

标签: c# .net wpf

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

  

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;
    }
}
}

URL.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(字符串表,字符串文件夹)。所以我只在mainwindow中收到错误。

现在该怎么办?

好的..当我抓住它给我的例外时

  

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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