diff --git a/Class/TcpServer.cs b/Class/TcpServer.cs index a9e601f..a8e7d96 100644 --- a/Class/TcpServer.cs +++ b/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>>(val.ToString()!)!; - - InfoPage.OnInfoPage(); - } + //发送消息 + WeakReferenceMessenger.Default.Send(new object()); + } if (data.TryGetValue("Query", out val) && val != null)//查询接受 { QueryPage.Query_data(val.ToString()!); diff --git a/InfoPage.xaml b/InfoPage.xaml index a08613e..6f42dab 100644 --- a/InfoPage.xaml +++ b/InfoPage.xaml @@ -2,7 +2,7 @@ @@ -12,8 +12,7 @@ - diff --git a/InfoPage.xaml.cs b/InfoPage.xaml.cs index 09f0fa9..fe98a93 100644 --- a/InfoPage.xaml.cs +++ b/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 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(this, (r, m) => { - InfoItems.Clear(); + // 收到信号,直接触发函数 + HandleUITrigger(); + }); + } + private void HandleUITrigger() + { + InfoItems.Clear(); foreach (Dictionary 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 diff --git a/Models/DataModel.cs b/Models/DataModel.cs index b3cff0a..d1554b2 100644 --- a/Models/DataModel.cs +++ b/Models/DataModel.cs @@ -36,7 +36,7 @@ namespace SunlightAggregationTerminal.Models [ObservableProperty] public partial bool MessageNotificationMode { get; set; } - + public class RefreshUISignal { } public static List> Info_data = new List>(); } } diff --git a/Models/DataSource.cs b/Models/DataSource.cs index 4224ae0..186c3ae 100644 --- a/Models/DataSource.cs +++ b/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; } diff --git a/View/LogPage.xaml.cs b/View/LogPage.xaml.cs index 12ebdda..efd1db6 100644 --- a/View/LogPage.xaml.cs +++ b/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;