Skip to content

Commit

Permalink
Use ResourceDictionary to Manage Localizable String Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ousiax committed Dec 11, 2023
1 parent 4e39717 commit 56740c9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Leo.Wpf.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,23 @@ protected override async void OnStartup(StartupEventArgs e)
scope.ServiceProvider.GetRequiredService<IOptions<WebOptions>>().Value.BaseAddress = new Uri(address);
}

LoadApplicationResources();

var mainWin = _host.Services.GetRequiredService<IMainWindowService>();
mainWin.Show();

base.OnStartup(e);
}

private static void LoadApplicationResources()
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("Resources/Localization.xaml", UriKind.RelativeOrAbsolute)
});
}

protected override async void OnExit(ExitEventArgs e)
{
using (_host)
Expand Down
5 changes: 5 additions & 0 deletions src/Leo.Wpf.App/Leo.Wpf.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<ApplicationIcon>leon.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
<Page Remove="Resources\MainWindow.xaml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
Expand All @@ -23,6 +27,7 @@
<Resource Include="leon.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Resources\MainWindow.xaml" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/Leo.Wpf.App/Resources/Localization.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainWindow.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
4 changes: 4 additions & 0 deletions src/Leo.Wpf.App/Resources/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib">
<System:String x:Key="MainWindow.Title">Customer Management System</System:String>
</ResourceDictionary>
2 changes: 1 addition & 1 deletion src/Leo.Wpf.App/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:viewModel="clr-namespace:Leo.Wpf.App.ViewModels"
mc:Ignorable="d"
Icon="../leon.ico"
Title="客户管理系统" Height="450" Width="800"
Title="{StaticResource MainWindow.Title}" Height="450" Width="800"
d:DataContext="{d:DesignInstance viewModel:MainWindowViewModel}">
<Window.Resources>
<converter:BooleanInverseConverter x:Key="BooleanInverseConverter" />
Expand Down

0 comments on commit 56740c9

Please sign in to comment.