c# – 用于缩短代码的动态图像名称
发布时间:2020-12-15 23:41:08 所属栏目:百科 来源:网络整理
导读:我得到了一个很长的代码,我打电话来检查棋盘上的瓷砖,并将相应的图像显示到具有相同图块名称的图片框中. private void DisplayBoardDisplayTile(string xtile,string piece) { if (xtile == "a1") { if (piece == "0000") { a1.Image = WindowsFormsApplicat
我得到了一个很长的代码,我打电话来检查棋盘上的瓷砖,并将相应的图像显示到具有相同图块名称的图片框中.
private void DisplayBoardDisplayTile(string xtile,string piece) { if (xtile == "a1") { if (piece == "0000") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0000; } //empty if (piece == "0001") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0001; } //white pawn if (piece == "0010") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0010; } //white rook if (piece == "0011") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0011; } //white knight if (piece == "0100") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0100; } //white bishop if (piece == "0101") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0101; } //white queen if (piece == "0110") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0110; } //white king if (piece == "0111") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0111; } //black pawn if (piece == "1000") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1000; } //black rook if (piece == "1001") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1001; } //black knight if (piece == "1010") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1010; } //black bishop if (piece == "1011") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1011; } //black queen if (piece == "1100") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1100; } //black king } 完整代码在此链接:https://pastebin.com/XLbEeZZH 如果图块是阴影的,我使用的图像名称以“S”开头,如果不是“U”. 我想以这种方式缩短代码(如果可能的话)使用名为的图像字典 tiles[xtile].Image = ?????."S"+piece; 或类似的东西. 这是我使用的image names的样本. 解决方法
你需要设置一些字典来映射xtile和piece以将代码缩小到一行,你只需要_tiles [xtile] .Image = _shadings [xtile] [piece] ;.
试试这个: private static Dictionary<string,System.Drawing.Image> _shaded = new Dictionary<string,System.Drawing.Image>() { { "0000",WindowsFormsApplication5.Properties.Resources.S0000 },{ "0001",WindowsFormsApplication5.Properties.Resources.S0001 },//etc { "1100",WindowsFormsApplication5.Properties.Resources.S1101 },}; private static Dictionary<string,System.Drawing.Image> _unshaded = new Dictionary<string,WindowsFormsApplication5.Properties.Resources.U0000 },WindowsFormsApplication5.Properties.Resources.U0001 },// etc { "1100",WindowsFormsApplication5.Properties.Resources.U1101 },PictureBox> _tiles = new Dictionary<string,PictureBox>() { { "a1",a1 },{ "b1",b1 },// etc { "h8",h8 },Dictionary<string,System.Drawing.Image>> _shadings = new Dictionary<string,System.Drawing.Image>>() { { "a1",_shaded },_unshaded },}; private void DisplayBoardDisplayTile(string xtile,string piece) { _tiles[xtile].Image = _shadings[xtile][piece]; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何使用SWIG将C运算符[]包装在命名空间内的模板类中?
- c – 使用boost :: any_range有什么好处?
- XML的Jaxp中DOM解析
- C#实现附件上传和下载功能
- 在C#中,“Customer cust = new Customer();”是做什么的?
- logback.xml实例
- c# – 使用RX(Reactive Extensions)创建20个延迟30毫秒的事
- postgresql – Postgres 8.3:“错误:缓存计划不能更改结果
- 如何添加包含路径到flycheck c / c -clang?
- 数组 – 如何从PostgreSQL 8.4中的string_to_array()返回一