c# – 用于缩短代码的动态图像名称

c# – 用于缩短代码的动态图像名称,第1张

概述我得到了一个很长的代码,我打电话来检查棋盘上的瓷砖,并将相应的图像显示到具有相同图块名称的图片框中. private void DisplayBoardDisplayTile(string xtile, string piece) { if (xtile == "a1") { if (piece == "0000") { a1.Image = 我得到了一个很长的代码,我打电话来检查棋盘上的瓷砖,并将相应的图像显示到具有相同图块名称的图片框中.

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];}
总结

以上是内存溢出为你收集整理的c# – 用于缩短代码的动态图像名称全部内容,希望文章能够帮你解决c# – 用于缩短代码的动态图像名称所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1220503.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存