using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using RedBookPlayer.GUI.ViewModels;
namespace RedBookPlayer.GUI.Views
{
public class PlayerView : UserControl
{
///
/// Read-only access to the view model
///
public PlayerViewModel PlayerViewModel => DataContext as PlayerViewModel;
///
/// Initialize the UI based on the default theme
///
public PlayerView() : this(null, null) { }
///
/// Initialize the UI based on the default theme with an existing view model
///
/// XAML data representing the theme, null for default
/// Existing PlayerViewModel to load in instead of creating a new one
public PlayerView(PlayerViewModel playerViewModel) : this(null, playerViewModel) { }
///
/// Initialize the UI based on the currently selected theme
///
/// XAML data representing the theme, null for default
/// Existing PlayerViewModel to load in instead of creating a new one
public PlayerView(string xaml, PlayerViewModel playerViewModel)
{
LoadTheme(xaml);
if(playerViewModel != null)
DataContext = playerViewModel;
else
DataContext = new PlayerViewModel();
}
#region Helpers
///
/// Update the view model with new settings
///
public void UpdateViewModel()
{
PlayerViewModel.SetLoadDataTracks(App.Settings.PlayDataTracks);
PlayerViewModel.SetLoadHiddenTracks(App.Settings.PlayHiddenTracks);
}
///
/// Load the theme from a XAML, if possible
///
/// XAML data representing the theme, null for default
private void LoadTheme(string xaml)
{
try
{
if(xaml != null)
new AvaloniaXamlLoader().Load(xaml, null, this);
else
AvaloniaXamlLoader.Load(this);
}
catch
{
AvaloniaXamlLoader.Load(this);
}
}
#endregion
}
}