2015-08-31 75 views
0

我面对以下问题:在.NET 4.5中的Microsoft Visual Studio 2013上开发的应用程序需要在Window XP平台中工作。我正在使用.NET 4.0重建软件并进行一些修改以添加兼容性,但是当我点击按钮时,应用程序崩溃并且不显示明确的错误消息,并且跟踪资源不记录任何内容。在应用程序启动时,我有一个要求用户输入姓名的小窗口,此功能正常工作。任何人有什么建议我可以做什么?从.NET 4.5到.NET 4.0的Wpf应用程序

EDIT 1:

后续代码是问题的根源,该代码是使用.NET 4.0编译:

SerialManager.cs

using System; 
using System.Windows; 
using TestSat; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.IO.Ports; 
using System.Linq; 
using System.Runtime.CompilerServices; 
using System.Text; 
using System.Threading.Tasks; 
using System.Threading; 
using System.Text.RegularExpressions; 
using System.Diagnostics; 

namespace TestSat.DataModel 
{ 
    /// <summary> 
    /// 
    /// </summary> 
    public class SerialManager : INotifyPropertyChanged 
    { 

     #region Events 

     public event PropertyChangedEventHandler PropertyChanged; 
     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="propertyName"></param> 
     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     /* [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] 
     public sealed class CallerMemberNameAttribute : Attribute 
     { 
     }*/ 

     /// <summary> 
     /// 
     /// </summary> 
     /// <typeparam name="T"></typeparam> 
     /// <param name="field"></param> 
     /// <param name="value"></param> 
     /// <param name="propertyName"></param> 
     /// <returns></returns> 
     protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null) 
     { 
      if (EqualityComparer<T>.Default.Equals(field, value)) return false; 
      field = value; 
      OnPropertyChanged(propertyName); 
      return true; 
     } 
     #endregion 

     #region Private Fields 

     private static SerialPort PortaSerial; 
     private ObservableCollection<String> mPorts; 
     private String mSelectedPort; 
     private int mBaudRate = 115200; 
     private int mDataBits = 8; 

     #endregion 

     #region Public Fields 
     public StringBuilder logText; 
     #endregion 

     #region Properties 

     /// <summary> 
     /// 
     /// </summary> 
     public ObservableCollection<String> COMPorts 
     { 
      get { return mPorts; } 
      set { SetField(ref mPorts, value); } 
     } 



     /// <summary> 
     /// 
     /// </summary> 
     public String TextoLog 
     { 
      get { return logText.ToString(); } 
      set 
      { 

       if (logText.Length >= logText.MaxCapacity) 
       { 
        logText.Clear();; 

        logText.Append(value); 
       } 
       else 
       { 
        logText.Append(value); 
        //MainWindow.last = value; 
       } 
       OnPropertyChanged("TextoLog"); 
      } 

     } 

     /// <summary> 
     /// 
     /// </summary> 
     public String SelectedPort 
     { 
      get { return mSelectedPort; } 
      set {SetField(ref mSelectedPort, value); } 
     } 

     #endregion 

     #region Construtors 

     /// <summary> 
     /// 
     /// </summary> 
     public SerialManager() 
     { 
      InitComponents(); 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     private void InitComponents() 
     { 
      RefreshPorts(); 

      /*Initialize the log variable*/ 
      logText = new StringBuilder(); 

      /* Update selected port */ 
      SelectedPort = COMPorts.Count > 0 ? COMPorts[0] : ""; 


     } 

     #endregion 

     #region Public Methods 

     /// <summary> 
     /// 
     /// </summary> 
     public void RefreshPorts() 
     { 
      // Update ports 
      string[] pPorts = SerialPort.GetPortNames(); 
      // Sort alphabetically 
      Array.Sort(pPorts); 
      // Sort by string length 
      Array.Sort(pPorts, (x, y) => x.Length.CompareTo(y.Length)); 

      // Create collection 
      COMPorts = new ObservableCollection<string>(pPorts); 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="mSelectedPort"></param> 
     public void ConnectSerial(String mSelectedPort) 
     { 
      PortaSerial = new SerialPort(); 
      PortaSerial.PortName = mSelectedPort; 
      PortaSerial.BaudRate = mBaudRate; 
      PortaSerial.Parity = Parity.None; 
      PortaSerial.DataBits = mDataBits; 
      PortaSerial.StopBits = StopBits.One; 
      PortaSerial.Handshake = Handshake.None; 
      PortaSerial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); 
      Trace.WriteLine("DataReceived definida"); 
      try 
      { 
       PortaSerial.Open(); 
      } 
      catch (SystemException) 
      { 
       MessageBox.Show("A porta serial esta sendo usada em outra aplicação.", "Erro", MessageBoxButton.OK); 
       throw new SystemException(); 
      } 

     } 

     /// <summary> 
     /// 
     /// </summary> 

     public void DesconnectSerial() 
     { 
      if (PortaSerial.IsOpen) 
      { 
       PortaSerial.Close(); 
      } 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     public void writeSerial(String text) 
     { 
      if (PortaSerial.IsOpen) 
      { 
       if (text.Length > 0) 
       { 
        /* char[] array = text.ToCharArray(0,text.Length); 
        foreach(char ch in array) 
        { 
         PortaSerial.Write(ch.ToString()); 
         Thread.Sleep(50); 

        }*/ 
        PortaSerial.WriteLine(text); 
       } 
       else 
       { 
        PortaSerial.WriteLine(""); 
       } 

      } 
      else 
      { 
       MessageBox.Show("Porta serial não esta aberta.", "Erro", MessageBoxButton.OK); 
       Console.WriteLine("Porta serial não esta aberta"); 
      } 


     } 

     /// <summary> 
     /// 
     /// </summary> 
     public bool IsOpen() 
     { 
      return PortaSerial.IsOpen; 
     } 


     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) 
     { 

      MainWindow.StartRawData = true; 
      SerialPort sp = (SerialPort)sender; 
      string indata = sp.ReadExisting(); 
      TextoLog = indata; 

      /*Omited code only logical operation*/ 

     } 

     #endregion 
    } 
} 

如果我不不做任何实例或引用串行端口的应用程序不崩溃。存在一种方法来强制这部分由.NET 3.5编译的代码?或者存在另一个解决这个问题的建议?

+0

如果您有源代码,则应该添加更多错误日志记录或附加远程调试器。根据你提供的信息,任何人都很难建议做什么。 .NET 4 WPF应用程序与Windows XP兼容,因此您正在做的事情在代码中不兼容(您没有提供)。 –

+1

通过运行eventvwr.exe查看Windows应用程序事件日志 – spender

+0

Windows XP不受支持,即使通过Microsoft **,您为什么关心该问题? –

回答

相关问题