I created a ListView Page using the ListView template in Visual studio 2015. For some reason I can't access properties of my ui from my code behind despite the fact that i have given the ui elements names with "x:Name". This is my ListView declared in Xaml:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Style="{DynamicResource ListItemTextStyle}" Text="Central" />
<Label
x:Name="editField"
Style="{DynamicResource ListItemDetailTextStyle}"
Text="{Binding EmergencyCentralNumber}" />
<Entry
x:Name="entryField"
Completed="Entry_Completed"
IsVisble="false"
Text="{Binding EmergencyCentralNumber}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is my codebehind:
async void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item == null)
return;
await DisplayAlert("Item Tapped", "An item was tapped.", "OK");
var selectedNumber = (AppUser)e.Item;
//Deselect Item
entryField.IsVisble = true;
editField.IsVisble = false;
//((ListView)sender).SelectedItem = null;
//Deselect Item
//((ListView)sender).SelectedItem = null;
}
async void Entry_Completed(object sender, EventArgs e)
{
var text = ((Entry)sender).Text;
if (string.IsNullOrEmpty(text))
{
await DisplayAlert("Alert", "Please do not leave this field empty", "OK");
}
await Task.Run(() =>
{
var realm = Realm.GetInstance();
var user = realm.All<AppUser>().First();
//cast sender to access the properties of the Entry
realm.Write(() => user.EmergencyCentralNumber = ((Entry)sender).Text);
});
editField.IsVisble = false;
entryField.IsVisble = true;
}
}
Why am i being told that the editField and entryField variable does not exist in the current context?
答案 0 :(得分:0)
I might be wrong, but names are only accessible inside DataTemplate scope for referencing, otherwise if you put an event handler on element outside your ListView and reference your item by name, how your code should know which Item from ListView specifically you are referencing? Cause they would all have the same name in that context.