基於Win服務的標簽列印(模板套打)

来源:https://www.cnblogs.com/liuju150/archive/2019/12/09/Service_Print_Template_Solution.html
-Advertisement-
Play Games

最近做了幾個項目,都有在產品貼標的需求 基本就是有個證卡類印表機,然後把產品的信息列印在標簽上。 然後通過機器人把標簽貼到產品上面 標簽信息包括文本,二維碼,條形碼之類的,要根據對應的數據生成二維碼,條形碼。 列印標簽的需求接到手後,開始了我的填坑之旅。 列印3.0源代碼:https://githu ...


最近做了幾個項目,都有在產品貼標的需求

基本就是有個證卡類印表機,然後把產品的信息列印在標簽上。

然後通過機器人把標簽貼到產品上面

標簽信息包括文本,二維碼,條形碼之類的,要根據對應的數據生成二維碼,條形碼。

列印標簽的需求接到手後,開始了我的填坑之旅。

 

列印3.0源代碼:https://github.com/zeqp/ZEQP.Print

 

列印1.0

第一個項目開始,因為原來沒有研究過列印,所以在Bing上查了一下.Net印表機關的資料

發現基本上都是基於.net的
System.Drawing.Printing.PrintDocument
這個類來做自定義列印

大家都用這個做列印,我想按理也沒有問題。

所以開始了我的代碼。

PrintDocument去做列印,無非就是設置好印表機名稱,
DefaultPageSettings.PrinterSettings.PrinterName
列印份數
DefaultPageSettings.PrinterSettings.Copies
紙張方向
DefaultPageSettings.Landscape
然後列印的具體的信息就是事件PrintPage寫進去
然後調用
Graphics.DrawString,Graphics.DrawImage來寫入具體的文本與圖片
Graphics.Draw的時候要指定字體,顏色,位置等數據
我把這些做成配置數據。
然後1.0版本就成了。

 

 


下圖為位置的配置文件

 

 

  代碼一寫完,用VS調試的時候。跑得飛起。、

所有的字體,要列印數據的位置也通過配置文件可以動態的調整。感覺還算完美。
但是現實很骨感,馬上就拍拍打臉了


PrintDocument類只能以WinForm的方式運行,不能以服務的方式運行。
具體可以參考:https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.printing?redirectedfrom=MSDN&view=netframework-4.8

 

 

 幸好客戶方面沒有什麼要求,而且生產的時候會有一臺專門的上位機可以做這個事,所以做了一個無界面的WinForm。在電腦啟動的時候運行

從而解決了不能以服務的方式運行的問題。

 

列印2.0

做完列印1.0後,又接到了一個項目。又是有列印相關的功能,自然又分配到我這裡來了。

但是對於上一個版本的列印。不能做為服務運行,做為自己寫的一個程式,居然有這麼大的瑕疵。總感覺心裡不爽

想去解決這個問題,但是在Bing上找到.Net的所有列印都是這樣做的。也找不到什麼更好的方法。

只到問了很多相關的相關人士。最後給了我一個第三方的商業解決方案BarTender
相關參考:https://www.bartendersoftware.com/
這個有自己的模板編輯器,

 

 

 

有自己的SDK,有編輯器,功能也非學強大。不愧是商業列印解決方案。

根據他的SDK,同時安裝了相關程式,寫下幾句列印代碼。一個基於Win服務的列印出來了

 

 

 於是。列印2.0出來了。

 

列印3.0
但是對於一個基於第三方的商業列印方案,所有功能都是很強大。實現也簡單。

就是對於一般公司的小項目。掙的錢還不夠買這個商業套件的License

而且對於一個只會使用別人家的SDK的程式。不是一個有靈魂的程式。

因為你都不知道人家背後是怎麼實現的。原理是什麼都不知道。

對於我,雖然能把這個項目用BarTender完成。但是總是對這個列印方案不是很滿意。

因為我只在這個上面加了一層殼。不知道後面做了什麼。

所以我一直想自己開發一個可以基於Win服務運行的列印程式。最好也要有自己的模板編輯器。

只到有一天。無意找到一篇文章

https://docs.aspose.com/display/wordsnet/Print+a+Document

他這裡也解釋了有關基於服務的列印有關的問題不能解決。

並且他們已經找到了對應的解決方案。基於他的解決方案。寫了對應一個列印幫助類。

這個是基於Windows的XPS文檔API列印。

