WPF 名称 '' 在当前上下文中不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19729211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF The name '' does not exist in the current context
提问by Xaphann
I am having an issue that I cannot figure out. Trying to call a function from a class in the same name space. Here is my class;
我遇到了一个我无法弄清楚的问题。尝试从同一名称空间中的类调用函数。这是我的课;
namespace MYNAMESPACE{
class myClass
{
public static string myFunction(string s)
{
//Function Logic
}
}}
Then in my windows;
然后在我的窗户里;
namespace MYNAMESPACE{
public partial class myWindow : Window
{
public myWindow()
{
string s1;
s1 = myFunction("My string");
InitializeComponent();
}
}}
The problem is calling the function gives "The name '' does not exist in the current context". Yet if I change the it to this it works;
问题是调用该函数给出“当前上下文中不存在名称''”。然而,如果我把它改成这样,它就起作用了;
s1 = MYNAMESPACE.myClass.myFunction("My string");
Why is it not in context?
为什么它不在上下文中?
回答by nvoigt
Thats because myClass and myWindow are not the same context. The current context of your line in question is MYNAMESPACE.myWindow and there is no function called myFunction in this context. So you need to spell out the whole context as you did.
那是因为 myClass 和 myWindow 不是同一个上下文。有问题的行的当前上下文是 MYNAMESPACE.myWindow 并且在此上下文中没有名为 myFunction 的函数。所以你需要像你一样拼出整个上下文。
回答by Raghulan Gowthaman
Check the .NET framework both the reference and the project is built on. Change it to latest .NET framework in the project properties, clean the solution and rebuild it.
检查参考和项目所基于的 .NET 框架。在项目属性中将其更改为最新的 .NET 框架,清理解决方案并重建它。
回答by Caleb George
This error can appear for several different reasons. It happened to me when I attempted to use a view inside a DataTemplate. For example:
出现此错误的原因可能有多种。当我尝试在 DataTemplate 中使用视图时,它发生在我身上。例如:
<Grid>
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<views:MyView x:Name="MyViewControl"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Grid>
WPF Rookie mistake; the code-behind will not recognize MyViewControlif you do that.
WPF菜鸟错误;如果您这样做,代码隐藏将无法识别MyViewControl。

