在编辑!中有一个更好的解释
我正在通过C#学习(我是一个学生),我正在自己做一个游戏。我是MasterMind。我已经完成了所有的代码,但是只有一个错误导致无法测试我的代码。
因此,使用调用的函数是静态的,方法本身也是静态的。我将给出一个代码示例,因为我不知道如何很好地描述这个问题。
public static void ProcesColors(Models.Rij_Master HuidigConfig, int lengte)
{
/* first get the control, so we can add changes to it */
ucRij try = (ucRij) FindChildInGrid(Res, 0, 0);
switch (lengte)
{
...;
}
}他给出了一个Res错误,Res是我的xaml页面上网格中的一个网格。我已经尝试了ref关键字(关于方法参数和方法中参数的定义)、out、静态的,但这些工作都没有。错误是:
非静态字段、方法或属性需要对象引用)。
所以我知道它需要一个引用,因为它是一个引用类型,但是我不知道如何强制它。
(顺便说一句,我的网格Res在一个定制的控件中)
我在网格中找到我的孩子的方法是:
private static FrameworkElement FindChildInGrid(Grid g, int row, int col)
{
var childs = g.Children.Cast<FrameworkElement>();
return childs.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == col).FirstOrDefault();
}我做了一些研究,有很多关于静态方法,变化,等等。但不知道该怎么称呼他们。
我希望我的问题是清楚的,这不是什么愚蠢的事情。很抱歉发了这么长的邮件,我已经感谢你刚刚读到了,
欢迎光临!
(删除了一些东西)
编辑::
最好先读到下面,我会问一个定义性的问题,如果这个问题被回答了,那么我就不需要所有其他的了
所以..。从哪里开始..。我有一个类,MasterMind,和一个类MasterMind_Row。我还构建了一些自定义控件(所有这些控件都嵌套在一起)。
主要思想是,如果用户dubbel点击图例中的颜色元素(ucLegend控件),它就必须更改试用行中的第一个可用颜色。所以我有两件事要处理,在我的类MasterMind中,我必须将CurrentConfig MasterMind_Row更改为它的值,这样类就可以检查正确的颜色,并且我必须在自定义控件中更改颜色本身。
您为ProcesColors看到的代码在我的控制ucGame中。在那里,它选择在网格中正确位置上的行控件,并从4个边框中更改它的背景。我通过在RowClass中创建4个公共属性实现了这一点,在设置器中,它改变了边框的背景。
在我的代码中,这都是一些指导,这样您就可以理解我想要实现的目标了。
现在,我将给出如何实现它的代码路径:(在ucLegend中,实现双击):
private void ProcesColor(object sender, DoubleTappedRoutedEventArgs e)
{
Border brd = (Border) sender;
Brush selectColor = brd.Background;
MasterMind.FillColorsByClick(selectColor);
}然后我们转到MasterMind类,在那里处理单击。这必须是静态的吗?或者把它排除在外(它可以解决所有问题)
public **static** void FillColorsByClick(Brush selectColor)
{
if (arrSelectionChoice.Length < 4)
{
/* resize array */
ExpandArray1Pos(arrSelectionChoice);
/* fill in the array */
arrSelectonChoice[arrSelectonChoice.Length - 1] = selectColor;
/* fill the color in on the right property (color1,color2,color3,color4)
in the MasterMind_Row CurrentConfig */
FillElementsInRowObject();
/* send the currentconfig Row (with it's colors) to the ucLegend control*/
ucSpel.ProcesColors(HuidigConfig, arrSelectieKeuze.Length);
}
}现在出现了我之前发布的代码。在控件ucGame中,我们有以下内容:
public **static** void ProcesColors(Models.Rij_Master HuidigConfig, int lengte)
{
/* first select rowcontrol to make changes to it */
ucRij tryout = (ucRij) FindChildInGrid(ref Res, 0, 0);
switch (lengte)
{
case 1:
tryout.Color1SetBackground = HuidigConfig.Color1;
case 2:
tryout.Color1SetBackground = HuidigConfig.Color1;
tryout.Color2SetBackground = HuidigConfig.Color2;
case 3:
tryout.Color1SetBackground = HuidigConfig.Color1;
tryout.Color2SetBackground = HuidigConfig.Color2;
tryout.Color3SetBackground = HuidigConfig.Color3;
case 4:
tryout.Color1SetBackground = HuidigConfig.Color1;
tryout.Color2SetBackground = HuidigConfig.Color2;
tryout.Color3SetBackground = HuidigConfig.Color3;
tryout.Color4SetBackground = HuidigConfig.Color4;
}
}现在的主要问题是什么,尽管我的代码也再看一遍,它们是否都有静态属性?我知道一旦你使1静态,你必须使整个链为静态,但它是否有可能这样做没有静态呢?
用于选择元素的网格也在我的ucGame控件中。所以我不能把它从班上或者其他地方传出去。
我知道这是一个很长的帖子,我真的很感谢那些阅读这篇文章的人。我仍然是一个学习程序员,但我不想变得更好,专业,因此,这个项目,我已经弥补了自己。
我已经在堆叠溢出问题上找到了很多答案,我自己也开始成为活跃的成员。我真的很喜欢这个网站。
再次为这篇长篇文章道歉,我希望这不是一个愚蠢的问题。
我真诚的
发布于 2015-04-17 16:05:51
我假设ProcesColors是您添加到窗口代码背后的一个函数。
正如您已经提到的,问题是您的ProcesColors函数是static,而Res是页面上Grid的一个实例。因此,ProcesColors不知道Res是什么。如果ProcesColors真的应该保持static,那么您需要从任何调用ProcesColors的代码中将Res传递给它。或者,也许ProcesColors并不是真正的static。如果您为调用ProcesColors的函数发布代码,我们可能会进一步帮助您。
https://stackoverflow.com/questions/29704018
复制相似问题