XPS是在Win 7後就是windows支持的列印文檔類型 類比PDF

基本 XpsPrint API   的相關說明

同時基本他的XPS列印幫助類。我做了測試。可以完美的在Windows服務裡面運行關列印。

  1 namespace ZEQP.Print.Framework
  2 {
  3     /// <summary>
  4     /// A utility class that converts a document to XPS using Aspose.Words and then sends to the XpsPrint API.
  5     /// </summary>
  6     public class XpsPrintHelper
  7     {
  8         /// <summary>
  9         /// No ctor.
 10         /// </summary>
 11         private XpsPrintHelper()
 12         {
 13         }
 14 
 15         // ExStart:XpsPrint_PrintDocument       
 16         // ExSummary:Convert an Aspose.Words document into an XPS stream and print.
 17         /// <summary>
 18         /// Sends an Aspose.Words document to a printer using the XpsPrint API.
 19         /// </summary>
 20         /// <param name="document"></param>
 21         /// <param name="printerName"></param>
 22         /// <param name="jobName">Job name. Can be null.</param>
 23         /// <param name="isWait">True to wait for the job to complete. False to return immediately after submitting the job.</param>
 24         /// <exception cref="Exception">Thrown if any error occurs.</exception>
 25         public static void Print(string xpsFile, string printerName, string jobName, bool isWait)
 26         {
 27             Console.WriteLine("Print");
 28             if (!File.Exists(xpsFile))
 29                 throw new ArgumentNullException("xpsFile");
 30             using (var stream = File.OpenRead(xpsFile))
 31             {
 32                 Print(stream, printerName, jobName, isWait);
 33             }
 34             //// Use Aspose.Words to convert the document to XPS and store in a memory stream.
 35             //File.OpenRead
 36             //MemoryStream stream = new MemoryStream();
 37 
 38             //stream.Position = 0;
 39             //Console.WriteLine("Saved as Xps");
 40             //Print(stream, printerName, jobName, isWait);
 41             Console.WriteLine("After Print");
 42         }
 43         // ExEnd:XpsPrint_PrintDocument
 44         // ExStart:XpsPrint_PrintStream        
 45         // ExSummary:Prints an XPS document using the XpsPrint API.
 46         /// <summary>
 47         /// Sends a stream that contains a document in the XPS format to a printer using the XpsPrint API.
 48         /// Has no dependency on Aspose.Words, can be used in any project.
 49         /// </summary>
 50         /// <param name="stream"></param>
 51         /// <param name="printerName"></param>
 52         /// <param name="jobName">Job name. Can be null.</param>
 53         /// <param name="isWait">True to wait for the job to complete. False to return immediately after submitting the job.</param>
 54         /// <exception cref="Exception">Thrown if any error occurs.</exception>
 55         public static void Print(Stream stream, string printerName, string jobName, bool isWait)
 56         {
 57             if (stream == null)
 58                 throw new ArgumentNullException("stream");
 59             if (printerName == null)
 60                 throw new ArgumentNullException("printerName");
 61 
 62             // Create an event that we will wait on until the job is complete.
 63             IntPtr completionEvent = CreateEvent(IntPtr.Zero, true, false, null);
 64             if (completionEvent == IntPtr.Zero)
 65                 throw new Win32Exception();
 66 
 67             //            try
 68             //            {
 69             IXpsPrintJob job;
 70             IXpsPrintJobStream jobStream;
 71             Console.WriteLine("StartJob");
 72             StartJob(printerName, jobName, completionEvent, out job, out jobStream);
 73             Console.WriteLine("Done StartJob");
 74             Console.WriteLine("Start CopyJob");
 75             CopyJob(stream, job, jobStream);
 76             Console.WriteLine("End CopyJob");
 77 
 78             Console.WriteLine("Start Wait");
 79             if (isWait)
 80             {
 81                 WaitForJob(completionEvent);
 82                 CheckJobStatus(job);
 83             }
 84             Console.WriteLine("End Wait");
 85             /*            }
 86                         finally
 87                         {
 88                             if (completionEvent != IntPtr.Zero)
 89                                 CloseHandle(completionEvent);
 90                         }
 91             */
 92             if (completionEvent != IntPtr.Zero)
 93                 CloseHandle(completionEvent);
 94             Console.WriteLine("Close Handle");
 95         }
 96         // ExEnd:XpsPrint_PrintStream
 97 
 98         private static void StartJob(string printerName, string jobName, IntPtr completionEvent, out IXpsPrintJob job, out IXpsPrintJobStream jobStream)
 99         {
100             int result = StartXpsPrintJob(printerName, jobName, null, IntPtr.Zero, completionEvent,
101                 null, 0, out job, out jobStream, IntPtr.Zero);
102             if (result != 0)
103                 throw new Win32Exception(result);
104         }
105 
106         private static void CopyJob(Stream stream, IXpsPrintJob job, IXpsPrintJobStream jobStream)
107         {
108 
109             //            try
110             //            {
111             byte[] buff = new byte[4096];
112             while (true)
113             {
114                 uint read = (uint)stream.Read(buff, 0, buff.Length);
115                 if (read == 0)
116                     break;
117 
118                 uint written;
119                 jobStream.Write(buff, read, out written);
120 
121                 if (read != written)
122                     throw new Exception("Failed to copy data to the print job stream.");
123             }
124 
125             // Indicate that the entire document has been copied.
126             jobStream.Close();
127             //            }
128             //            catch (Exception)
129             //            {
130             //                // Cancel the job if we had any trouble submitting it.
131             //                job.Cancel();
132             //                throw;
133             //            }
134         }
135 
136         private static void WaitForJob(IntPtr completionEvent)
137         {
138             const int INFINITE = -1;
139             switch (WaitForSingleObject(completionEvent, INFINITE))
140             {
141                 case WAIT_RESULT.WAIT_OBJECT_0:
142                     // Expected result, do nothing.
143                     break;
144                 case WAIT_RESULT.WAIT_FAILED:
145                     throw new Win32Exception();
146                 default:
147                     throw new Exception("Unexpected result when waiting for the print job.");
148             }
149         }
150 
151         private static void CheckJobStatus(IXpsPrintJob job)
152         {
153             XPS_JOB_STATUS jobStatus;
154             job.GetJobStatus(out jobStatus);
155             switch (jobStatus.completion)
156             {
157                 case XPS_JOB_COMPLETION.XPS_JOB_COMPLETED:
158                     // Expected result, do nothing.
159                     break;
160                 case XPS_JOB_COMPLETION.XPS_JOB_FAILED:
161                     throw new Win32Exception(jobStatus.jobStatus);
162                 default:
163                     throw new Exception("Unexpected print job status.");
164             }
165         }
166 
167         [DllImport("XpsPrint.dll", EntryPoint = "StartXpsPrintJob")]
168         private static extern int StartXpsPrintJob(
169             [MarshalAs(UnmanagedType.LPWStr)] String printerName,
170             [MarshalAs(UnmanagedType.LPWStr)] String jobName,
171             [MarshalAs(UnmanagedType.LPWStr)] String outputFileName,
172             IntPtr progressEvent,   // HANDLE
173             IntPtr completionEvent, // HANDLE
174             [MarshalAs(UnmanagedType.LPArray)] byte[] printablePagesOn,
175             UInt32 printablePagesOnCount,
176             out IXpsPrintJob xpsPrintJob,
177             out IXpsPrintJobStream documentStream,
178             IntPtr printTicketStream);  // This is actually "out IXpsPrintJobStream", but we don't use it and just want to pass null, hence IntPtr.
179 
180         [DllImport("Kernel32.dll", SetLastError = true)]
181         private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);
182 
183         [DllImport("Kernel32.dll", SetLastError = true, ExactSpelling = true)]
184         private static extern WAIT_RESULT WaitForSingleObject(IntPtr handle, Int32 milliseconds);
185 
186         [DllImport("Kernel32.dll", SetLastError = true)]
187         [return: MarshalAs(UnmanagedType.Bool)]
188         private static extern bool CloseHandle(IntPtr hObject);
189     }
190 
191     /// <summary>
192     /// This interface definition is HACKED.
193     /// 
194     /// It appears that the IID for IXpsPrintJobStream specified in XpsPrint.h as 
195     /// MIDL_INTERFACE("7a77dc5f-45d6-4dff-9307-d8cb846347ca") is not correct and the RCW cannot return it.
196     /// But the returned object returns the parent ISequentialStream inteface successfully.
197     /// 
198     /// So the hack is that we obtain the ISequentialStream interface but work with it as 
199     /// with the IXpsPrintJobStream interface. 
200     /// </summary>
201     [Guid("0C733A30-2A1C-11CE-ADE5-00AA0044773D")]  // This is IID of ISequenatialSteam.
202     [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
203     interface IXpsPrintJobStream
204     {
205         // ISequentualStream methods.
206         void Read([MarshalAs(UnmanagedType.LPArray)] byte[] pv, uint cb, out uint pcbRead);
207         void Write([MarshalAs(UnmanagedType.LPArray)] byte[] pv, uint cb, out uint pcbWritten);
208         // IXpsPrintJobStream methods.
209         void Close();
210     }
211 
212     [Guid("5ab89b06-8194-425f-ab3b-d7a96e350161")]
213     [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
214     interface IXpsPrintJob
215     {
216         void Cancel();
217         void GetJobStatus(out XPS_JOB_STATUS jobStatus);
218     }
219 
220     [StructLayout(LayoutKind.Sequential)]
221     struct XPS_JOB_STATUS
222     {
223         public UInt32 jobId;
224         public Int32 currentDocument;
225         public Int32 currentPage;
226         public Int32 currentPageTotal;
227         public XPS_JOB_COMPLETION completion;
228         public Int32 jobStatus; // UInt32
229     };
230 
231     enum XPS_JOB_COMPLETION
232     {
233         XPS_JOB_IN_PROGRESS = 0,
234         XPS_JOB_COMPLETED = 1,
235         XPS_JOB_CANCELLED = 2,
236         XPS_JOB_FAILED = 3
237     }
238 
239     enum WAIT_RESULT
240     {
241         WAIT_OBJECT_0 = 0,
242         WAIT_ABANDONED = 0x80,
243         WAIT_TIMEOUT = 0x102,
244         WAIT_FAILED = -1 // 0xFFFFFFFF
245     }
246 }
XpsPrintHelper

到此,基於windows服務的列印已經解決。

就只有模板編輯器的事情了。

對於原來做過基於Word的郵件合併域的經驗。自己開發一個編輯器來說工程量有點大

所以選擇了一個現有的,功能又強大的文檔編輯器。Word來做為我的標簽編輯器了。

Word可以完美的解決紙張,格式,位置等問題。只是在對應的地方用“文本域”來做占位符

然後用自定義的數據填充就可以了。

下圖為Word模板編輯

 

 編輯占位符(域)

 

 

這樣的話。一個模板就出來了

如果是圖片的話。就在功能變數名稱前加Image:

如果是表格的話。在表格的開始加上TableStart:表名
在表格的未尾加上TableEnd:表名

 

協議的話。走的是所有語言都支持的http,對於以後開發SDK也方便

對於上面的模板,只要發送這樣的請球POST

對於Get請求

 

然後列印出來的效果

到此,列印3.0已經完成。

關鍵代碼
根據請求數據生成列印實體

 1 private PrintModel GetPrintModel(HttpListenerRequest request)
 2         {
 3             var result = new PrintModel();
 4             result.PrintName = ConfigurationManager.AppSettings["PrintName"];
 5             result.Template = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", "Default.docx");
 6             result.Action = PrintActionType.Print;
 7 
 8             var query = request.Url.Query;
 9             var dicQuery = this.ToNameValueDictionary(query);
10             if (dicQuery.ContainsKey("PrintName")) result.PrintName = dicQuery["PrintName"];
11             if (dicQuery.ContainsKey("Copies")) result.Copies = int.Parse(dicQuery["Copies"]);
12             if (dicQuery.ContainsKey("Template"))
13             {
14                 var tempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", dicQuery["Template"]);
15                 if (File.Exists(tempPath))
16                     result.Template = tempPath;
17             }
18             if (dicQuery.ContainsKey("Action")) result.Action = (PrintActionType)Enum.Parse(typeof(PrintActionType), dicQuery["Action"]);
19 
20             foreach (var item in dicQuery)
21             {
22                 if (item.Key.StartsWith("Image:"))
23                 {
24                     var keyName = item.Key.Replace("Image:", "");
25                     if (result.ImageContent.ContainsKey(keyName)) continue;
26                     var imageModel = item.Value.ToObject<ImageContentModel>();
27                     result.ImageContent.Add(keyName, imageModel);
28                     continue;
29                 }
30                 if (item.Key.StartsWith("Table:"))
31                 {
32                     var keyName = item.Key.Replace("Table:", "");
33                     if (result.TableContent.ContainsKey(keyName)) continue;
34                     var table = item.Value.ToObject<DataTable>();
35                     table.TableName = keyName;
36                     result.TableContent.Add(keyName, table);
37                     continue;
38                 }
39                 if (result.FieldCotent.ContainsKey(item.Key)) continue;
40                 result.FieldCotent.Add(item.Key, item.Value);
41             }
42 
43             if (request.HttpMethod.Equals("POST", StringComparison.CurrentCultureIgnoreCase))
44             {
45                 var body = request.InputStream;
46                 var encoding = Encoding.UTF8;
47                 var reader = new StreamReader(body, encoding);
48                 var bodyContent = reader.ReadToEnd();
49                 var bodyModel = bodyContent.ToObject<Dictionary<string, object>>();
50                 foreach (var item in bodyModel)
51                 {
52                     if (item.Key.StartsWith("Image:"))
53                     {
54                         var imageModel = item.Value.ToJson().ToObject<ImageContentModel>();
55                         var keyName = item.Key.Replace("Image:", "");
56                         if (result.ImageContent.ContainsKey(keyName))
57                             result.ImageContent[keyName] = imageModel;
58                         else
59                             result.ImageContent.Add(keyName, imageModel);
60                         continue;
61                     }
62                     if (item.Key.StartsWith("Table:"))
63                     {
64                         var table = item.Value.ToJson().ToObject<DataTable>();
65                         var keyName = item.Key.Replace("Table:", "");
66                         table.TableName = keyName;
67                         if (result.TableContent.ContainsKey(keyName))
68                             result.TableContent[keyName] = table;
69                         else
70                             result.TableContent.Add(keyName, table);
71                         continue;
72                     }
73                     if (result.FieldCotent.ContainsKey(item.Key))
74                         result.FieldCotent[item.Key] = HttpUtility.UrlDecode(item.Value.ToString());
75                     else
76                         result.FieldCotent.Add(item.Key, HttpUtility.UrlDecode(item.Value.ToString()));
77                 }
78             }
79             return result;
80         }
GetPrintModel

 

文檔郵件合併域

  1 public class MergeDocument : IDisposable
  2     {
  3         public PrintModel Model { get; set; }
  4         public Document Doc { get; set; }
  5         private PrintFieldMergingCallback FieldCallback { get; set; }
  6         pu

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 這篇文章主要介紹redis的使用。 簡單介紹下redis,一個高性能key-value的存儲系統,支持存儲的類型有string、list、set、zset和hash。在處理大規模數據讀寫的場景下運用比較多。 1.連接Redis資料庫: 1)直接連接 2)連接池連接 連接池的原理是, 通過預先創建多個 ...
  • 1.在setting.py中,將DEBUG改為False,否則不會生效 2.DEBUG為False後,會導致靜態資源的路徑(STATIC_URL)失效, 簡單的做法,在開發時: 複雜的方法(django2.2.7,會導致404頁面的靜態資源的圖片無法載入) setting.py urls.py 3. ...
  • tomcat是8.0版本. 在eclipse啟動時,第二行報這個, 同時項目也沒載入(tomcat啟動成功了). 網上搜了半天, 試了半天, 沒搞定. 最後不經意間發現: <Context docBase="xxxx" path="/" reloadable="true"/> 這種不帶</Conte ...
  • 近日開始了移動端自動化測試的學習之路,決定在學習的過程中進行筆記,總結學習,印象或許會深刻一些。 [TOC] Android自動化環境準備 1、 Android SDK: 下載 Android SDK(可以先使用Android Studio輔助安裝),並設置 PATH 變數加入 SDK 的工具目錄; ...
  • Python 中如何實現參數化測試? 之前,我曾轉過一個單元測試框架系列的文章,裡面介紹了 unittest、nose/nose2 與 pytest 這三個最受人歡迎的 Python 測試框架。 本文想針對測試中一種很常見的測試場景,即參數化測試,繼續聊聊關於測試的話題,並嘗試將這幾個測試框架串聯起 ...
  • 新聞 "宣告.NET Core 3.1" "新書:Kevin Avignon的F 提升效率" ".NET Core 2.2將在2019年12月23日迎來終結" "Visual Studio 16.5預覽版1中升級了.NET Core Windows Forms設計器" "用於垃圾回收的運行時配置選項 ...
  • 前言本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理。作者:唯戀殊雨 目錄 pexpect fabric pexpect Pexpect 是 Don Libes 的 Expect 語言的一個 Python 實現,是一個用來啟動子程式 ...
  • [TOC] 數據可視化是用圖形或者表格的形式進行數據顯示,用圖形化的手段,清晰有效地傳遞與溝通信息。既要保證直觀易分析,又要保證美感。實現的對稀疏,肉眼無法分析的數據進行深入洞察。 下麵就介紹用python的一些方法進行可視化處理。 使用工具:jupyter notebook。 一:配置jupyte ...
