mirror of
https://github.com/aaru-dps/RedBookPlayer.git
synced 2025-12-16 19:24:41 +00:00
Persist view model between theme swaps
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Xml;
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Input;
|
using Avalonia.Input;
|
||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
|
using RedBookPlayer.Common;
|
||||||
|
|
||||||
namespace RedBookPlayer.GUI
|
namespace RedBookPlayer.GUI
|
||||||
{
|
{
|
||||||
@@ -30,10 +31,13 @@ namespace RedBookPlayer.GUI
|
|||||||
if(string.IsNullOrWhiteSpace(theme))
|
if(string.IsNullOrWhiteSpace(theme))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// If we already have a view, cache the view model
|
||||||
|
PlayerViewModel pvm = ((PlayerView)Instance.ContentControl.Content).PlayerViewModel;
|
||||||
|
|
||||||
// If the theme name is "default", we assume the internal theme is used
|
// If the theme name is "default", we assume the internal theme is used
|
||||||
if(theme.Equals("default", StringComparison.CurrentCultureIgnoreCase))
|
if(theme.Equals("default", StringComparison.CurrentCultureIgnoreCase))
|
||||||
{
|
{
|
||||||
Instance.ContentControl.Content = new PlayerView();
|
Instance.ContentControl.Content = new PlayerView(pvm);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -50,12 +54,12 @@ namespace RedBookPlayer.GUI
|
|||||||
{
|
{
|
||||||
string xaml = File.ReadAllText(xamlPath);
|
string xaml = File.ReadAllText(xamlPath);
|
||||||
xaml = xaml.Replace("Source=\"", $"Source=\"file://{themeDirectory}/");
|
xaml = xaml.Replace("Source=\"", $"Source=\"file://{themeDirectory}/");
|
||||||
Instance.ContentControl.Content = new PlayerView(xaml);
|
Instance.ContentControl.Content = new PlayerView(xaml, pvm);
|
||||||
}
|
}
|
||||||
catch(XmlException ex)
|
catch(XmlException ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default");
|
Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default");
|
||||||
Instance.ContentControl.Content = new PlayerView();
|
Instance.ContentControl.Content = new PlayerView(pvm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,15 +30,27 @@ namespace RedBookPlayer.GUI
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initialize the UI based on the default theme
|
/// Initialize the UI based on the default theme
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public PlayerView() : this(null) { }
|
public PlayerView() : this(null, null) { }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize the UI based on the default theme with an existing view model
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="xaml">XAML data representing the theme, null for default</param>
|
||||||
|
/// <param name="playerViewModel">Existing PlayerViewModel to load in instead of creating a new one</param>
|
||||||
|
public PlayerView(PlayerViewModel playerViewModel) : this(null, playerViewModel) { }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initialize the UI based on the currently selected theme
|
/// Initialize the UI based on the currently selected theme
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="xaml">XAML data representing the theme, null for default</param>
|
/// <param name="xaml">XAML data representing the theme, null for default</param>
|
||||||
public PlayerView(string xaml)
|
/// <param name="playerViewModel">Existing PlayerViewModel to load in instead of creating a new one</param>
|
||||||
|
public PlayerView(string xaml, PlayerViewModel playerViewModel)
|
||||||
{
|
{
|
||||||
DataContext = new PlayerViewModel();
|
if(playerViewModel != null)
|
||||||
|
DataContext = playerViewModel;
|
||||||
|
else
|
||||||
|
DataContext = new PlayerViewModel();
|
||||||
|
|
||||||
PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged;
|
PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged;
|
||||||
|
|
||||||
LoadTheme(xaml);
|
LoadTheme(xaml);
|
||||||
|
|||||||
Reference in New Issue
Block a user