如何声明在 xaml 中声明的静态属性/名称?或者
时,我收到此错误
Error 1 An object reference is required for the non-static field, method, or property 'SilverlightForum.MainPage.stackPanelAssetsControl' C:\Users\UserName\Documents\Visual Studio 2010\Projects\SilverlightForum\SilverlightForum\MainPage.xaml.cs 23 13 SilverlightForum
当在 MainPage.xaml.cs 中使用此 xaml
<StackPanel x:Name="stackPanelAssetsControl" Orientation="Vertical" Grid.Row="3" Grid.ColumnSpan="2">
</StackPanel>
和以下代码(代码隐藏)
public static void AttachBoardControl()
{
stackPanelAssetsControl.Children.Clear();
stackPanelAssetsControl.Children.Add(SilverlightForum.App.forumBoardControl);
}
有人可以帮我处理这个问题吗?如何声明在 xaml 中声明的静态属性/名称?或者有什么办法可以解决这个错误?
谢谢大家的帮助!!!
I get this error,
Error 1 An object reference is required for the non-static field, method, or property 'SilverlightForum.MainPage.stackPanelAssetsControl' C:\Users\UserName\Documents\Visual Studio 2010\Projects\SilverlightForum\SilverlightForum\MainPage.xaml.cs 23 13 SilverlightForum
when using this xaml
<StackPanel x:Name="stackPanelAssetsControl" Orientation="Vertical" Grid.Row="3" Grid.ColumnSpan="2">
</StackPanel>
and the following code in MainPage.xaml.cs (code behind)
public static void AttachBoardControl()
{
stackPanelAssetsControl.Children.Clear();
stackPanelAssetsControl.Children.Add(SilverlightForum.App.forumBoardControl);
}
Can someone help me deal with this problem? how do i declare a static property/name declared in xaml? or is there a work around where i can deal with this error?
Thank you for all the help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在静态函数中使用实例级对象,
请在此处阅读有关 静态
You cant use instance level objects in static functions
Read here about static
为什么
AttachBoardControl
声明为static
?这会导致错误,因为您需要一个对象来访问 stackPanelAssetsControl 。
因此,要么删除
static
,要么您需要:Why is
AttachBoardControl
declared asstatic
?This is causing the error as you need an object to access
stackPanelAssetsControl
.So either remove the
static
or you'll need: