using System.Threading.Tasks; using StrixMusic.Sdk.ViewModels; using StrixMusic.Sdk.WinUI.Controls.Collections.Abstract; using StrixMusic.Sdk.WinUI.Controls.Items; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace StrixMusic.Sdk.WinUI.Controls.Collections { /// /// A Templated for displaying an . /// public partial class TrackCollection : CollectionControl { /// /// Initializes a new instance of the class. /// public TrackCollection() { DefaultStyleKey = typeof(TrackCollection); } /// /// Backing dependency property for . /// public ITrackCollectionViewModel Collection { get { return (ITrackCollectionViewModel)GetValue(CollectionProperty); } set { SetValue(CollectionProperty, value); } } /// /// Dependency property for . /// public static readonly DependencyProperty CollectionProperty = DependencyProperty.Register(nameof(Collection), typeof(ITrackCollectionViewModel), typeof(TrackCollection), new PropertyMetadata(0)); /// protected override void OnApplyTemplate() { // OnApplyTemplate is often a more appropriate point to deal with // adjustments to the template-created visual tree than is the Loaded event. // The Loaded event might occur before the template is applied, // and the visual tree might be incomplete as of Loaded. base.OnApplyTemplate(); AttachHandlers(); } /// protected override async Task LoadMore() { if (Collection == null) return; if (!Collection.PopulateMoreTracksCommand.IsRunning) await Collection.PopulateMoreTracksCommand.ExecuteAsync(25); } /// protected override void CheckAndToggleEmpty() { if (Collection == null) return; if (!Collection.PopulateMoreTracksCommand.IsRunning && Collection.TotalTrackCount == 0) SetEmptyVisibility(Visibility.Visible); } private void AttachHandlers() { Unloaded += TrackCollection_Unloaded; } private void DetachHandlers() { Unloaded -= TrackCollection_Unloaded; } private void TrackCollection_Unloaded(object sender, RoutedEventArgs e) { DetachHandlers(); } } }