簡體   English   中英

在點擊事件中捕獲條目值

[英]Capture entry value in click event

我有一個使用c#添加到我的xamarin.forms應用程序的入口控件。 我也有一個工具欄項目,單擊它我想保存用戶輸入的數據。

我無法在點擊事件中訪問文本值,出現以下錯誤:

Error 20 The name 'txtTest' does not exist in the current context

這是我的代碼示例:

public SettingsPage()
{

ToolbarItem Settings = new ToolbarItem();
Settings.Name = "Settings";
Settings.Clicked += OnClick_Settings;
Settings.Order = ToolbarItemOrder.Primaru;

ToolbarItems.Add(Settings);
loadData();
}

async public void loadData()
{
    Label lblTest = new Label { Text = "Test", FontAttributes = FontAttributes.Bold };
    Entry txtTest = new Entry();

    StackLayout stLTest = new StackLayout
    {
        Padding = new Thickness(10, 0, 0, 0),
        Children ={ 
            lblTest,
            txtTest
        }
    };  
    Content = stTest
}

async private void OnClick_Settings(object sender, EventArgs e)
{
    var test= txtTest.Text;
}

在我的OnClick_Settings ,找不到文本值。

您在loadData()方法中具有txtTest 您應該將其移出那里。

做這個

Entry txtTest;    
async public void loadData()
{
    Label lblTest = new Label { Text = "Test", FontAttributes = FontAttributes.Bold };
    txtTest = new Entry();

    StackLayout stLTest = new StackLayout
    {
        Padding = new Thickness(10, 0, 0, 0),
        Children ={ 
            lblTest,
            txtTest
        }
    };  
    Content = stTest
}

暫無
暫無

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

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