gpt4 book ai didi

c# - 获取选择的 Item 的父 ObservableCollection - WP8

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:21 25 4
gpt4 key购买 nike

我正在编写一个播放流媒体音乐的应用程序,在应用程序中我有很多列表(排名列表、搜索结果列表、精选歌曲列表......),每个列表都有一个相同的数据模板,我将其绑定(bind)到 LongListSelector对于每个页面。所以我将这个数据模板用作资源并将其放在 app.xaml 中

<DataTemplate x:Key="BasicVideoTemplate">
<Grid Tap="ChangeSong_Tap" RowsAuto="50,50" ColumnsAuto="150,*" Background="White" Margin="5,0,5,10">
<Grid.ColumnDefinition>
<ColumnDefinition Width = "150"/>
<ColumnDefinition Width = "*"/>
</Grid.ColumnDefinition>
<Grid.RowDefinition>
<RowDefinition Height = "50"/>
<RowDefinition Height = "50"/>
</Grid.RowDefinition>
<Border BorderThickness="1" BorderBrush="Black" Grid.RowSpan="2" Grid.Column="0" VerticalAlignment="Center" Margin="5,0,5,0">
<Image Source="{Binding Cover}"/>
</Border>
<TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1" Style="{StaticResource BlackTextBlock}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding Artist}" Grid.Row="1" Grid.Column="1" Foreground="Black" Margin="5,0,0,0"/>
<!-- .............. -->
</Grid>
</DataTemplate>

和这段代码(我放在 app.xaml.cs 中)从列表中选择一首歌曲,从这个项目创建一个 AudioTrack 并导航到 playSongPage:

private void ChangeSong_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var item = (SongItemModel)(sender as FrameworkElement).DataContext;
App.Model.ChangeSong(item.Id); /// this code will create a audio track for this item
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Pages/DetailSongPage.xaml", UriKind.Relative));
}

这里的问题是,我必须为我的播放列表创建一个 List ,那么我怎样才能获得点击项目的父列表并将其添加到 List ,而所有这些代码都放在 app. xaml.cs ???

最佳答案

我会在每个 longlistselectorSelectionChanged 事件中处理它。网格上的整个 Tap 东西不适合我。

<phone:LongListSelector x:Name="myLSS" SelectionChanged="myLSS_SelectionChanged"/>

// event handler changes to
private void myLSS_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector lls = sender as LongListSelector; // get lls
var item = (SongItemModel) lls.SelectedItem;
App.Model.ChangeSong(item.Id); /// this code will create a audio track for this item


// now your ObservableCollection is just the ItemsSource, save a reference to it
// in the State manager so you can reference it on another page if you wish
ObservableCollection<SongItemModel> obs = (ObservableCollection<SongItemModel>) lls.ItemsSource;
PhoneApplicationService.Current.State["current_obs"] = obs;

// navigate..............
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Pages/DetailSongPage.xaml", UriKind.Relative));
}

关于c# - 获取选择的 Item 的父 ObservableCollection - WP8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25966912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com