Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UiApplication to fix Application.Current null #851

Merged
merged 11 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ First, your application needs to load custom styles, add in the **MyApp\App.xaml
</Application>
```

If your application does not have **MyApp\App.xaml** file, use `ApplicationThemeManager.Apply(frameworkElement)` to apply/update the theme resource in the `frameworkElement`.

```C#
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
ApplicationThemeManager.Apply(this);
}
}
```

Now you can create fantastic apps, e.g. with one button:

```xml
Expand Down
20 changes: 19 additions & 1 deletion Wpf.Ui.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wpf.Ui.ToastNotifications",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wpf.Ui.Tray", "src\Wpf.Ui.Tray\Wpf.Ui.Tray.csproj", "{073BF126-377B-49CD-838A-E8B779EB4862}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wpf.Ui.SyntaxHighlight", "src\Wpf.Ui.SyntaxHighlight\Wpf.Ui.SyntaxHighlight.csproj", "{07F7A65A-6061-4606-ACA2-F8D1A3D0E19A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wpf.Ui.SyntaxHighlight", "src\Wpf.Ui.SyntaxHighlight\Wpf.Ui.SyntaxHighlight.csproj", "{07F7A65A-6061-4606-ACA2-F8D1A3D0E19A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wpf.Ui.Demo.Console", "src\Wpf.Ui.Demo.Console\Wpf.Ui.Demo.Console.csproj", "{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -298,6 +300,22 @@ Global
{07F7A65A-6061-4606-ACA2-F8D1A3D0E19A}.Release|x64.Build.0 = Release|Any CPU
{07F7A65A-6061-4606-ACA2-F8D1A3D0E19A}.Release|x86.ActiveCfg = Release|Any CPU
{07F7A65A-6061-4606-ACA2-F8D1A3D0E19A}.Release|x86.Build.0 = Release|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Debug|arm64.ActiveCfg = Debug|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Debug|arm64.Build.0 = Debug|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Debug|x64.ActiveCfg = Debug|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Debug|x64.Build.0 = Debug|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Debug|x86.ActiveCfg = Debug|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Debug|x86.Build.0 = Debug|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Release|Any CPU.Build.0 = Release|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Release|arm64.ActiveCfg = Release|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Release|arm64.Build.0 = Release|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Release|x64.ActiveCfg = Release|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Release|x64.Build.0 = Release|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Release|x86.ActiveCfg = Release|Any CPU
{1FB6D1C2-B2E7-48F5-B5BA-9F0CA4F1B826}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 13 additions & 0 deletions src/Wpf.Ui.Demo.Console/Models/DataColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Windows.Media;

namespace Wpf.Ui.Demo.Console.Models;

public struct DataColor
{
public Brush Color { get; set; }
}
30 changes: 30 additions & 0 deletions src/Wpf.Ui.Demo.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System;
using System.Threading;
using System.Windows;

public static class Program
{
[STAThread]
public static void Main(string[] args)
{
if (Application.Current is null)
Console.WriteLine($"Application.Current is null.");

try
{
new Wpf.Ui.Demo.Console.Views.SimpleView().ShowDialog();
new Wpf.Ui.Demo.Console.Views.MainView().ShowDialog();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Thread.Sleep(10000);
throw;
}
}
}
122 changes: 122 additions & 0 deletions src/Wpf.Ui.Demo.Console/ThemeUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Wpf.Ui;
using Wpf.Ui.Appearance;

public static class ThemeUtils
{
public static void ApplyTheme(this FrameworkElement frameworkElement)
{
ApplicationThemeManager.Apply(frameworkElement);

ThemeChangedEvent themeChanged = (sender, args) =>
{
ApplicationThemeManager.Apply(frameworkElement);
if (frameworkElement is Window window)
{
if (window != UiApplication.Current.MainWindow)
WindowBackgroundManager.UpdateBackground(window, sender, Wpf.Ui.Controls.WindowBackdropType.None, true);
}
};

if (frameworkElement.IsLoaded)
{
ApplicationThemeManager.Changed += themeChanged;
}
frameworkElement.Loaded += (s, e) =>
{
ApplicationThemeManager.Changed += themeChanged;
};
frameworkElement.Unloaded += (s, e) =>
{
ApplicationThemeManager.Changed -= themeChanged;
};

#if DEBUG
if (frameworkElement is Window window)
{
window.KeyDown += (s, e) =>
{
if (e.Key == System.Windows.Input.Key.T)
{
ChangeTheme();
}

if (e.Key == System.Windows.Input.Key.C)
{
var rnd = new Random();
var randomColor = Color.FromRgb((byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256));

ApplicationAccentColorManager.Apply(randomColor, ApplicationThemeManager.GetAppTheme());

var current = ApplicationThemeManager.GetAppTheme();
var applicationTheme = ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Light
? ApplicationTheme.Dark
: ApplicationTheme.Light;

ApplicationThemeManager.Apply(applicationTheme, updateAccent: false);
ApplicationThemeManager.Apply(current, updateAccent: false);
}
};
}
#endif
}

public static void ChangeTheme()
{
var applicationTheme = ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Light
? ApplicationTheme.Dark
: ApplicationTheme.Light;

ApplicationThemeManager.Apply(applicationTheme, updateAccent: false);
}

/// <summary>
/// Applies Resources in the <paramref name="frameworkElement"/>.
/// </summary>
private static void Apply(FrameworkElement frameworkElement)
{
if (frameworkElement is null)
return;

var resourcesRemove = frameworkElement.Resources.MergedDictionaries
.Where(e => e.Source is not null)
//.Where(e => e.Source.ToString().ToLower().Contains(Wpf.Ui.Appearance.ApplicationThemeManager.LibraryNamespace))
.Where(e => e.Source.ToString().ToLower().Contains("Wpf.Ui;"))
.ToArray();

foreach (var resource in resourcesRemove)
{
//System.Console.WriteLine(
// $"INFO | {typeof(MainView)} Remove {resource.Source}",
// "Wpf.Ui.Appearance"
//);
frameworkElement.Resources.MergedDictionaries.Remove(resource);
}

foreach (var resource in UiApplication.Current.Resources.MergedDictionaries)
{
//System.Console.WriteLine(
// $"INFO | {typeof(MainView)} Add {resource.Source}",
// "Wpf.Ui.Appearance"
//);
frameworkElement.Resources.MergedDictionaries.Add(resource);
}

foreach (System.Collections.DictionaryEntry resource in UiApplication.Current.Resources)
{
//System.Console.WriteLine(
// $"INFO | {typeof(MainView)} Copy Resource {resource.Key} - {resource.Value}",
// "Wpf.Ui.Appearance"
//);
frameworkElement.Resources[resource.Key] = resource.Value;
}
}
}
76 changes: 76 additions & 0 deletions src/Wpf.Ui.Demo.Console/Views/MainView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<ui:FluentWindow x:Class="Wpf.Ui.Demo.Console.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf.Ui.Demo.Console.Views"
xmlns:pages="clr-namespace:Wpf.Ui.Demo.Console.Views.Pages"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Title="WPF UI - Console Demo"
Width="1200"
Height="654"
d:DesignHeight="650"
d:DesignWidth="900"
ExtendsContentIntoTitleBar="True"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">

<ui:FluentWindow.InputBindings>
<KeyBinding Key="F"
Command="{Binding ElementName=AutoSuggestBox, Path=FocusCommand}"
Modifiers="Control" />
</ui:FluentWindow.InputBindings>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ui:NavigationView x:Name="RootNavigation"
Grid.Row="1">
<ui:NavigationView.AutoSuggestBox>
<ui:AutoSuggestBox x:Name="AutoSuggestBox"
PlaceholderText="Search">
<ui:AutoSuggestBox.Icon>
<ui:IconSourceElement>
<ui:SymbolIconSource Symbol="Search24" />
</ui:IconSourceElement>
</ui:AutoSuggestBox.Icon>
</ui:AutoSuggestBox>
</ui:NavigationView.AutoSuggestBox>
<ui:NavigationView.Header>
<ui:BreadcrumbBar Margin="42,32,0,0"
FontSize="28"
FontWeight="DemiBold" />
</ui:NavigationView.Header>
<ui:NavigationView.MenuItems>
<ui:NavigationViewItem Content="Dashboard"
NavigationCacheMode="Enabled"
TargetPageType="{x:Type pages:DashboardPage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="Home24" />
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
<ui:NavigationViewItem Content="Data"
NavigationCacheMode="Enabled"
TargetPageType="{x:Type pages:DataPage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="DataHistogram24" />
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
</ui:NavigationView.MenuItems>
<ui:NavigationView.FooterMenuItems>
<ui:NavigationViewItem Content="Settings"
NavigationCacheMode="Enabled"
TargetPageType="{x:Type pages:SettingsPage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="Settings24" />
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
</ui:NavigationView.FooterMenuItems>
</ui:NavigationView>

<ui:TitleBar Title="WPF UI - Console Demo"
Grid.Row="0"/>
</Grid>
</ui:FluentWindow>
27 changes: 27 additions & 0 deletions src/Wpf.Ui.Demo.Console/Views/MainView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Wpf.Ui.Demo.Console.Views.Pages;

namespace Wpf.Ui.Demo.Console.Views;
public partial class MainView
{
public MainView()
{
DataContext = this;

InitializeComponent();

Loaded += (_, _) => RootNavigation.Navigate(typeof(DashboardPage));

UiApplication.Current.MainWindow = this;

this.ApplyTheme();
}
}
35 changes: 35 additions & 0 deletions src/Wpf.Ui.Demo.Console/Views/Pages/DashboardPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Page
x:Class="Wpf.Ui.Demo.Console.Views.Pages.DashboardPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Wpf.Ui.Demo.Console.Views.Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Title="DashboardPage"
d:DesignHeight="450"
d:DesignWidth="800"
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
ScrollViewer.CanContentScroll="False"
mc:Ignorable="d">

<Grid Margin="42" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<ui:Button
Grid.Column="0"
Click="OnBaseButtonClick"
Content="Click me!"
Icon="{ui:SymbolIcon Fluent24}" />
<TextBlock
x:Name="CounterTextBlock"
Grid.Column="1"
Margin="12,0,0,0"
VerticalAlignment="Center" />
</Grid>
</Page>
Loading
Loading