c# - 无法激活 Windows 应用商店应用程序“不支持操作”
问题描述
我正在尝试将 char 数组转换为字符串,并将字符串放在 TextBlock 中。具体行'_textBl.Text = word;' 使我的程序无法运行,这是整个代码。
public sealed partial class MainPage : Page
{
int _currentIndex;
string _currentWord;
string[] _strArr = { "ant", "bee", "spider", "mosquito" };
int _difficulty = 1;
public MainPage()
{
Random rnd = new Random();
_currentIndex = rnd.Next(0, 5);
foreach (char c in _strArr[_currentIndex])
{
_currentWord = _strArr[_currentIndex];
string _temp = _currentWord;
char[] _wordArr = _currentWord.ToCharArray();
for (int i = 0; i < _wordArr.Length; i++)
{
_wordArr[i] = '_';
}
string word = new string(_wordArr);
_textBl.Text = word;
}
this.InitializeComponent();
}
}
}
解决方案
您可以在字符串中插入空格(char)160
,我简化了您的代码,如下所示。
请注意,_strArr
实现的只有 4 个项目,如果你使 rnd.Next(0, 5),它会导致访问超出范围的问题。
int _currentIndex;
string _currentWord;
string[] _strArr = { "ant", "bee", "spider", "mosquito" };
int _difficulty = 1;
Random rnd = new Random();
_currentIndex = rnd.Next(0, 4);
foreach (char c in _strArr[_currentIndex])
{
_currentWord = _strArr[_currentIndex];
string rest = string.Empty;
foreach (var item in _currentWord)
{
rest += "_" + (char)160;
}
MyTb.Text = rest;
}
推荐阅读
- java - 将只有“行”和“列”的 JSON 响应解析为 POJO
- python - 从 Python 中的嵌套 for 循环中绘制子图
- javascript - Webpack 和 svg-inline-loader 不起作用
- git - Git pull vs fetch - 新获取的分支没有区别?
- javascript - 意外的标记 '。' 尽管我有 95% 的把握,但我想我会放一个“。”。这里
- mysql - 使用 MySQL 限定值时我犯了错误吗?我已经运行了同一个查询的多个版本,但每个版本都不会输出任何内容
- r - 如何绘制彼此相邻的图表?
- css - 重叠的网格元素:第二个 div 无法加载
- c# - 如何确保 C# 应用程序从睡眠中醒来后会响应(Windows 10)?
- mongodb - 如何(从客户端的角度)获取使用(和免费)到 MongoDB 的连接数?