一周排行
    -Advertisement-
    Play Games
  • 概述:在C#中,++i和i++都是自增運算符,其中++i先增加值再返回,而i++先返回值再增加。應用場景根據需求選擇,首碼適合先增後用,尾碼適合先用後增。詳細示例提供清晰的代碼演示這兩者的操作時機和實際應用。 在C#中,++i 和 i++ 都是自增運算符,但它們在操作上有細微的差異,主要體現在操作的 ...
  • 上次發佈了:Taurus.MVC 性能壓力測試(ap 壓測 和 linux 下wrk 壓測):.NET Core 版本,今天計劃準備壓測一下 .NET 版本,來測試並記錄一下 Taurus.MVC 框架在 .NET 版本的性能,以便後續持續優化改進。 為了方便對比,本文章的電腦環境和測試思路,儘量和... ...
  • .NET WebAPI作為一種構建RESTful服務的強大工具,為開發者提供了便捷的方式來定義、處理HTTP請求並返迴響應。在設計API介面時,正確地接收和解析客戶端發送的數據至關重要。.NET WebAPI提供了一系列特性,如[FromRoute]、[FromQuery]和[FromBody],用 ...
  • 原因:我之所以想做這個項目,是因為在之前查找關於C#/WPF相關資料時,我發現講解圖像濾鏡的資源非常稀缺。此外,我註意到許多現有的開源庫主要基於CPU進行圖像渲染。這種方式在處理大量圖像時,會導致CPU的渲染負擔過重。因此,我將在下文中介紹如何通過GPU渲染來有效實現圖像的各種濾鏡效果。 生成的效果 ...
  • 引言 上一章我們介紹了在xUnit單元測試中用xUnit.DependencyInject來使用依賴註入,上一章我們的Sample.Repository倉儲層有一個批量註入的介面沒有做單元測試,今天用這個示例來演示一下如何用Bogus創建模擬數據 ,和 EFCore 的種子數據生成 Bogus 的優 ...
  • 一、前言 在自己的項目中,涉及到實時心率曲線的繪製,項目上的曲線繪製,一般很難找到能直接用的第三方庫,而且有些還是定製化的功能,所以還是自己繪製比較方便。很多人一聽到自己畫就害怕,感覺很難,今天就分享一個完整的實時心率數據繪製心率曲線圖的例子;之前的博客也分享給DrawingVisual繪製曲線的方 ...
  • 如果你在自定義的 Main 方法中直接使用 App 類並啟動應用程式,但發現 App.xaml 中定義的資源沒有被正確載入,那麼問題可能在於如何正確配置 App.xaml 與你的 App 類的交互。 確保 App.xaml 文件中的 x:Class 屬性正確指向你的 App 類。這樣,當你創建 Ap ...
  • 一:背景 1. 講故事 上個月有個朋友在微信上找到我,說他們的軟體在客戶那邊隔幾天就要崩潰一次,一直都沒有找到原因,讓我幫忙看下怎麼回事,確實工控類的軟體環境複雜難搞,朋友手上有一個崩潰的dump,剛好丟給我來分析一下。 二:WinDbg分析 1. 程式為什麼會崩潰 windbg 有一個厲害之處在於 ...
  • 前言 .NET生態中有許多依賴註入容器。在大多數情況下,微軟提供的內置容器在易用性和性能方面都非常優秀。外加ASP.NET Core預設使用內置容器,使用很方便。 但是筆者在使用中一直有一個頭疼的問題:服務工廠無法提供請求的服務類型相關的信息。這在一般情況下並沒有影響,但是內置容器支持註冊開放泛型服 ...
  • 一、前言 在項目開發過程中,DataGrid是經常使用到的一個數據展示控制項,而通常表格的最後一列是作為操作列存在,比如會有編輯、刪除等功能按鈕。但WPF的原始DataGrid中,預設只支持固定左側列,這跟大家習慣性操作列放最後不符,今天就來介紹一種簡單的方式實現固定右側列。(這裡的實現方式參考的大佬 ...