Въпрос с WPF + XAML + четене от сериен порт
Здравейте
Трябва да направя едно проста програмка която чете данни от серийния порт и ги визуализира на прозорче. И понеже нямам почти никакъв опит с тези работи, явно ми липсват някакви основни знания и нещата не тръгват :). Нещата с байндването май не се получават. С четене на тутариали и гледане на код от интернет стигнах до тук:
това ми е класът за четене от порта:
[CODE]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace Inspector
{
    class MyCommPort : INotifyPropertyChanged
    {
        SerialPort serialPort = null;
        public string TestText = "";
        public MyCommPort()
        {
            serialPort = new SerialPort("COM7", 9600);
            serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            serialPort.Open();
        }
        ~MyCommPort()
        {
            serialPort.Close();
        }
        
        
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string testString = null;
            string indata = sp.ReadLine();
            TestText = indata;
           
        }
       
        
        // Extend the INotifyPropertyChanged interface
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
               
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
[/CODE]
това тук ми е XAML - а
[CODE]<Window x:Class="Inspector.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Inspector"
        mc:Ignorable="d"
        
        Title="MainWindow" Height="720" Width="1280">
    
    <Grid
            <Paragraph>
                   <TextBlock Name="TestText" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=TestText}"></TextBlock>
            </Paragraph>
    </Grid>
</Window>
[/CODE]
о това е code behind - а
[CODE]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.ComponentModel;
using System.IO.Ports;
namespace Inspector
{
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            TestText.DataContext = new MyCommPort();
            
        }
       
    }
}[/CODE]
Резултатът e празен екран и не мога да си обясня защо. На серийния ми порт е свързано едно Arduino което просто брои от 0 до int.MaxValue
Така го направих, но все още на прозореца нямам нищо. А това вярно ли е ?
<TextBlock Name="TestText" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=TestText}">
Вярно е. Пробвай да зададеш DataContext на Window:
this.DataContext = new MyCommPort();
Не се получава. Прочетох за някви неща с
Dispatcher.Invoke, сега разучавам как се използва тази работа