我无法在ListView
MainPage.xaml中
<ListView
ReorderMode="Disabled"
SelectionMode="Single"
IsItemClickEnabled="True"
ItemClick="Section_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListViewItem Margin="0,0,0,10" Background="LightGray">
<StackPanel Orientation="Horizontal">
<Image Source="/Assets/Icons/1.png" Width="94" Height="94"/>
<TextBlock x:Uid="1" />
</StackPanel>
</ListViewItem>
...
</ListView>
MainPage.xaml.cs中
private async void Section_ItemClick(object sender, ItemClickEventArgs e)
{
ListViewItem itemId = ((sender as ListView).SelectedItem as ListViewItem);
if (itemId != null)
{
.. do something ..
}
else
{
throw new Exception(this.resourceLoader.GetString("NavigationFailedExceptionMessage"));
}
}
编辑**
在进一步测试中,我发现事件触发时sender
和e
都为空?
答案 0 :(得分:6)
好的,这解决了(最后)。使用SelectionChanged事件,并通过x:Name
MainPage.xaml中
<ListView
x:Name="SettingsHub"
ReorderMode="Disabled"
SelectionChanged="SettingsHub_SelectionChanged"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
...
</ListView>
MainPage.xaml.cs中
private void SettingsHub_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
var itemId = (e.AddedItems[0] as ListViewItem).Name;
}
}
答案 1 :(得分:0)
先展开ClickedItem
。下列之一应该可以工作(我不确定哪一个会起作用,因为我现在没有安装Windows Phone SDK):
var itemId = (e.ClickedItem as ListViewItem);
var itemId = (e.ClickedItem as Item);
然后检查null
参考:
if (itemId != null && itemId.IsSelected)
{ }
的文档
答案 2 :(得分:0)
对于刚出现问题的listView。我在列表视图中存储rss feed urls,因此是方法名称。在代码中保持调试以提供帮助。
private void RSSItem_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(" CALLED ITEM CHANGED ");
//ListViewItem itemId = ((sender as ListView).SelectedItem as ListViewItem);
int count = e.AddedItems.Count;
string itemStr = e.AddedItems[0].ToString();
System.Diagnostics.Debug.WriteLine(" DOWNLOADING FEED URL : " + itemStr);
}
答案 3 :(得分:0)
private void DrivingHistoryListView_ItemClick(object sender, ItemClickEventArgs e)
{
var SelectedItem = (DrivingHistoryItem)e.ClickedItem;
.....
}
答案 4 :(得分:-1)
IDataReader ReadAll()
{
var connection = new SqlConnection(connectionString);
try
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM mytable";
var result = command.ExecuteReader(CommandBehavior.CloseConnection);
connection = null; // Don't dispose the connection.
return result;
}
finally
{
if (connection != null)
connection.Dispose();
}
}