From 981923c2a2cff6d61cd5a474d2ba398ba48f1fd5 Mon Sep 17 00:00:00 2001 From: chr <chr@chr.com> Date: 星期一, 02 九月 2024 17:40:17 +0800 Subject: [PATCH] 更新代码; --- PdmAlert/Util.cs | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 179 insertions(+), 0 deletions(-) diff --git a/PdmAlert/Util.cs b/PdmAlert/Util.cs index b5ed241..71f1c83 100644 --- a/PdmAlert/Util.cs +++ b/PdmAlert/Util.cs @@ -7,10 +7,158 @@ using System.Text; using System.Threading.Tasks; using System.Web; +using System.Windows.Controls; +using System.Windows.Documents; +using System.Windows.Media; +using System.Windows; using Newtonsoft.Json; namespace PdmAlert.Util { + public class MaskAdorner : Adorner + { + private UIElement child; + private TextBox textBox; + + public static readonly Dictionary<Visual, MaskAdorner> cache = new Dictionary<Visual, MaskAdorner>(); + + public MaskAdorner(UIElement adornedElement, string message = null) : base(adornedElement) + { + textBox = new TextBox + { + TextAlignment = TextAlignment.Center, + Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)), + Text = message, + BorderThickness = new Thickness(0), + BorderBrush = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)) + }; + StackPanel stackPanel = new StackPanel() + { + HorizontalAlignment = HorizontalAlignment.Center, + VerticalAlignment = VerticalAlignment.Center, + Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255)), + }; + stackPanel.Children.Add(new ProgressBar + { + Width = 100, + Height = 20, + IsIndeterminate = true + }); + stackPanel.Children.Add(textBox); + Child = new Border + { + Background = new SolidColorBrush(Color.FromArgb(100, 0, 0, 0)), + HorizontalAlignment = HorizontalAlignment.Stretch, + VerticalAlignment = VerticalAlignment.Stretch, + Child = stackPanel + }; + } + //Adorner 鐩存帴缁ф壙鑷� FrameworkElement锛� + //娌℃湁Content鍜孋hild灞炴�э紝 + //鑷繁娣诲姞涓�涓紝鏂逛究鍚戝叾涓坊鍔犳垜浠殑鎺т欢 + public UIElement Child + { + get => child; + set + { + if (value == null) + { + RemoveVisualChild(child); + } + else + { + AddVisualChild(value); + } + child = value; + } + } + //閲嶅啓VisualChildrenCount 琛ㄧず姝ゆ帶浠跺彧鏈変竴涓瓙鎺т欢 + protected override int VisualChildrenCount => 1; + //鎺т欢璁$畻澶у皬鐨勬椂鍊欙紝鎴戜滑鍦ㄨ楗板眰涓婃坊鍔犵殑鎺т欢涔熻绠椾竴涓嬪ぇ灏� + protected override Size ArrangeOverride(Size finalSize) + { + child?.Arrange(new Rect(finalSize)); + return finalSize; + } + //閲嶅啓GetVisualChild,杩斿洖鎴戜滑娣诲姞鐨勬帶浠� + protected override Visual GetVisualChild(int index) + { + if (index == 0 && child != null) return child; + return base.GetVisualChild(index); + } + + public void ShowMessage(string message) + { + this.textBox.Text = message; + } + + + public static void ShowMask(Visual visual, string message = null) + { + var sb = cache; + if (cache.ContainsKey(visual)) + { + return; + } + visual.Dispatcher.Invoke(() => + { + AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(visual); + if (adornerLayer == null) return; + Adorner[] ads = adornerLayer.GetAdorners(adornerLayer); + if (ads != null) + { + foreach (Adorner ad in ads) + { + if (ad is MaskAdorner adorner) + { + return; + } + } + } + //鍒涘缓鎴戜滑瀹氫箟鐨凙dorner + MaskAdorner _adorner = new MaskAdorner(adornerLayer, message); + //娣诲姞鍒癮dornerLayer瑁呴グ灞備笂 + adornerLayer.Add(_adorner); + cache.Add(visual, _adorner); + }); + } + + public static void ShowMessage(Visual visual, string message) + { + if (!cache.ContainsKey(visual)) + { + return; + } + visual.Dispatcher.Invoke(() => + { + AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(visual); + MaskAdorner mask = cache[visual]; + if (mask == null) + { + return; + } + mask.ShowMessage(message); + }); + } + + public static void HideMask(Visual visual) + { + if (!cache.ContainsKey(visual)) + { + return; + } + visual.Dispatcher.Invoke(() => + { + if (cache.ContainsKey(visual)) + { + AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(visual); + adornerLayer.Remove(cache[visual]); + cache.Remove(visual); + } + }); + } + } + [Serializable] public class Result<T> { @@ -37,6 +185,16 @@ { get; set; } + } + + public class Page<T> + { + public int pageNo { get; set; } + public int pageSize { get; set; } + public int total { get; set; } + public int current { get; set; } + public int size { get; set; } + public List<T> records { get; set; } } public static class ResultHandler @@ -236,6 +394,27 @@ return task2.Result; } + public static Result<T> PutSyncAction<T>(this HttpClient client, string url, object data = null) + { + StringContent content; + if (data != null) + { + string dataStr = JsonUtil.Serialize(data); + content = new StringContent(dataStr, Encoding.UTF8, "application/json"); + } + else + { + content = new StringContent(""); + } + Task<HttpResponseMessage> task = client.PutAsync(url, content); + task.Wait(); + HttpResponseMessage response = task.Result; + response.EnsureSuccessStatusCode(); + Task<string> task2 = response.Content.ReadAsStringAsync(); + task2.Wait(); + return JsonUtil.Deserialize<Result<T>>(task2.Result); + } + public static async Task<T> PostAsyncAction<T>(this HttpClient client, string url, string data) { StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); -- Gitblit v1.9.1