WPF 常用代码段

13 篇文章 5 订阅

【WPF 基础】WPF 常用代码段

1. 获取指定目录下文件、文件夹

DirectoryInfo folder = new DirectoryInfo(@"D:demos\images");
FileInfo[] files = folder.GetFiles();
DirectoryInfo[] folders = folder.GetDirectories();

// 判断文件夹是否存在,不存在则创建
DirectoryInfo folder = new DirectoryInfo(vm.StrSaveImagePath);
if (!folder.Exists)
{
    folder.Create();
}
// 删除文件
File.Delete(files[i].FullName);

// 一些属性
FileInfo.Exists:文件是否存在;
FileInfo.Name:文件名;
FileInfo.Extensioin:文件扩展名;
FileInfo.FullName:文件完整路径;
FileInfo.Directory:文件所在目录;
FileInfo.DirectoryName:文件完整路径;
FileInfo.Length:文件大小(字节数);
FileInfo.IsReadOnly:文件是否只读;
FileInfo.CreationTime:文件创建时间;
FileInfo.LastAccessTime:文件访问时间;
FileInfo.LastWriteTime:文件修改时间;

2. string.Format 格式化输出

string.Format("{0:N1}", 56789);           // 56,789.0
string.Format("{0:N2}", 56789);           // 56,789.00
string.Format("{0:F1}", 56789);           // 56789.0
string.Format("{0:F2}", 56789);           // 56789.00
(56789 / 100.0).ToString("#.##");         // 567.89
(56789 / 100).ToString("#.##");           // 567
string.Format("{0:C}", 0.2)               // ¥0.20 
string.Format("{0:C1}", 23.15)            // ¥23.2
string.Format("{0:D3}", 23)               // 023
string.Format("{0:D2}", 1223)            // 1223
string.Format("{0:N}", 14200)            // 14,200.00
string.Format("{0:N3}", 14200.2458)      // 14,200.246
string.Format("{0:P}", 0.24583)          // 24.58%
string.Format("{0:P1}", 0.24583)         // 24.6%
string.Format("{0:0000.00}", 12394.039)  // 12394.04
string.Format("{0:0000.00}", 194.039)    // 0194.04
string.Format("{0:###.##}", 12394.039)   // 12394.04
string.Format("{0:####.#}", 194.039)     // 194`

3. 日期时间

// 当前时间
DateTime currentTime = DateTime.Now;
int year = currentTime.Year;
int month = currentTime.Month;
int day = currentTime.Day;
int hour = currentTime.Hour;
int minute = currentTime.Minute;
int second = currentTime.Second;
int millisecond = currentTime.Millisecond;

// 间隔
DateTime t1 = new DateTime(2021, 8, 20);
DateTime t2 = DateTime.Now;
double day = t2.Subtract(t1).TotalDays;

// 格式化输出
string.Format("{0:d}", System.DateTime.Now)  // 2009-3-20
string.Format("{0:D}", System.DateTime.Now)  // 2009年3月20日
string.Format("{0:f}", System.DateTime.Now)  // 2009年3月20日 15:37
string.Format("{0:F}", System.DateTime.Now)  // 2009年3月20日 15:37:52
string.Format("{0:g}", System.DateTime.Now)  // 2009-3-20 15:38
string.Format("{0:G}", System.DateTime.Now)  // 2009-3-20 15:39:27
string.Format("{0:m}", System.DateTime.Now)  // 3月20日
string.Format("{0:t}", System.DateTime.Now)   // 15:41
string.Format("{0:T}", System.DateTime.Now)  // 15:41:50

4. 线程

Thread thread = new Thread(TestUnitThread);
thread.Start();
thread.IsBackground = true;

private void TestUnitThread()
{
    Thread.Sleep(2000);
}

5. 计时器

private DispatcherTimer del_timer;

del_timer = new DispatcherTimer
{
    // 一天执行一次
    Interval = new TimeSpan(1, 0, 0, 0)
};
del_timer.Tick += new EventHandler(DeleteFiles);
del_timer.Start();

private void DeleteFiles(object sender, EventArgs e)
{
    DeleFiles();
}

private void DeleFiles()
{

}

6. 保存配置文件 Serializable 序列化和反序列化

MVVM 模式不行,可创建一个相同内容的类

// CfgBearingVMs.cs
[Serializable]
public class CfgBearingVMs
{
    private int intSelectedIndexDel;
    public int IntSelectedIndexDel
    {
        get => intSelectedIndexDel;
        set { intSelectedIndexDel = value; }
    }

    private string strSaveImagePath;
    public string StrSaveImagePath
    {
        get => strSaveImagePath;
        set { strSaveImagePath = value; }
    }

    public CfgBearingVMs()
    {
        IntSelectedIndexDel = 2;
        StrSaveImagePath = @"C:\Users\Administrator";
    }
}

