
C#
C#语言及特性
sdhongjun
这个作者很懒,什么都没留下…
展开
-
c#连接MySQL显示SSL Authentication Error错误处理
C#连接MySQL数据库时,提示。排除方法,在连接字符串中添加。原创 2023-03-11 15:44:00 · 2048 阅读 · 1 评论 -
WPF DataGrid多Image显示, 导致“没有足够的内存继续执行程序“问题
WPF DataGrid 多图显示 没有足够的内存继续执行程序原创 2023-03-04 09:06:51 · 571 阅读 · 0 评论 -
StreamJsonRpc使用TCP数据流通讯
StreamJsonRpc是微软维护的开源JSON-RPC的.NET库使用非常方便,常用的操作方式是服务器端通过JsonRpc的Attach静态函数与一个本地类实例实现对外接口,客户端通过Attach方法附加到一个Stream,然后调用Invoke或InvokeAsync函数实现函数的基本操作,详细介绍可参阅https://www.cnblogs.com/willick/p/13233704.html精致码农相关文章。我在搜索StreamJsonRpc相关资料时发现网上全是使用Pip管道实现的进程间通讯,原创 2022-05-02 18:45:13 · 1577 阅读 · 0 评论 -
VS2022编译时禁用Nullable类型检查
禁用Nullable特性编译检查原创 2022-03-01 16:03:14 · 2911 阅读 · 2 评论 -
VS2022 .netcore启用本地代码调试
调出项目属性界面在项目上点击右键,选择Properties(属性)页面。选择Debug属性选中Debug分项中的General后点击Open debug launch profiles UI链接。启用本地代码调试选择Enable native code debugging启用本地代码调试。...原创 2022-03-01 11:07:16 · 3727 阅读 · 0 评论 -
VS2022/VS2019/VS2017 .netcore项目设置Debug与Release输出相同目录不同名称
更改.csproj文件,在其中增加<AssemblyName />与<OutDir/>节,详细代码如下: <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net5.0-windows</TargetFramework> <Nullable>enable</Nullable> <U原创 2022-03-01 10:48:23 · 1829 阅读 · 0 评论 -
WPF Debug与Release编译输出相同目录
项目类型.NetCore<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net5.0-windows</TargetFramework> <UseWPF>true</UseWPF> <AssemblyName Condi原创 2021-09-15 14:25:34 · 543 阅读 · 0 评论 -
WPF中实现WindowsFormsHost右键菜单功能
MainWindow.xaml<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blen.原创 2021-05-20 13:03:45 · 578 阅读 · 0 评论 -
C# float数组转换为byte数组
通过Buffer类的BlockCopy函数实现不同类型数组之间转换,函数原型:public static void BlockCopy (Array src, int srcOffset, Array dst, int dstOffset, int count);示例:static byte[] GetBytes(float[] values) { var result = new byte[values.Length * sizeof(double)]; Buffer.BlockC原创 2021-05-20 11:09:34 · 4719 阅读 · 1 评论 -
C# 获取PATH环境变量中可执行文件绝对路径
public static string FindFilePath(string file, string localPath){ file = Environment.ExpandEnvironmentVariables(file); if (!File.Exists(file)) { if (Path.GetDirectoryName(file) == String.Empty) { foreach (string tes原创 2021-05-09 17:59:33 · 743 阅读 · 0 评论 -
C#定义联合体时相同偏移位置不能同时包含值类型与类类型成员变量
注意C#定义联合体时, 无法在相同偏移同时使用值类型与类。using System;using System.Runtime.InteropServices;namespace ConsoleApp1{ [StructLayout(LayoutKind.Sequential, Pack = 8)] public struct PtXY { public float x; public float y; } [StructLa原创 2021-04-16 15:41:39 · 805 阅读 · 0 评论 -
C#调用VC动态库, VC正则表达式延时问题
今天在测试软件时发现使用纯VC软件刷新IO状态无明显延时, 但使用C#程序调用VC编写的动态库有较明白延时, 经测试发现出现延时的根本原因为: VS2015的VC动态库中使用C++11标准的正则表达式搜索数据算法。使用标准搜索算法后此问题解决。使用C++标准库的正则表达式库, C#下调用时耗时为1至2秒(100个IO测试), VC下调用的耗时30至100毫秒左右。修改前 string re...原创 2020-05-07 15:35:58 · 206 阅读 · 0 评论 -
Simple deserialization of XML to C# object
文章目录Simple deserialization of XML to C# object1. Prepare XML string2. Prepare C# object3. Create XML serializer4. Create StringReader object5. Finally, deserialize to your C# objectSimple deserializa...转载 2020-02-06 21:49:28 · 160 阅读 · 0 评论 -
wpf 嵌套表滚动设置
在WPF中若嵌套DataGrid需要滚动支持时需要如下设置父DataGridScrollViewer.CanContentScroll="False"子DataGridIsReadOnly="True"ScrollViewer.CanContentScroll="False"IsEnabled="False"或IsHitTestVisible = "False"参考网址:ht...原创 2020-01-13 21:23:58 · 640 阅读 · 0 评论 -
c#中如何使用DLL导出字符串指针
I would like to import a function like this:[return: MarshalAs(UnmanagedType.LPWStr)][DllImport("DLL.dll", EntryPoint="FuncUtf16", ExactSpelling=true, PreserveSig=true, CharSet=CharSet.Unicode)]pub...转载 2019-12-24 17:17:57 · 1140 阅读 · 0 评论 -
WPF枚举指定类型控件
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject{ if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetCh...原创 2019-12-08 22:33:09 · 479 阅读 · 0 评论 -
Dynamic loading and unloading of unmanaged DLLs
ExampleWhen using the DllImport attribute you have to know the correct dll and method name at compile time. If you want to be more flexible and decide at runtime which dll and methods to load, you ca...转载 2019-11-16 14:04:53 · 201 阅读 · 0 评论 -
WinForm窗口借助DLL显示MFC控件
1 DLL导出函数定义注意示例代码使用.def文件定义导出函数class ShowPropWndHelper{public: ShowPropWndHelper() : _unit(nullptr) { } ~ShowPropWndHelper() { Destroy(); } bool Create(HWND...原创 2019-11-15 19:55:53 · 1076 阅读 · 3 评论 -
C#调试程序出现托管调试助手 "DisconnectedContext"、托管调试助手 "ContextSwitchDeadlock"错误的处理
错误现象调试C#代码出现如下错误,然后多次点击按钮后软件正常运行托管调试助手 "DisconnectedContext"Message=托管调试助手 “DisconnectedContext”:“针对此 RuntimeCallableWrapper 向 COM 上下文 0xbf9e03f8 的转换失败,错误如下: 被调用的对象已与其客户端断开连接。 (异常来自 HRESULT:0x80010...原创 2019-11-13 10:20:24 · 12397 阅读 · 9 评论 -
C#调用C++回调函数中动态数组参数定义
直接上代码,注意SizeParaIndex的使用, 其代表回调函数传入参数数组长度在当前回调函数中的参数顺序,若此参数设置不正确会导致无法正确解析数组长度,直接影响用户定义回调函数实现。//回调函数定义[UnmanagedFunctionPointer(CallingConvention.Cdecl)]public delegate void FnDiDoMonitor([MarshalAs...原创 2019-11-13 09:31:49 · 1113 阅读 · 0 评论 -
How to make a callback to C# from C/C++ code
转自:https://www.codeproject.com/Tips/318140/How-to-make-a-callback-to-Csharp-from-C-CplusplusThis post shows how to make a callback to C# from C/C++Almost everyone knows how to make a call to a fun...转载 2019-11-11 22:14:14 · 186 阅读 · 0 评论 -
CefSharp向浏览器中指定DOM发送点击事件
引用CefSharp的SendMouseClickEvent,通过此函数发送鼠标点击事件。操作方法先借助jQuery获取指定DOM的偏移,然后发送SendMouseClickEvent完成模拟点击操作。函数/// <summary>/// 模拟点击指定选择符DOM元素/// </summary>/// <param name="selector">...原创 2019-11-03 10:36:58 · 8902 阅读 · 0 评论 -
C# WPF如何使用App.config中的连接字符串
1. 引用System.Configuration命名空间2. 使用App.config中的连接字符串App.config配置:&lt;?xml version="1.0" encoding="utf-8" ?&gt;&lt;configuration&gt; &lt;startup&gt; &原创 2018-09-03 14:37:51 · 2606 阅读 · 0 评论 -
WPF DataGrid根据内容设置行颜色
转载:https://code.4noobz.net/wpf-change-color-of-a-row-in-a-datagrid-depending-on-the-value/效果图XAML代码<Window x:Class="DataTrigger_Test.MainWindow" xmlns="http://schemas.microsoft.com/winf...转载 2019-02-15 23:08:21 · 11123 阅读 · 4 评论 -
WPF解决因未捕获异常导致软件崩溃堆栈
在App中增加构造函数并设置UnhandledException代理函数public App(){ var currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += CurrentDomain_UnhandledException; } private voi...原创 2019-01-20 23:07:25 · 1321 阅读 · 0 评论 -
WPF中ComboBox控件在允许编辑时的数据绑定方法
在WPF中若ComboBox允许用户输入,其绑定的对象直接使用Text,而不是SelectedItem属性。XAML示例:<ComboBox IsEditable="True" ItemsSource="{Binding YJCodeList}" Text="{Binding YJCode}"/>...原创 2019-01-04 07:09:07 · 3900 阅读 · 4 评论 -
ComboBox绑定True与False到对应的索引项
转载:https://stackoverflow.com/questions/4335339/how-to-bind-a-boolean-to-combobox-in-wpf定义转换类using System.Globalization;using System.Windows.Data;public class BoolToIndexConverter : IValueConverter...转载 2018-12-09 23:04:24 · 589 阅读 · 0 评论 -
C#获取本机IP
转自https://stackoverflow.com/questions/37977619/get-local-ip-address-of-ethernet-interface-in-c-sharpusing System.Net;using System.Net.Sockets;public static string GetIpV4(){ try { ...转载 2018-12-06 21:44:43 · 734 阅读 · 0 评论 -
获取DataGrid行指定列值
1.获取选中行2.选中行转换为DataRowView3.获取指定列示例代码//dg为DataGrid控件名称if (null != dg.SelectedItem) { DataRowView drv = dg.SelectedItem as DataRowView; var id = drv["id"].ToString();}...原创 2018-12-14 00:05:19 · 1978 阅读 · 0 评论 -
WPF中IsEditable=True的ComboBox自动获取焦点方法
在WPF中若让界面中某个TextBox在显示时获取输入焦点时,可以按如下方式配对XAML文件。&lt;Window FocusManager.FocusedElement="{Binding ElementName=编辑控件名}"&gt; &lt;!--其他控件--&gt;&lt;/Window&gt;可编辑下拉列表组件,无法比照TextBox那样自动获取输入焦点,但原创 2019-03-03 20:30:00 · 2659 阅读 · 2 评论 -
WPF处理未捕捉异常, 防止软件崩溃
1.在App.xaml中增加DispatcherUnhandledException处理消息<Application x:Class="SmartMeasureSystem.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="htt...原创 2019-03-11 15:27:01 · 1159 阅读 · 0 评论 -
WPF调用SerialPort的Close函数导致软件宕机问题解决方法
在使用SerialPort进行串口数据的读写时,若在读写数据正在进行的过程中调用SerialPort的Close函数,可能会导致软件无响应, 这种无响应的情况在读写数据频繁时变得更加频繁,综合Google搜索结果,经验证在DataReceived函数中使用Dispatcher.BeginInvoke方法进行界面数据处理可以完美解决Close串口时出现的软件无响应问题。示例代码如下:private...原创 2019-03-11 15:50:35 · 1030 阅读 · 1 评论 -
C#调用C++动态库回调函数
1.C++动态库导出函数定义typedef void(__stdcall* fnCB)(int);__declspec(dllexport)void __stdcall TestCB(fnCB fn, int val){ fn(val);}注意:需要在.def文件中声明导出函数名称LIBRARYEXPORTS TestCB2.C#调用动态库函数namespa...原创 2019-03-15 21:42:03 · 1276 阅读 · 0 评论 -
C#结构体使用bool类型,并使其长度为1
Pack=1MarshalAs(UnmanagedType.U1)MarshalAs(UnmanagedType.ByValArray, ArraySubType =UnmanagedType.U1, SizeConst =2) [StructLayout(LayoutKind.Sequential, Pack =1)] public struct Star...原创 2019-03-19 12:06:44 · 1507 阅读 · 0 评论 -
WPF中WindowsFormsHost对象层叠实现
1.添加System.Windows.Forms与WindowsFormsIntegration引用在引用中分别添加System.Windows.Forms与WindowsFormsIntegration对象,如下图。2.在XAML中增加引用对象的System.Windows.Forms的引用引用命名空间设置为wf(xmlns:wf="clr-namespace:System.Window...原创 2019-04-01 15:55:19 · 4346 阅读 · 4 评论 -
C#函数传入数组到C++动态库
传入引用数组参数,函数声明:public bool GetStatus(short channelId, ref CardStatus[] cardStatus, out int count){ //.....}原创 2019-09-03 15:58:59 · 3632 阅读 · 0 评论 -
C#中常用结构体与字节数组转换辅助函数
/// <summary>/// 结构体转换为字节数组/// </summary>/// <typeparam name="T">结构体类型</typeparam>/// <param name="obj">结构体对象</param>/// <returns>字节数组</returns>in...原创 2019-09-04 14:24:51 · 225 阅读 · 0 评论 -
SQL Server插入中文数据出现乱码问题
转载自:https://www.cnblogs.com/yichengbo/archive/2011/08/12/2135892.html问题如上图。创建数据库的代码—创建promary表create table promary ( proID int primary key, proName varchar(50) not null )出现上图所示内容是因为proName...转载 2018-11-18 00:02:37 · 7644 阅读 · 4 评论 -
c#中接口与抽象类区别
IntroductionIn this article, I will explain about abstract class and interface, the most important topics to cover. Most of the interviewers ask about the abstract class and interface. I will cover a...转载 2018-11-22 09:10:20 · 1181 阅读 · 0 评论 -
WPF 线程更新UI界面
在线程中无法直接操作UI元素,可以通过线程的Dispatch.Invoke方法来更新UI界面。XAML界面&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xam...原创 2018-10-08 21:07:45 · 2456 阅读 · 0 评论