5 changed files with 139 additions and 2 deletions
@ -0,0 +1,57 @@ |
|||
<?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="SunlightAggregationTerminal.InfoChemicalStatistics" |
|||
Title="耗用统计"> |
|||
<Grid> |
|||
<Grid> |
|||
<CollectionView x:Name="CardCollectionView"> |
|||
<!-- 保持滚动条 --> |
|||
<CollectionView.ItemTemplate> |
|||
<DataTemplate> |
|||
<!-- 这里定义单个卡片的 UI 结构 --> |
|||
<Border BackgroundColor="White" Stroke="#CCCCCC" |
|||
StrokeThickness="2" Margin="10,0,10,10" |
|||
StrokeShape="RoundRectangle 10"> |
|||
<VerticalStackLayout> |
|||
<!-- 设备信息 --> |
|||
<HorizontalStackLayout> |
|||
<Label Text="原料: " FontAttributes="Bold" |
|||
FontSize="22" TextColor="Black"/> |
|||
<Label Text="{Binding ProductName}" FontAttributes="Bold" |
|||
FontSize="22" TextColor="Black"/> |
|||
</HorizontalStackLayout> |
|||
<HorizontalStackLayout> |
|||
<Label Text="代码: " FontAttributes="Bold" |
|||
FontSize="22" TextColor="Black"/> |
|||
<Label Text="{Binding ProductCode}" FontAttributes="Bold" |
|||
FontSize="22" TextColor="Black"/> |
|||
</HorizontalStackLayout> |
|||
<HorizontalStackLayout> |
|||
<Label Text="用量(kg): " FontAttributes="Bold" |
|||
FontSize="22" TextColor="Black"/> |
|||
<Label Text="{Binding TotalDispenseGrams}" FontAttributes="Bold" |
|||
FontSize="22" TextColor="Black"/> |
|||
</HorizontalStackLayout> |
|||
</VerticalStackLayout> |
|||
</Border> |
|||
</DataTemplate> |
|||
</CollectionView.ItemTemplate> |
|||
</CollectionView> |
|||
</Grid> |
|||
<!-- 查询中动画页面 --> |
|||
<Grid x:Name="LoadingIndicator" BackgroundColor="Gray" |
|||
IsVisible="False" Opacity="0.9"> |
|||
<VerticalStackLayout Padding="30" Spacing="10" |
|||
VerticalOptions="Center" |
|||
HorizontalOptions="Center"> |
|||
<ActivityIndicator IsRunning="True" |
|||
Color="#512BD4" |
|||
HorizontalOptions="Center" |
|||
VerticalOptions="Center" /> |
|||
<Label Text="查询中..." FontAttributes="Bold" |
|||
FontSize="24" TextColor="Black"/> |
|||
</VerticalStackLayout> |
|||
</Grid> |
|||
</Grid> |
|||
</ContentPage> |
|||
@ -0,0 +1,73 @@ |
|||
using SunlightAggregationTerminal.Class; |
|||
using SunlightAggregationTerminal.Models; |
|||
using System.Collections.ObjectModel; |
|||
using System.Reflection; |
|||
using System.Text.Json; |
|||
using TouchSocket.Core; |
|||
|
|||
namespace SunlightAggregationTerminal; |
|||
|
|||
public partial class InfoChemicalStatistics : ContentPage |
|||
{ |
|||
public class ChemicalTotal |
|||
{ |
|||
public string? ProductName { get; set; } |
|||
public string? ProductCode { get; set; } |
|||
public float? TotalDispenseGrams { get; set; } |
|||
} |
|||
|
|||
public static ObservableCollection<ChemicalTotal> ChemicalTotals { get; set; } = new(); |
|||
public InfoChemicalStatistics() |
|||
{ |
|||
InitializeComponent(); |
|||
Dictionary<string, object> Dictionary = new Dictionary<string, object>(); |
|||
Dictionary.Add("Command", "ChemicalStatistics"); |
|||
Total_Command(Dictionary.ToJsonString()); |
|||
} |
|||
|
|||
public async void Total_Command(string dat) |
|||
{ |
|||
LoadingIndicator.IsVisible = true; |
|||
ChemicalTotals.Clear(); |
|||
|
|||
DataReceived.Transmit(dat); |
|||
|
|||
// 创建一个 15 秒的延时任务
|
|||
Task delayTask = Task.Delay(15000); |
|||
// 创建一个监测任务,不断检查变量
|
|||
Task monitorTask = Task.Run(async () => |
|||
{ |
|||
while (ChemicalTotals.Count == 0) |
|||
{ |
|||
// 每隔 100 毫秒检查一次,避免 CPU 占用过高
|
|||
await Task.Delay(100); |
|||
} |
|||
}); |
|||
// 使用 Task.WhenAny 等待任意一个任务完成
|
|||
await Task.WhenAny(delayTask, monitorTask); |
|||
CardCollectionView.ItemsSource = ChemicalTotals; |
|||
|
|||
LoadingIndicator.IsVisible = false; |
|||
} |
|||
public static void Total_data(string dat) |
|||
{ |
|||
|
|||
try |
|||
{ |
|||
var data = JsonSerializer.Deserialize<List<ChemicalTotal>>(dat); |
|||
|
|||
if (data != null) |
|||
{ |
|||
foreach (var item in data) |
|||
{ |
|||
item.TotalDispenseGrams = item.TotalDispenseGrams / 1000; |
|||
|
|||
ChemicalTotals.Add(item); |
|||
} |
|||
} |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue