非静态字段、方法或属性“TingTong.MainWindow.animategrid(string, string,
本文关键字:string TingTong MainWindow animategrid 属性 字段 静态 方法 | 更新日期: 2023-09-27 18:32:17
我有两个类,如下所示
AnimateUtils 类:
namespace TingTong.View
{
public class AnimateUtils
{
public static void animategrid(string motion, ref DoubleAnimation slide, ref Grid grid, ref Grid grid2, ref Storyboard sbfade, ref Storyboard sbslide)
{
if (motion == "away")
{
slide.To = 310;
slide.From = 0;
}
else
{
slide.To = 0;
slide.From = 310;
}
switch (grid.Name)
{
case "GD_Lockscreen":
slide.Duration = new Duration(TimeSpan.FromMilliseconds(400.0));
Storyboard.SetTarget(slide, grid);
Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(TranslateTransform.X)"));
sbfade.Children.Add(slide);
sbfade.Begin();
if (grid2.Name == "GD_Login")
{
slide.To = 0;
slide.From = 310;
slide.Duration = new Duration(TimeSpan.FromMilliseconds(400.0));
Storyboard.SetTarget(slide, grid2);
Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(TranslateTransform.X)"));
sbslide.Children.Add(slide);
sbslide.Begin();
}
break;
case "GD_Login":
slide.Duration = new Duration(TimeSpan.FromMilliseconds(400.0));
Storyboard.SetTarget(slide, grid);
Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(TranslateTransform.X)"));
sbfade.Children.Add(slide);
sbfade.Begin();
if (grid2.Name == "GD_Background")
{
slide.To = 310;
slide.From = 0;
slide.Duration = new Duration(TimeSpan.FromMilliseconds(800.0));
Storyboard.SetTarget(slide, grid2);
Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(TranslateTransform.X)"));
sbslide.Children.Add(slide);
sbslide.Begin();
}
break;
}
}
}
}
}
第二类是:
namespace TingTong
{
public partial class MainWindow : Window
{
static void xmppCon_OnLogin(object sender)
{
System.Windows.MessageBox.Show("Logged in to server");
animategrid("loginscreen", "away", "backgroundimg"); <------error is shown here
}
私人虚空动画网格(字符串网格,字符串运动,字符串与网格) { 开关(电网) { 案例"锁屏":
AnimateUtils.animategrid(motion, ref slide, ref GD_Lockscreen, ref GD_Login, ref sbfade, ref sbslide);
break;
case "loginscreen":
AnimateUtils.animategrid(motion, ref slide, ref GD_Login, ref GD_Background, ref sbfade, ref sbslide);
break;
}
}
}
}
但是,我收到以下错误:
Error:An object reference is required for the non-static field, method, or property 'TingTong.MainWindow.animategrid(string, string, string)' D:'TingTong'TingTong'MainWindow.xaml.cs 400 13 TingTong
包含以下引用:
using TingTong.View;
主窗口中的函数应如下所示:
public partial class MainWindow : Window
{
static void xmppCon_OnLogin(object sender)
{
System.Windows.MessageBox.Show("Logged in to server");
animategrid("loginscreen", "away", "backgroundimg");
}
private void animategrid(string grid, string motion, string withgrid)
{
switch (grid)
{
case "lockscreen":
AnimateUtils.animategrid(motion, ref slide, ref GD_Lockscreen, ref GD_Login, ref sbfade, ref sbslide);
break;
case "loginscreen":
AnimateUtils.animategrid(motion, ref slide, ref GD_Login, ref GD_Background, ref sbfade, ref sbslide);
break;
}
}
}
编辑:将包含更改为使用(我最近一直在编码到vb)
编辑2:添加了新功能。我以前错过了。
尝试像下面这样调用,因为animategrid(...)方法写在AnimateUtils类中。
AnimateUtils.animategrid("loginscreen", "away", "backgroundimg");
此外,您可能需要在顶部导入"TingTong.View"命名空间,如果尚未导入,如下所示:
using TingTong.View;
编辑
在仔细查看了第二个类,即 TingTong 命名空间中的 MainWindow 之后,您必须将 animategrid() 方法签名更改为:
private static void animategrid(string grid, string motion, string withgrid)
或
您必须删除事件处理程序前面的"static"关键字:xmppCon_OnLogin(对象发送器)
不能从静态方法调用非静态/实例方法。