【问题标题】:XmlSerializer crashing only when "Start without debugging", but not "Step Into"XmlSerializer 仅在“启动而不调试”时崩溃,而不是“步入”时崩溃
【发布时间】:2011-08-03 03:18:35
【问题描述】:

正如标题所说,我遇到了 XmlSerializer 的问题。如果我运行“Step Into”,它运行良好,并按应有的方式保存。但是如果我“不调试就开始”它会一直运行到

XmlSerializer serial = new XmlSerializer(typeof(ObservableCollection<Bill>));

此时它崩溃了。我已经尝试预先放置一个 thread.sleep 以确保它很清楚,但没有帮助。我整天都在这里和谷歌搜索...请帮我找出问题???

这里是完整的代码:

using System;
using System.Collections.Generic;
using System.Linq;
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;
using Microsoft.Win32;
using System.IO;
using System.Data;
using System.Xml.Serialization;
using System.Collections.ObjectModel;


namespace BudgetHelper
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public Bills billsClass = new Bills();

    public MainWindow()
    {
        InitializeComponent();
        upcomingDueBills.ItemsSource = billsClass.PopulateUpcomingBills();
    }

    public void RefreshBills()
    {
        upcomingDueBills.Items.Refresh();
    }

    private void addBill_Click(object sender, RoutedEventArgs e)
    {
        AddAccount addAccountWindow = new AddAccount();
        addAccountWindow.ShowDialog();
        RefreshBills();
    }

    private void exitMenu_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        MessageBoxResult key = MessageBox.Show(
            "Are you sure you want to quit",
            "Confirm",
            MessageBoxButton.YesNo,
            MessageBoxImage.Question,
            MessageBoxResult.No);
        e.Cancel = (key == MessageBoxResult.No);
    }

    private void openMenu_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openDialog = new OpenFileDialog();
        openDialog.DefaultExt = ".txt";
        openDialog.FileName = "Text Documents (.txt)";

        Nullable<bool> result = openDialog.ShowDialog();

        if (result == true)
        {
            string filename = openDialog.FileName;
        }
    }

    private void updateBills_Click(object sender, RoutedEventArgs e)
    {
        upcomingDueBills.Items.Refresh();
    }



    private void saveMenu_Click(object sender, RoutedEventArgs e)
    {
        if (upcomingDueBills == null)
        {
            return;
        }
        else
        {
            billsClass.SaveBills();
        }
    }
}
}



using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using Microsoft.Win32;
using System.Threading;

namespace BudgetHelper
{
public class Bills : IEditableObject
{
    ObservableCollection<Bill> billCollection = new ObservableCollection<Bill>();

    public ObservableCollection<Bill> PopulateUpcomingBills()
    {


        using (StreamReader reader = new StreamReader(@"c:\users\Keven\Documents    
\Test.txt"))
        {
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                string[] fields = line.Split(new char[] { '|' });
                billCollection.Add(new Bill()
                {
                    accountName = fields[0],
                    projectedDueDate = DateTime.Parse(fields[1]),
                    projectedAmount = decimal.Parse(fields[2]),
                    totalDue = decimal.Parse(fields[3]),
                    category = fields[4],
                    notes = fields[5],
                    paidStatus = bool.Parse(fields[6])
                });


            }
        }

        return billCollection;
    }

    public void AddNewBill(string account, DateTime dueDate, decimal amount, decimal 
total, string category, string notes, bool isPaid)
    {
        billCollection.Add(new Bill()
        {
            accountName = account,
            projectedDueDate = dueDate,
            projectedAmount = amount,
            totalDue = total,
            category = category,
            notes = notes,
            paidStatus = isPaid
        });
    }

    public void SaveBills()
    {
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.AddExtension = true;
        saveDialog.DefaultExt = ".xml";
        saveDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
        saveDialog.InitialDirectory = @"C:\Users\Keven\Documents";
        saveDialog.OverwritePrompt = true;
        saveDialog.Title = "New Account Details";
        saveDialog.ValidateNames = true;
        if (saveDialog.ShowDialog().Value)
        {
            Thread.Sleep(2000);
            MessageBox.Show("2");
            XmlSerializer serial = new XmlSerializer(typeof
(ObservableCollection<Bill>));
            using (StreamWriter writer = new StreamWriter(saveDialog.FileName))
            {

                serial.Serialize(writer, billCollection);

            }
        }
    }

    public static MainWindow main;

    public object UpcomingDueBills
    {
        get;
        set;
    }

    public class Bill
    {
        public string accountName { get; set; }
        public DateTime projectedDueDate { get; set; }
        public decimal projectedAmount { get; set; }
        public decimal totalDue { get; set; }
        public string category { get; set; }
        public string notes { get; set; }
        public bool paidStatus { get; set; }
    }

    void IEditableObject.BeginEdit()
    {
        throw new NotImplementedException();
    }

    void IEditableObject.CancelEdit()
    {
        throw new NotImplementedException();
    }

    void IEditableObject.EndEdit()
    {
        throw new NotImplementedException();
    }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
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.Shapes;
using System.IO;
using Microsoft.Win32;
using System.Windows.Threading;
using System.Threading;
using System.Data;

namespace BudgetHelper
{
/// <summary>
/// Interaction logic for AddAccount.xaml
/// </summary>
public partial class AddAccount : Window
{
    public Bills bills = new Bills();

    public AddAccount()
    {
        InitializeComponent();
    }

    private void cancel_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void saveAccountInfo_Click(object sender, RoutedEventArgs e)
    {
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.AddExtension = true;
        saveDialog.DefaultExt = "txt";
        saveDialog.InitialDirectory = @"C:\Users\Keven\Documents";
        saveDialog.OverwritePrompt = true;
        saveDialog.Title = "New Account Details";
        saveDialog.ValidateNames = true;
        if (saveDialog.ShowDialog().Value)
        {

        }

        this.Close();
    }

    private void done_Click(object sender, RoutedEventArgs e)
    {
        bills.AddNewBill(newAccountName.Text, projectedDueDate.SelectedDate.Value, 
decimal.Parse(projectedAmount.Text.ToString()),
            decimal.Parse(totalDue.Text.ToString()), accountCategory.Text, 
accountNotes.Text, (bool)isPaid.IsChecked);
    }

}
}

【问题讨论】:

  • 它会因什么错误而崩溃?另外,如果您在运行时附加了调试器但不设置任何断点,它是否有效?
  • 发布异常的堆栈跟踪。

标签: c# datagrid io observablecollection xmlserializer


【解决方案1】:

找到了答案,它是 Comodo 自动对应用程序进行沙盒处理。如果其他人遇到此问题,只需转到 Comodo 中的 Defense+,打开“无法识别的文件”窗口,然后将该程序添加到受信任的文件列表中。

另一方面,有人知道是否有办法让 Comodo 信任我从事的所有 Visual Studio 项目?

【讨论】:

  • 请不要在回答中提问。答案帖子包含...答案:)
猜你喜欢
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 2021-09-17
相关资源
最近更新 更多