加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读