【发布时间】:2019-05-11 17:11:20
【问题描述】:
我有一个相当简单的 WCF 程序,用户可以在其中单击两个按钮:ChangeBackGroundColour 和 ChangeButtonColour。
WCF 服务从颜色列表中返回一个随机颜色,并且该字符串通过 switch case 语句指示两个对象的颜色。
但是,对象的颜色不能相同。如何阻止重复?
这是我的代码:
前端
public partial class Front_End : Form
{
RandomColourServiceReference.RandomColoursServiceClient ws = null;
string BackGroundColour { get; set; }
string TextColour { get; set; }
public Front_End()
{
InitializeComponent();
}
private void Front_End_Load(object sender, EventArgs e)
{
ws = new RandomColourServiceReference.RandomColoursServiceClient();
}
private void BtnChangeBack_Click(object sender, EventArgs e)
{
BackGroundColour = ws.GenerateRandomColour();
switch (BackGroundColour)
{
case "Red":
this.BackColor = Color.Red;
break;
case "Blue":
this.BackColor = Color.Blue;
break;
case "Black":
this.BackColor = Color.Black;
break;
case "Purple":
this.BackColor = Color.Purple;
break;
case "Green":
this.BackColor = Color.Green;
break;
}
}
private void BtnChangeButton_Click(object sender, EventArgs e)
{
TextColour = ws.GenerateRandomColour();
switch (TextColour)
{
case "Red":
btnChangeButton.BackColor = Color.Red;
btnChangeBack.BackColor = Color.Red;
break;
case "Blue":
btnChangeButton.BackColor = Color.Blue;
btnChangeBack.BackColor = Color.Blue;
break;
case "Black":
btnChangeButton.BackColor = Color.Black;
btnChangeBack.BackColor = Color.Black;
break;
case "Purple":
btnChangeButton.BackColor = Color.Purple;
btnChangeBack.BackColor = Color.Purple;
break;
case "Green":
btnChangeButton.BackColor = Color.Green;
btnChangeBack.BackColor = Color.Green;
break;
}
}
}
RandomColourService
public class RandomColoursService : IRandomColoursService
{
public string GenerateRandomColour()
{
//Declare and initialize a list of string colours
List<String> colours = new List<String>();
colours.AddRange(new String[]{ "Red", "Blue", "Green", "Pink", "Purple", "Black"});
//create new instance of random
Random rand = new Random();
//return a random colour in array
return colours[rand.Next(0, colours.Count)];
}
}
IRandomColourService
[ServiceContract]
public interface IRandomColoursService
{
[OperationContract]
string GenerateRandomColour();
}
我觉得应该涉及一个 while 循环,但我不太确定。
或者也许我应该让 2 个对象从 wcf 返回,而不是只返回一个,然后检查它们在 wcf 中是否重复,如果是,则生成一个新对象?
所以它只会发送 2 种不同的颜色。那会是最好的吗?
*这是尝试添加更多属性后的更新代码
前端
public partial class Front_End : Form
{
RandomColoursService.RandomColoursServiceClient ws = null;
/*create two properties that can:385
A. set the initial colours for the form objects.
B. be passed to wcf service as a used color to be removed from the list of colours that could potentially be returned next. */
Color BackGroundColour { get; set; } = Color.Blue;
Color TextColour { get; set; } = Color.Red;
Color ButtonColour { get; set; } = Color.Black;
public Front_End()
{
//objects are initilized with colours.
InitializeComponent();
btnChangeBack.BackColor = ButtonColour;
btnChangeButton.BackColor = ButtonColour;
btnChangeText.BackColor = ButtonColour;
this.BackColor = BackGroundColour;
lblTitle.ForeColor = TextColour;
btnChangeBack.ForeColor = TextColour;
btnChangeButton.ForeColor = TextColour;
btnChangeText.ForeColor = TextColour;
}
private void Front_End_Load(object sender, EventArgs e)
{
//create new instance of wcf service
ws = new RandomColoursService.RandomColoursServiceClient();
}
private void BtnChangeBack_Click(object sender, EventArgs e)
{
//This list is populated with the with current colours
List<Color> UsedColours = new List<Color>() {TextColour, ButtonColour, BackGroundColour};
//Call for another colour, pass in the current colour to be removed from list.
BackGroundColour = ws.GenerateRandomColour(UsedColours);
//set object colour to returned colour
this.BackColor = BackGroundColour;
}
private void BtnChangeButton_Click(object sender, EventArgs e)
{
//This list is populated with the with current colours
List<Color> UsedColours = new List<Color>() {TextColour, BackGroundColour, ButtonColour};
//Call for another colour, pass in the current colour to be removed from list.
ButtonColour = ws.GenerateRandomColour(UsedColours);
//set object colour to returned colour
btnChangeButton.BackColor = ButtonColour;
btnChangeBack.BackColor = ButtonColour;
btnChangeText.BackColor = ButtonColour;
}
private void BtnChangeText(object sender, EventArgs e)
{
//This list is populated with the with current colours
List<Color> UsedColours = new List<Color>() { BackGroundColour, ButtonColour, TextColour};
//Call for another colour, pass in the current colour to be removed from list.
TextColour = ws.GenerateRandomColour(UsedColours);
//set object colour to returned colour
btnChangeButton.ForeColor = TextColour;
btnChangeBack.ForeColor = TextColour;
btnChangeText.ForeColor = TextColour;
lblTitle.ForeColor = TextColour;
}
}
}
IRandomColoursService
[ServiceContract]
public interface IRandomColoursService
{
[OperationContract]
//As a parameter, pass in a list of coloured.
//The list will be populated with the current colours on the front end,
//when a button is clicked a new call is made to wcf.
Color GenerateRandomColour(List<Color> UsedColours);
}
}
RandomColoursService
public class RandomColoursService : IRandomColoursService
{
public Color GenerateRandomColour(List<Color> UsedColours)
{
//This is a list of potential colours that wcf can send.
List<Color> Colours = new List<Color>()
{
Color.Red, Color.Blue, Color.Black, Color.Green, Color.Indigo, Color.Orange
};
//This is a list of available colours once the current object colours are removed from the list.
List<Color> AvailableColours = Colours.Except(UsedColours).ToList();
Random rand = new Random();
return Colours[rand.Next(0, AvailableColours.Count)];
}
}
为了澄清,我应该在调用 wcf 时传递一个 UsedColours 列表。从颜色列表中扣除 UsedColors,然后可用颜色列表保存剩余的颜色。然后每次调用都应该返回可用颜色列表中的一个随机颜色?
我哪里出错了?
【问题讨论】: