简体   繁体   English

.NET 4.5到.NET 4.0上的WPF应用程序

[英]Wpf application on .NET 4.5 to .NET 4.0

I'm in face on the following problem: An application developed on Microsoft Visual Studio 2013 in .NET 4.5, needs to work in Window XP Platforms. 我面对以下问题:在.NET 4.5中的Microsoft Visual Studio 2013上开发的应用程序需要在Window XP Platforms中工作。 I'm rebuild the software using .NET 4.0 and make some modifications to add compatibility, but when i click in a button the app crash and don't show a clear error message and the Trace resource don't log anything. 我正在使用.NET 4.0重建软件,并进行了一些修改以增加兼容性,但是当我单击按钮时,应用程序崩溃了,并且没有显示明确的错误消息,并且Trace资源没有记录任何内容。 On application start i had a little window that ask user to put your name, and this feature works fine. 在应用程序启动时,我有一个小窗口,要求用户输入您的姓名,并且此功能运行良好。 Anybody have any suggestion of what can i do ? 有人对我该怎么办有什么建议?

EDIT 1: 编辑1:

The follow code is the root of problems, this code was compiled using .NET 4.0: 以下代码是问题的根源,该代码是使用.NET 4.0编译的:

SerialManager.cs 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
    }
}

If i don't do any instance or reference to serial port the applications don't crashes. 如果我不执行任何实例或对串行端口的引用,则应用程序不会崩溃。 Exist a way to force this part of code compiled by .NET 3.5? 是否存在一种强制使用.NET 3.5编译的这部分代码的方法? Or exist another suggestion of solution for this problem? 还是存在解决此问题的另一种建议?

我在http://blogs.msdn.com/b/bclteam/p/asynctargetingpackkb.asp (第8期)中找到了解决方案。安装.NET 4.0更新不会再出现问题了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将.net 4.5应用程序降级到4.0 - downgrade .net 4.5 application to 4.0 将我的.net wpf应用程序与sqlite一起从.net 4.5迁移到4.0? SQLite参考没有得到解决 - Migrating my .net wpf application from .net 4.5 to 4.0 along with sqlite? Sqlite reference is not getting resolved 在安装了.NET 4.5的TFS上构建时,WPF .NET 4.0应用程序无法启动 - WPF .NET 4.0 application does not start when build on TFS with .NET 4.5 installed 如何在只有.NET Framework 4.0和4.5的系统上支持.NET Framework 3.0 WPF应用程序? - How to support .NET Framework 3.0 wpf application on the system that has only .NET Framework 4.0 & 4.5? EDMX .NET 4.5到4.0? - EDMX .NET 4.5 to 4.0? 如何使应用程序与.NET 4.0和.NET 4.5兼容 - How to make an application compatible with .NET 4.0 and .NET 4.5 如果将框架替换为.NET 4.5,对于使用.NET 4.0的应用程序是否安全? - Is it safe for an application using .NET 4.0 if the framework is replaced with .NET 4.5? .NET 4.5应用程序在.NET 4.0系统上的功能特定兼容性 - Feature-specific compatibility of a .NET 4.5 application on a system with .NET 4.0 c#在.net 4.0应用程序中使用.net 4.5编译的dll - c# use .net 4.5 compiled dll, in a .net 4.0 application 使用.NET 4.5构建的应用程序可以在.NET 4.0上运行吗? - Can application built with .NET 4.5 run on .NET 4.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM