Browse Source

InfoPage消息接收显示

master
Administrator 3 months ago
parent
commit
b3b42d129f
  1. 9
      Class/TcpServer.cs
  2. 5
      InfoPage.xaml
  3. 42
      InfoPage.xaml.cs
  4. 2
      Models/DataModel.cs
  5. 6
      Models/DataSource.cs
  6. 4
      View/LogPage.xaml.cs

9
Class/TcpServer.cs

@ -1,4 +1,5 @@
using Microsoft.VisualBasic;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.VisualBasic;
using SunlightAggregationTerminal.Models;
using System;
using System.Collections.Generic;
@ -122,9 +123,9 @@ namespace SunlightAggregationTerminal.Class
if (data.TryGetValue("Info", out val) && val != null)//机台信息接受
{
DataModel.Info_data = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(val.ToString()!)!;
InfoPage.OnInfoPage();
}
//发送消息
WeakReferenceMessenger.Default.Send(new object());
}
if (data.TryGetValue("Query", out val) && val != null)//查询接受
{
QueryPage.Query_data(val.ToString()!);

5
InfoPage.xaml

@ -2,7 +2,7 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SunlightAggregationTerminal.InfoPage"
xmlns:converters="clr-namespace:SunlightAggregationTerminal.Converters"
xmlns:converters="clr-namespace:SunlightAggregationTerminal.Converters"
Title="设备信息">
<ContentPage.Resources>
<ResourceDictionary>
@ -12,8 +12,7 @@
</ContentPage.Resources>
<ScrollView>
<VerticalStackLayout Padding="20" Spacing="20">
<CollectionView x:Name="CardCollectionView"
ItemsSource="{Binding InfoItems}"
<CollectionView x:Name="CardCollectionView"
RemainingItemsThreshold="5">
<!-- 保持滚动条 -->
<CollectionView.ItemTemplate>

42
InfoPage.xaml.cs

@ -1,3 +1,4 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Maui.Controls;
using SunlightAggregationTerminal.Class;
using SunlightAggregationTerminal.Models;
@ -6,21 +7,26 @@ using System.Collections.ObjectModel;
namespace SunlightAggregationTerminal;
public partial class InfoPage : ContentPage
{
{
public static ObservableCollection<InfoItem> InfoItems { get; set; } = new();
public static string? _Name, _State, _MachineName, _Machine, _Information, _Model,
_Type, _ModelA, _ModelB, _ModelC, _ModelD, _ModelE, _ModelF;
public InfoPage()
{
InitializeComponent();
}
public static void OnInfoPage()//打开时加载信息
{
MainThread.BeginInvokeOnMainThread(() =>
InitializeComponent();
CardCollectionView.ItemsSource = InfoItems;
// 注册订阅一个不携带数据的消息(使用 object 作为泛型)
WeakReferenceMessenger.Default.Register<object>(this, (r, m) =>
{
InfoItems.Clear();
// 收到信号,直接触发函数
HandleUITrigger();
});
}
private void HandleUITrigger()
{
InfoItems.Clear();
foreach (Dictionary<string, object> List in DataModel.Info_data)
{
if (List.TryGetValue("Name", out var val) && val != null) _Name = val.ToString()!;
@ -56,10 +62,30 @@ public partial class InfoPage : ContentPage
}
InfoItems.Add(new InfoItem
{
Name = _Name,
State = _State,
MachineName = _MachineName,
Machine = _Machine,
Information = _Information,
Type = _Type,
Model = _Model,
ModelA = _ModelA,
ModelB = _ModelB,
ModelC = _ModelC,
ModelD = _ModelD,
ModelE = _ModelE,
ModelF = _ModelF
});
}
~InfoPage()//关闭时注销消息
{
WeakReferenceMessenger.Default.UnregisterAll(this);
}
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
// 将 sender 强制转换为 BindingContext 定义在 BindableObject

2
Models/DataModel.cs

@ -36,7 +36,7 @@ namespace SunlightAggregationTerminal.Models
[ObservableProperty]
public partial bool MessageNotificationMode { get; set; }
public class RefreshUISignal { }
public static List<Dictionary<string, object>> Info_data = new List<Dictionary<string, object>>();
}
}

6
Models/DataSource.cs

@ -25,11 +25,9 @@ namespace SunlightAggregationTerminal.Models
public DateTime Time { get; set; }
public MsgType Type { get; set; }
}
public partial class InfoItem : ObservableObject
public partial class InfoItem
{
[ObservableProperty]
public string _name;
// public string? Name { get; set; }
public string? Name { get; set; }
public string? State { get; set; }
public string? MachineName { get; set; }
public string? Machine { get; set; }

4
View/LogPage.xaml.cs

@ -76,6 +76,10 @@ public partial class LogPage : ContentPage
{
LoadingIndicator.IsVisible = true;
App.GlobalData.ServerID = SERVERIP.Text;
App.GlobalData.User = UsernameEntry.Text;
App.GlobalData.UserPassword = PasswordEntry.Text;
if (await AppModels.LogIn())
{
App.GlobalData.ServerID = SERVERIP.Text;

Loading…
Cancel
Save