// CfgBearingVM.cs
public class CfgBearingVM : ViewModelBase
{
    private int intSelectedIndexDel;
    public int IntSelectedIndexDel
    {
        get => intSelectedIndexDel;
        set => Set(ref intSelectedIndexDel, value);
    }

    private string strSaveImagePath;
    public string StrSaveImagePath
    {
        get => strSaveImagePath;
        set => Set(ref strSaveImagePath, value);
    }

    public CfgBearingVM()
    {
        IntSelectedIndexDel = 2;
        StrSaveImagePath = @"C:\Users\Administrator";
    }
}

// 保存
CfgBearingVMs vm = new CfgBearingVMs(EnumAcqMode, IntSelectedIndexAcq, IntSelectedIndexDel, StrSaveImagePath);
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("setting/setting.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, vm);
stream.Close();

// 读取
using (FileStream stream = new FileStream("setting/setting.bin", FileMode.Open, FileAccess.Read, FileShare.Read))
{
    BinaryFormatter b = new BinaryFormatter();
    CfgBearingVMs vm = b.Deserialize(stream) as CfgBearingVMs;
    stream.Close();
}

7. 屏幕截图

private void CaptureScreen()
{
    System.Drawing.Size size = Screen.PrimaryScreen.Bounds.Size;
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(size.Width, size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
    g.CopyFromScreen(0, 0, 0, 0, size);
    bitmap.Save("screen.png", System.Drawing.Imaging.ImageFormat.Png);
}

8. TextBlock 多行文本

// 后台代码
TextBlock1.Text = "AAAAAAA\nBBBBBBBB";

XAML代码
<TextBlock Text=“AAAAAAA BBBBBB”/>

9. 控件相对位置

// 相对于父级偏移量
Vector vector = VisualTreeHelper.GetOffset(myTextBlock);
Point currentPoint = new Point(vector.X, vector.Y);
// 相对于 WIndow 偏移量
GeneralTransform transform = myTextBlock.TransformToAncestor(this);
Point currentPoint = transform .Transform(new Point(0, 0));

10. 屏幕尺寸

// 全屏尺寸
double width = SystemParameters.PrimaryScreenWidth;
double height = SystemParameters.PrimaryScreenHeight;

// 不含任务栏尺寸
double width = SystemParameters.WorkArea.Width;
double height = SystemParameters.WorkArea.Height;

11. 显示图像

XAML 代码
<Image Source=“pack://application:,/images/test.bmp”/>

BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(@"test.png", UriKind.RelativeOrAbsolute);
image.EndInit();
ImageShow.Source = image;

12. 程序暂停

// 线程里直接
Thread.Sleep(1000);

public static class DispatcherHelper
{
    [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    public static void DoEvents()
    {
        DispatcherFrame frame = new DispatcherFrame();
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrames), frame);
        try 
        { 
            Dispatcher.PushFrame(frame); 
        }
        catch (InvalidOperationException) 
        { 
        }
    }
    private static object ExitFrames(object frame)
    {
        ((DispatcherFrame)frame).Continue = false;
        return null;
    }
}

// 暂停 1s
DateTime t = DateTime.Now.AddMilliseconds(1000);
while (DateTime.Now < t)
{
    DispatcherHelper.DoEvents();
}

13. 试错

try
{
    // do something
}
catch (Exception ex)
{
    string msg = ex.Message;
}

14. 一维数组保存成文本

double[] a1 = new double[4];
a1[3] = 8;
File.WriteAllLines("a1.txt", a1.Select(d => d.ToString()));

15. 文件路径

string dirPath = @"D:\TestDir";
string filePath = @"D:\TestDir\TestFile.txt";
// 当前路径
Environment.CurrentDirectory;
// 文件或文件夹所在目录
Path.GetDirectoryName(filePath);     // D:\TestDir
Path.GetDirectoryName(dirPath);      // D:\
// 文件扩展名
Path.GetExtension(filePath);         // .txt
// 文件名
Path.GetFileName(filePath);          // TestFile.txt
Path.GetFileName(dirPath);           // TestDir
Path.GetFileNameWithoutExtension(filePath); // TestFile
// 绝对路径
Path.GetFullPath(filePath);          // D:\TestDir\TestFile.txt
Path.GetFullPath(dirPath);           // D:\TestDir  
// 更改扩展名
Path.ChangeExtension(filePath, ".jpg"); // D:\TestDir\TestFile.jpg
// 根目录
Path.GetPathRoot(dirPath);           //D:\      
// 生成路径
Path.Combine(new string[] { @"D:\", "BaseDir", "SubDir", "TestFile.txt" }); // D:\BaseDir\SubDir\TestFile.txt
// 生成随机文件
Path.GetRandomFileName();
// 创建临时文件
Path.GetTempFileName();
// 返回当前系统的临时文件夹的路径
Path.GetTempPath();
// 文件名中无效字符
Path.GetInvalidFileNameChars();
// 路径中无效字符
Path.GetInvalidPathChars();

16. 渐变色

// XAML 
<Grid x:Name="GridBack">
    <Grid.Background>
        <LinearGradientBrush>
            <LinearGradientBrush.GradientStops>
                <GradientStop Offset="0" Color="Red"/>
                <GradientStop Offset="0.5" Color="Indigo"/>
                <GradientStop Offset="1.0" Color="Violet"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
    </Grid.Background>
</Grid>

// 等价后台代码
GradientStop color1 = new GradientStop
{
    Offset = 0,
    Color = Colors.Red,
};
GradientStop color2 = new GradientStop
{
    Offset = 0.5,
    Color = Colors.Indigo,
};
GradientStop color3 = new GradientStop
{
    Offset = 1,
    Color = Colors.Violet,
};
LinearGradientBrush brush = new LinearGradientBrush();
brush.GradientStops.Add(color1);
brush.GradientStops.Add(color2);
brush.GradientStops.Add(color3);
GridBack.Background = brush;

17. Button 复杂背景

<Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="White" BorderBrush="White">
    <Grid>
        <Polygon Points="100,25 125,0 200,25 125,50" Fill="LightBlue"/>
        <Polygon Points="100,25 75,0 0,25 75,50" Fill="LightPink"/>
    </Grid>
</Button>

18. 后台设置元素绑定

// XAML 绑定
<Slider x:Name="SliderFont" Margin="10" Minimum="10" Maximum="40" SmallChange="1" LargeChange="4"/>
<Button x:Name="ButtonFont" Content="Binding fontsize" Margin="5"
        FontSize="{Binding ElementName=SliderFont, Path=Value, Mode=TwoWay}"/>

// 后台绑定
Binding binding = new Binding
{
    Source = SliderFont,
    Path = new PropertyPath("Value"),
    Mode = BindingMode.TwoWay,
};
ButtonFont.SetBinding(FontSizeProperty, binding);

// 注意设置 Mode 为 TwoWay,否则若其它地方有修改,则绑定失效,比如
ButtonFont.FontSize = 30;

19. 资源样式动画

// 资源定义
<Window.Resources>
    <ImageBrush x:Key="TileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 16 16" Opacity="0.5"
                    ImageSource="Resource\Image\icon.ico"/>
    
    <Style x:Key="BigFontButtonStyle" TargetType="Button">
            <Setter Property="FontFamily" Value="Times New Roman"/>
            <Setter Property="FontSize" Value="24"/>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
    
    <!-- 资源继承 -->
    <Style x:Key="EmphasizeBigFont" BasedOn="{StaticResource BigFontButtonStyle}" TargetType="Button">
            <Setter Property="Control.Foreground" Value="Magenta"/>
            <Setter Property="Control.Background" Value="DarkBlue"/>
        </Style>

    <!-- 关联事件 -->
    <Style x:Key="MouseOverHighlightStyle" TargetType="TextBlock">
            <Setter Property="Padding" Value="5"/>
            <EventSetter Event="MouseEnter" Handler="TextBlock_MouseEnter"/>
            <EventSetter Event="MouseLeave" Handler="TextBlock_MouseLeave"/>
            <!-- 事件触发器,字体缩放效果 -->
            <Style.Triggers>
                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize" To="24"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:1" Storyboard.TargetProperty="FontSize"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
</Window.Resources>

// 资源使用
<Button Content="DynamicResource" Margin="10" Background="{DynamicResource TileBrush}"/>
<Button Content="StaticResource" Margin="10" Background="{StaticResource TileBrush}"/>
<Button Content="ButtonStyle" Margin="10" Style="{StaticResource EmphasizeBigFont}"/>
<TextBlock Text="Hover over me" Margin="10" Style="{StaticResource MouseOverHighlightStyle}"/>

// 后台代码
// 后台修改资源,动态资源改变
Resources["TileBrush"] = new SolidColorBrush(Colors.LightGoldenrodYellow);

private void TextBlock_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    (sender as TextBlock).Background = new SolidColorBrush(Colors.LightGoldenrodYellow);
}

private void TextBlock_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    (sender as TextBlock).Background = null;
}

20. VS2019 C# 类注释模板

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\2052\Class\Class.cs

namespace $rootnamespace$
{
	///
	/// ----------------------------------------------------------------
	/// Copyright @Taosy.W $year$ All rights reserved
	/// Author      : Taosy.W
	/// Created Time: $time$
	/// Description :
	/// ------------------------------------------------------
	/// Version      Modified Time            Modified By    Modified Content
	/// V1.0.0.0     $time$    Taosy.W                 
	///
    public class $safeitemrootname$
    {
    }
}

21. 子线程更新UI

// 在新的线程里用如下方式更新到界面
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(ThreadStart)delegate ()
{
    // 列表更新
    DataListNG.Add(dataModel);
}
);

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cool2Feel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值