簡體   English   中英

C#Wp8:獲取文本框輸入值時出錯?

[英]c# Wp8: error in getting textbox entered value?

它說createPasswordrepeatPassword不在當前上下文中。 為什么會發生什么?

碼;

public MainPage()
    {
        InitializeComponent();
        TextBox createPassword = new TextBox();
        createPassword.Width = 400;
        TextBox repeatPassword = new TextBox();
        repeatPassword.Width = 400;
        Button createButton = new Button();
        createButton.Content = "Create New Password";
        createButton.Click += new RoutedEventHandler(savePassword);
        StackPanel content = new StackPanel();
        content.HorizontalAlignment = HorizontalAlignment.Center;
        content.Children.Add(createPassword);
        content.Children.Add(repeatPassword);
        content.Children.Add(createButton);
        LayoutRoot.Children.Add(content);
        }
        void savePassword(object sender, RoutedEventArgs e)
        {
            string password1 = createPassword.Text;
            string password2 = repeatPassword.Text;
        }

createPasswordrepeatPassword必須是類成員才能在不同的類方法中使用它們:

TextBox createPassword;
TextBox repeatPassword;

public MainPage()
{
    InitializeComponent();
    createPassword = new TextBox();
    createPassword.Width = 400;
    repeatPassword = new TextBox();
    repeatPassword.Width = 400;
    Button createButton = new Button();
    createButton.Content = "Create New Password";
    createButton.Click += new RoutedEventHandler(savePassword);
    StackPanel content = new StackPanel();
    content.HorizontalAlignment = HorizontalAlignment.Center;
    content.Children.Add(createPassword);
    content.Children.Add(repeatPassword);
    content.Children.Add(createButton);
    LayoutRoot.Children.Add(content);
}
void savePassword(object sender, RoutedEventArgs e)
{
    string password1 = createPassword.Text;
    string password2 = repeatPassword.Text;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM