C#   发布时间:2022-04-13  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了支持组合的枚举大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

问题描述

虑这样一个需求:画布上的对象支持手势操作,手势操作模式有平移、缩放、旋转,对象可以支持一种或多种手势,如何定义这个手势操作模式?

就像文件的权限一样,只读、只写、读写,手势操作也可以这样设计。将手势操作模式定义为简单的枚举类型是不够的,我们需要表示不同模式的组合,需要支持位运算,因此每个枚举成员应该是位字段,即每个枚举成员都应该是2的幂。这样我们就可以使用&、|、^、~运算符对模式进行逻辑运算。手势操作模式定义如下:

public enum ManipulationModes
{
    None       = 0,
    Translate  = 2,
    Rotate     = 4,
    Scale      = 8,
}

此时会有一个问题,比如想表示平移旋转时,结果如下:

static void Main()
{
    ManipulationModes mode = ManipulationModes.Translate | ManipulationModes.Rotate;
    Console.WriteLine(modE); // 输出:6
}

我们期望像文件读写权限一样,打印结果能为Translate, Rotate,但打印输出结果为6,与期望不符。而且6不是ManipulationModes的成员,无法表示ManipulationModes操作模式。

FlagsAttribute

.NET提供了FlagsAttribute特性,表示声明为位字段的枚举类型,我们需要为ManipulationModes添加FlagsAttribute:

[Flags]
public enum ManipulationModes
{
    None       = 0,
    Translate  = 2,
    Rotate     = 4,
    Scale      = 8,
}

static void Main()
{
    ManipulationModes mode = ManipulationModes.Translate | ManipulationModes.Rotate;
    Console.WriteLine(modE); // 输出:Translate, Rotate
}

这样输出结果就符合期望值了,经过逻辑运算的值仍然是ManipulationModes的基础值。

逻辑运算

可以使用|运算符表示组合模式,使用&运算符判断是否支持特定模式,

// 使用 | 表示组合模式
var operMode = ManipulationModes.Translate | ManipulationModes.Rotate;
var transScaleMode = ManipulationModes.Translate | ManipulationModes.Scale;

// 使用 & 判断是否支持某种模式
Console.WriteLine($"operMode是否支持平移模式:{(operMode & ManipulationModes.TranslatE) == ManipulationModes.TranslatE}");
Console.WriteLine($"operMode是否支持平移、旋转模式:{(operMode & transScaleModE) == transScaleModE}");

WPF ManipulationModes源码

下述代码为WPF ManipulationModes的源码:

[Flags]
public enum ManipulationModes
{
    None        = 0x0000,
    TranslateX  = 0x0001,
    TranslateY  = 0x0002,
    Translate   = TranslateX | TranslateY,
    Rotate      = 0x0004,
    Scale       = 0x0008,
    All         = Translate | Rotate | Scale,
}

文章

大佬总结

以上是大佬教程为你收集整理的支持组合的枚举全部内容,希望文章能够帮你解决支持组合的枚举所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: