Ver Mensaje Individual
  #3 (permalink)  
Antiguo 27/02/2015, 21:23
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 17 años
Puntos: 8
Respuesta: Usando richTextBox del WPF

Buen argumento campeón.

He intentado usar:
Código:
RichTextBox_Mensajes.DataContext = "Hola";
Simplemente no funciona. Pruebo con el tuyo.
Código:
            var doc = new System.Windows.Documents.FlowDocument();
            doc.Blocks.Add(new Paragraph(new Run("Hola")));
            RichTextBox_Mensajes.Document = doc;
Y funciona de maravilla y a la primera.

Vaya con el cambio de tecnología se pega Microsoft y a veces se complica la vida. Antes se hacía con una sola línea y ahora se hace con tres. Si es así todo el rato, me incomoda bastante, se hace un programa enorme, quiezás me equivoque y solo en algunos casos y cosas.

Cada enlace que me diste esta en español-
https://msdn.microsoft.com/es-es/lib...or=-2147217396

Por supuesto, me meteré con una buena leída ya que esto es complejo más de lo que creía. Adaptación a los nuevos tiempos. Windows Forms parece que no se quiere ir, al menos a corto plazo para formar el WPF.

Por eso y poco más, no me funciona este código al recibir datos y meterlo en el RichTextBox.
Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 System.IO.Ports; // No olvidar.
using System.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Utilizaremos un string como buffer de recepción.
        string Recibidos;

        SerialPort serialPort1 = new SerialPort();

        public MainWindow()
        {
            InitializeComponent();

            serialPort1.BaudRate = 115200;
            serialPort1.PortName = "COM4";
            serialPort1.Parity = Parity.None;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = StopBits.Two;

            // Abrir puerto mientras se ejecute la aplicación.
            if (!serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.Open();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }

            // Ejecutar la función REcepción por disparo del Evento ¡DataReived'.
            // serialPort1.DataReceived += new SerialDataReceivedEventHandler(Recepcion);
        }

        private void Recepcion(object sender, SerialDataReceivedEventHandler e)
        {
            // Acumular los caracteres recibidos a nuestro 'buffer' (string).
            Recibidos += serialPort1.ReadExisting();

            // Invocar o llamar al proceso de tramas.
            // this.Invoke(new EventHandler(Actualizar));
             
        }

        // Procesar los datos recibidos en el buffer y estraer tramas completas.
        private void Actualizar(object s, EventArgs e)
        {
            // Asignar el valor de la trama al RichTextBox.
            //RichTextBox_Mensajes.DataContext = Recibidos;

            var doc = new System.Windows.Documents.FlowDocument();
            doc.Blocks.Add(new Paragraph(new Run(Recibidos)));
            RichTextBox_Mensajes.Document = doc;
        }

        private void Button_Led_8_ON_Click(object sender, RoutedEventArgs e)
        {
            byte[] mBuffer = Encoding.ASCII.GetBytes("Led_8_ON");
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }

        private void Button_Led_8_OFF_Click(object sender, RoutedEventArgs e)
        {
            byte[] mBuffer = Encoding.ASCII.GetBytes("Led_8_OFF");
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }

    }
}
Gracias por las explicaciones.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar

Última edición por REHome; 27/02/2015 a las 21:29