Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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# 从WPF应用程序中的文本框获取用户输入_C#_Wpf_Textbox_User Input - Fatal编程技术网

C# 从WPF应用程序中的文本框获取用户输入

C# 从WPF应用程序中的文本框获取用户输入,c#,wpf,textbox,user-input,C#,Wpf,Textbox,User Input,我试图从正在构建的WPF应用程序的文本框中获取用户输入。用户将输入一个数值,我希望将其存储在变量中。我刚开始学习C。我怎么做 目前,我正在打开文本框,让用户输入值。之后,用户必须按下一个按钮,文本框中的文本将存储在一个变量中 private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { } private void Button_Click(object sender, RoutedEventArgs e)

我试图从正在构建的WPF应用程序的文本框中获取用户输入。用户将输入一个数值,我希望将其存储在变量中。我刚开始学习C。我怎么做

目前,我正在打开文本框,让用户输入值。之后,用户必须按下一个按钮,文本框中的文本将存储在一个变量中

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    var h = text1.Text;
}

我知道这不对。正确的方法是什么?

就像@Michael McMullin已经说过的,您需要在函数外部定义变量,如下所示:

string str;

private void Button_Click(object sender, RoutedEventArgs e)
{
    str = text1.Text;
}

// somewhere ...
DoSomething(str);
<Window x:Class="StackoverflowHelpWPF5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:[YOURNAMESPACEHERE]"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:SimpleViewModel></local:SimpleViewModel>
    </Window.DataContext>
    <Window.Resources>
        <local:SimpleConverter x:Key="myConverter"></local:SimpleConverter>
    </Window.Resources>
    <Grid>
        <TextBox Text="{Binding MyValue, Converter={StaticResource myConverter}, UpdateSourceTrigger=PropertyChanged}"></TextBox>
    </Grid>
</Window>

要点是:变量的可见性取决于其范围。请看一看。

好吧,下面是一个如何使用MVVM实现这一点的简单示例

首先编写一个视图模型:

public class SimpleViewModel : INotifyPropertyChanged
{
    private int myValue = 0;

    public int MyValue
    {
        get
        {
            return this.myValue;
        }
        set
        {
            this.myValue = value;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
然后编写一个转换器,以便将字符串转换为int,反之亦然:

[ValueConversion( typeof(int), typeof(string))]
class SimpleConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int returnedValue;

        if (int.TryParse((string)value, out returnedValue))
        {
            return returnedValue;
        }

        throw new Exception("The text is not a number");
    }
}
然后像这样编写XAML代码:

string str;

private void Button_Click(object sender, RoutedEventArgs e)
{
    str = text1.Text;
}

// somewhere ...
DoSomething(str);
<Window x:Class="StackoverflowHelpWPF5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:[YOURNAMESPACEHERE]"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:SimpleViewModel></local:SimpleViewModel>
    </Window.DataContext>
    <Window.Resources>
        <local:SimpleConverter x:Key="myConverter"></local:SimpleConverter>
    </Window.Resources>
    <Grid>
        <TextBox Text="{Binding MyValue, Converter={StaticResource myConverter}, UpdateSourceTrigger=PropertyChanged}"></TextBox>
    </Grid>
</Window>

您也可以为控件命名:

<TextBox Height="251" ... Name="Content" />

是否尝试将输入转换为数字?@crossemup如果要在单击按钮后将要存储的值存储在变量中。你做得对。。。为什么你觉得这不正确?不,我不是想把输入转换成数字。输入本身就是一个数字。我想把这个数字存储到一个变量中,实际上,它是一个字符串。我是问您是否需要将其转换为数字,因为您的代码已经正确地存储了它。如果您需要在Button\u Click事件处理程序之外访问它,那么您只需要在该范围之外定义变量。如果您想在Button Click事件上存储文本框值,那么您就可以直接访问了。您也可以使用绑定,但由于您是c#的新手,这对您来说会很复杂……因为在事件处理程序中,局部变量超出了。。。[至少完成答案,让OP知道当前方法不起作用的原因]…
var h将不起作用。只能在方法范围内使用。@Olivier Jacot Descombes如果您是对的,我已经编辑了.str=text1.Text;我在哪里可以访问文本1?@agDev这是您在XAMLI中的文本框的名称。我想说的很简单:当您按下按钮时,它会显示一个消息框,告诉您文本框中的文本。我需要做所有这些仅仅是为了在文本框中获取文本吗?我不能直接访问文本框并调用getText()或其他什么吗?哇,这是一个非常平凡的代码。人们想知道我为什么讨厌MVVM/MVC。。。。不过,演讲很棒。