ArkTs 如何将$r(’app.string.xxx‘) 转成string字符串

资源转string类型

某些组件的入参并不支持 Resource 类型,比如 RichText(content: string),它只接收一个不可为空的字符串类型,这个时候想使用 string.json 内的资源,就需要用到 resourceManager 了



struct LoginPage {
	
  build() {
    Column() {
	  Text($r('app.string.title'))
	  // toString() 可以省略	  
	  RichText(getContext(this).resourceManager.getStringSync($r('app.string.title')).toString())
	}
  }
  
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为将敏感信息(比如数据库连接字符串)进行保护,可以对其进行加密处理。以下是一个示例代码,可以实现app.config文件中的数据库连接字符串的加密: 1. 创建一个控制台应用程序,然后在程序中使用以下代码: ``` using System; using System.Configuration; using System.Security.Cryptography; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { // 获取app.config中的数据库连接字符串 string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; // 加密字符串 string encryptedString = EncryptString(connectionString); // 将加密后的字符串写入app.config文件 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings["MyConnectionString"].Value = encryptedString; config.Save(); Console.WriteLine("String has been encrypted!"); } // 加密字符串 private static string EncryptString(string inputString) { byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 }; byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 }; byte[] inputBytes = Encoding.UTF8.GetBytes(inputString); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] outputBytes = null; // 创建加密器对象 using (ICryptoTransform encryptor = des.CreateEncryptor(key, iv)) { // 执行加密操作 outputBytes = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length); } // 将加密后的字节数组转成字符串 return Convert.ToBase64String(outputBytes); } } } } ``` 2. 在app.config文件中,添加以下内容: ``` <connectionStrings> <add name="MyConnectionString" connectionString="Data Source=(local);Initial Catalog=MyDatabase;User ID=myUsername;Password=myPassword" /> </connectionStrings> ``` 3. 运行程序,程序将会读取app.config文件中的数据库连接字符串,并将其加密后写入app.config文件中。加密后的连接字符串将会是一长串随机字符。 4. 在应用程序中,可以使用以下代码来获取解密后的数据库连接字符串: ``` string encryptedString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; string decryptedString = DecryptString(encryptedString); ``` 其中,DecryptString方法的实现如下: ``` // 解密字符串 private static string DecryptString(string encryptedString) { byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 }; byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 }; byte[] inputBytes = Convert.FromBase64String(encryptedString); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] outputBytes = null; // 创建解密器对象 using (ICryptoTransform decryptor = des.CreateDecryptor(key, iv)) { // 执行解密操作 outputBytes = decryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length); } // 将解密后的字节数组转成字符串 return Encoding.UTF8.GetString(outputBytes); } } ``` 这样,应用程序就可以安全地使用app.config文件中的数据库连接字符串了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值