首页 > 解决方案 > Xamarin 转换 rawValue

问题描述

我试图在 Xamarin.MacOS 中强调 NSButton 的标题。在可变属性字符串中,我使用

attrString.AddAttribute(NSStringAttributeKey.UnderlineStyle, NSNumber.FromInt32((int)NSUnderlineStyle.Single), range);

它不起作用。Swift 中的建议是使用

let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue]

在 Xamarin.MacOS 中,没有 NSUnderlineStyle.Single.RawValue。如何获取 NSUnderlineStyle 枚举的 RawValue?谢谢。

标签: xamarinxamarin.mac

解决方案


首先,快速回顾。NSUnderlineStyle是具有以下值的枚举:

NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,

NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,

NSUnderlineByWord = 0x8000 

所以我们可以检查以下代码

string prefixedHex = "0x01";
            
int intValue = Convert.ToInt32(prefixedHex, 16);

NSMutableAttributedString str = new NSMutableAttributedString();
      str.AddAttribute(NSStringAttributeKey.UnderlineStyle,NSNumber.FromInt32(1),range);

button.AttributedTitle = str;

推荐阅读