.NET MAUI TCP listener; Scrollview not updating
Im new in MAUI and i have problem with tcp listener. The listener is working properly but not visualisation signals in UI at (CollectionView).The (CollectionView) in UI stay freeze. When user clicl at CollectionView show buffered signals and stop again to next change on selected row.I try async Task and Thread without succsess. Where im wrong ? Please HELP !
C# code
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Xml;
using Microsoft.Maui.Controls;
using Npgsql;
namespace Black_Program;
public partial class SignalsPage : ContentPage
{
public static bool searchRedy = true;
public static int daysNotPayedAlarm = 60;
System.Threading.Thread ReadTerminalSignals;
public SignalsPage()
{
InitializeComponent();
Task.Run(async () => await ReadFromIp());
}
async Task ReadFromIp()
{
var terminalSignalsList = new List<string>();
int terminalSignalsCount = 0;
TcpClient tcpclnt = new TcpClient();
try
{
while (true)
{
try
{
tcpclnt.Connect(MainPage.sqlServerIP, MainPage.sqlServerTerminalPort);
}
catch (Exception)
{
}
while (tcpclnt.Connected)
{
Thread.Sleep(10);
string messege = string.Empty;
Stream stream = tcpclnt.GetStream();
byte[] bytes = new byte[1024];
int length = stream.Read(bytes, 0, 1024);
/* Convert from ascii to stream */
for (int i = 0; i < length; i++)
messege += (Convert.ToChar(bytes[i]));
//byte[] utfBytes = Encoding.Default.GetBytes(messege);
messege = Encoding.UTF8.GetString(bytes);
try
{
////Update new signal if object selected
string[] newMessages = messege
.Split(new char[] { '-' }
, StringSplitOptions.RemoveEmptyEntries)
.ToArray();
if (signals_number_search.Text == newMessages[0].TrimEnd())
{
string numberData = signals_number_search.Text.Replace(" ", string.Empty); ;// Read the input from the user.
DateTime time = DateTime.Now;
string dateToday = time.AddDays(1).ToString("yyyy'-'MM'-'dd"); // 2 days log
string dateBefore = time.AddDays(-5).ToString("yyyy'-'MM'-'dd");
ShowSignalsFromLogDB(numberData, dateToday, dateBefore);
}
}
catch (Exception)
{
}
//Application.Current.MainPage.Dispatcher.Dispatch(() => terminalSignalsList.Add(messege));
terminalSignalsList.Add(messege);
signals_terminal.ItemsSource = terminalSignalsList;
terminalSignalsCount++;
Application.Current.MainPage.Dispatcher.Dispatch(() => signals_terminal.ScrollTo(terminalSignalsCount));
}
}
}
catch (Exception)
{
}
}
UI Code
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Black_Program.SignalsPage"
Title="Сигнали">
<ScrollView
BackgroundColor="Black">
<!-- Navigation Menu-->
<!-- Test design -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="AUTO"/><!--0-->
<RowDefinition Height="AUTO"/><!--1-->
<RowDefinition Height="AUTO"/><!--2-->
<RowDefinition Height="AUTO"/><!--3-->
<RowDefinition Height="AUTO"/><!--4-->
<RowDefinition Height="AUTO"/><!--5-->
<RowDefinition Height="AUTO"/><!--6-->
<RowDefinition Height="AUTO"/><!--7-->
</Grid.RowDefinitions>
<!-- Cool Adres and Users -->
<HorizontalStackLayout
HeightRequest="{OnPlatform Default='40',iOS='40',Android='35'}"
Margin="{OnPlatform Default='5,0,10,0',iOS='5,0,5,0',Android='5,0,5,0'}"
Grid.Row="1"
HorizontalOptions="CenterAndExpand">
<ScrollView>
<CollectionView
MaximumHeightRequest="300"
x:Name="signals_terminal"
SelectionMode="Single">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Няма данни</x:String>
</x:Array>
</CollectionView.ItemsSource>
</CollectionView>
</ScrollView>
</Frame>
</Grid>
</ScrollView>
</ContentPage>
Comments
Post a Comment