WPF学习记录
目录
1. 获取Debug文件目录
string str = System.Environment.CurrentDirectory;
输出结果:X:/工程/bin/debug
2. npoi简单操作
public OutExcel(List<OutExcelModel> list)
{
IWorkbook wb = null;
using (FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.ReadWrite))
{
wb = WorkbookFactory.Create(fs);
}
ISheet sheet = wb.GetSheetAt(0);
IRow row = sheet.GetRow(2);
ICell cell = row.GetCell(1);
cell.SetCellValue(1);
using (FileStream newfs = new FileStream(outFilePath, FileMode.Create, FileAccess.ReadWrite))
{
wb.Write(newfs);
}
}
3. Textbox多行设置
TextWrapping=“Wrap” AcceptsReturn=“True” VerticalScrollBarVisibility=“Visible”
4. byte转16进制
Convert.ToString(item, 16)
5. CRC16校验
public byte[] CRC16(byte[] data)
{
int len = data.Length;
if (len > 0)
{
ushort crc = 0xFFFF;
for (int i = 0; i < len; i++)
{
crc = (ushort)(crc ^ (data[i]));
for (int j = 0; j < 8; j++)
{
crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
}
}
byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
byte lo = (byte)(crc & 0x00FF); //低位置
return new byte[] { lo, hi };
}
return new byte[] { 0, 0 };
}
6.读写app.config
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
//读取
string sqlServer = config.AppSettings.Settings["MySQLServer"].Value;
//写入
config.AppSettings.Settings["MySQLServer"].Value = "XXX";
//增加节点
config.AppSettings.Settings.Add("MySQLServer","XXX");
7.UI占位
<ContentPresenter Content="{Binding MyDate}" />
8.字体图片文件
要设置字体文件属性为资源
<TextBlock
Text=""
FontFamily="pack://application:,,,/../Icon/#iconfont" />
9.FluentFTP简单操作
using FluentFTP;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Windows;
namespace FtpTest
{
class Ftp
{
private static Ftp ftp = null;
static Ftp()
{
ftp = new Ftp();
}
private string user = "FtpTest";
private string password = "123";
private string ftpHost = "ftp://192.168.1.246/";
private string basePath = "/Test/"; //Ftp内部基础路径
private string tempPath = Environment.CurrentDirectory + "Temp/temp.jpg"; //下载文件临时储存路径(这个存在debug文件夹下)
/// <summary>
/// 把FTP上面的文件下载到本地,然后通过process打开
/// path:文件在ftp内的路径
/// </summary>
/// <param name="path"></param>
private void Open(string path)
{
using (FtpClient connection = new FtpClient())
{
connection.Host = ftpHost;
connection.Credentials = new NetworkCredential(user, password);
byte[] outbytes;
connection.Download(out outbytes, basePath+path);
if (outbytes==null)
{
MessageBox.Show("未找到该文件");
return;
}
using (FileStream fs = new FileStream(tempPath,FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite))
{
fs.Write(outbytes, 0, outbytes.Length);
}
using (Process pro = new Process())
{
pro.StartInfo.FileName = tempPath;
pro.Start();
}
}
}
/// <summary>
/// 直接将byte[]数组上传到Ftp上面指定路径path
/// </summary>
/// <param name="bytes"></param>
/// <param name="path"></param>
private void UpLoad(byte[] bytes,string path)
{
using (FtpClient connection = new FtpClient())
{
connection.Host = ftpHost;
connection.Credentials = new NetworkCredential(user, password);
connection.Upload(bytes, basePath+path);
}
}
}
}
10.NPOI简单插入新行
public void Level1WorkSheet(string path)
{
IWorkbook wb = null;
using (FileStream fs = new FileStream(level1Path, FileMode.Open, FileAccess.ReadWrite))
{
wb = WorkbookFactory.Create(fs);
}
ISheet sheet = wb.GetSheetAt(0);
IRow row = sheet.GetRow(4 + i);
var rowStyle = row.RowStyle;
if (i!=level1List.Count-1)
{
sheet.ShiftRows(5 + i, sheet.LastRowNum, 1,true,true);
var insertRow = sheet.CreateRow(5 + i);
if (rowStyle!=null)
{
insertRow.RowStyle = rowStyle;
}
insertRow.Height = row.Height;
for (int col = 0; col < row.LastCellNum; col++)
{
var cellsource = row.GetCell(col);
var cellInsert = insertRow.CreateCell(col);
var cellStyle = cellsource.CellStyle;
//设置单元格样式
if (cellStyle != null)
cellInsert.CellStyle = cellsource.CellStyle;
}
}
using (FileStream newfs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
wb.Write(newfs);
}
}
11.WPF元素隐藏显示与占位
// 摘要:
// 指定元素的显示状态。
public enum Visibility : byte
{
//
// 摘要:
// 显示元素。
Visible = 0,
//
// 摘要:
// 不显示元素,但是在布局中为元素保留空间。
Hidden = 1,
//
// 摘要:
// 不显示元素,并且不在布局中为它保留空间。
Collapsed = 2
}
12.单例模式
https://www.cnblogs.com/igeekfan/p/Singleton-Pattern.html
13.TextBox多行输入
<TextBox
TextWrapping="Wrap"
VerticalContentAlignment="Top"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"/>
14.XAML时间格式化
要求被格式化数据的类型是DateTime
StringFormat='yyyy-MM-dd'
StringFormat={}{0:yyyy-MM-dd}
15.viewmodel里面获取dispatcher
App.Current.Dispatcher
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