Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c当前上下文中不存在名称“xy”_C# - Fatal编程技术网

C# c当前上下文中不存在名称“xy”

C# c当前上下文中不存在名称“xy”,c#,C#,我刚从C开始工作,我读了很多关于我在StackOverflow的错误的问题。不幸的是,我仍然不明白我做错了什么 所以,我遵循了这篇关于C和UI自动化的教程。在我不得不编写实际代码之前,一切都很顺利。所以我的代码是: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; usin

我刚从C开始工作,我读了很多关于我在StackOverflow的错误的问题。不幸的是,我仍然不明白我做错了什么

所以,我遵循了这篇关于C和UI自动化的教程。在我不得不编写实际代码之前,一切都很顺利。所以我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Automation;
using Automation = System.Windows.Automation;

namespace AutomateCSharp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    static AutomationElement desktopObject = AutomationElement.RootElement;

    static Automation.Condition testWindowNameCondition = new PropertyCondition(AutomationElement.NameProperty, "Demo Window For Csharp Automation");
    static Automation.Condition textConditionOne = new PropertyCondition(AutomationElement.AutomationIdProperty, "InputOne");
    static Automation.Condition textConditionTwo = new PropertyCondition(AutomationElement.AutomationIdProperty, "InputTwo");
    static Automation.Condition textConditionTotal = new PropertyCondition(AutomationElement.AutomationIdProperty, "Total");

    static AutomationElement testWindow = desktopObject.FindFirst(TreeScope.Children, testWindowNameCondition);

    static AutomationElement textOne = testWindow.FindFirst(TreeScope.Descendants, textConditionOne);
    static ValuePattern valuetextOne = textOne.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
    valuetextOne.SetValue("4");

    }
}
我得到了一个错误:“valuetextOne”名称在当前上下文中不存在


有人能告诉我我错在哪里吗?非常感谢:

这个问题与函数不能直接在类主体内部调用有关。只有字段、属性和方法可以写入其中。您需要做的是将其包装到另一个函数中,如下所示:

public partial class MainWindow : Window
{
    // Stuff
    public void SetValue() => valuetextOne.SetValue("4");
}
然后你称之为:

public void Foo()
{
    MainWindow window = Bar();
    window.SetValue();
}

我将对此进行更详细的介绍。

我建议您从以下几点开始。问题是不能将代码放在方法之外。另外,你的代码和你链接的教程不一样,所以…谢谢,我今天是瞎子,我想知道为什么它已经在一个方法之外工作了,但我认为这是C语言的魔力。