2012-10-29 58 views
1

我每次運行應用程序時都會收到此異常。我知道它是什麼「XamlParseException未處理」

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(字符串表,字符串文件夾)\ r \ n位於D:\ html5 \ chrm \ chrm \ GoogleChrome.cs中chrm.GoogleChrome.ExtractUserHistory(字符串文件夾)第45行\ r \ n位於D:\ html5 \ chrm \ chrm \ GoogleChrome.cs中的chrm.GoogleChrome.GetHistory()中:第35行\ r \ n位於chrm.MainWindow..ctor()的D:\ html5 \ chrm \ chrm \ MainWindow.xaml.cs:line 33

是因爲我使用的是v2.0的dll ..我的應用程序需要4.0嗎?

+1

似乎您的MainWindow Xaml不正確。調試你的代碼,看看代碼流是否進入ch.GetHistory()?如果它真的是Xaml問題,那麼你在InitializeComponent()處得到異常。 –

回答

0

根據你IOException,我的確會說這是一個錯誤,由於正在編譯在.NET 4.0中試圖使用內置針對.NET 2.0中的DLL事實

嘗試的配置部分添加此你的app.config文件。您可以嘗試在您的主應用程序或DLL中使用上面的代碼片段,如果需要的話:

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