Skip to content

Commit

Permalink
add a Sample Project to Use
Browse files Browse the repository at this point in the history
  • Loading branch information
robinhood3351 committed Jul 18, 2024
1 parent 3fbce60 commit 15125fe
Show file tree
Hide file tree
Showing 33 changed files with 1,144 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<PackageVersion Include="Microsoft.Extensions.Localization.Abstractions" Version="6.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="System.Text.Json" Version="6.0.0" />
<PackageVersion Include="PolySharp" Version="1.13.1" />
<PackageVersion Include="WPF-UI" Version="3.0.4" />
<PackageVersion Include="xunit.extensibility.core" Version="2.6.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageVersion Include="xunit" Version="2.6.2" />
Expand Down
8 changes: 7 additions & 1 deletion Lepo.i18n.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lepo.i18n.DependencyInjecti
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lepo.i18n.Wpf.UnitTests", "tests\Lepo.i18n.Wpf.UnitTests\Lepo.i18n.Wpf.UnitTests.csproj", "{ED677E81-8D0F-42B2-A001-D37FEB3AAEA2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lepo.i18n.Json.UnitTests", "tests\Lepo.i18n.Json.UnitTests\Lepo.i18n.Json.UnitTests.csproj", "{46BB2EDE-5A5C-46BC-930F-F1FB6128B4AE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lepo.i18n.Json.UnitTests", "tests\Lepo.i18n.Json.UnitTests\Lepo.i18n.Json.UnitTests.csproj", "{46BB2EDE-5A5C-46BC-930F-F1FB6128B4AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Galery", "src\Sample\Galery\Galery.csproj", "{9C513AB3-9C79-4D4B-BCDA-37621B2DAF1C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -70,6 +72,10 @@ Global
{46BB2EDE-5A5C-46BC-930F-F1FB6128B4AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46BB2EDE-5A5C-46BC-930F-F1FB6128B4AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46BB2EDE-5A5C-46BC-930F-F1FB6128B4AE}.Release|Any CPU.Build.0 = Release|Any CPU
{9C513AB3-9C79-4D4B-BCDA-37621B2DAF1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C513AB3-9C79-4D4B-BCDA-37621B2DAF1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C513AB3-9C79-4D4B-BCDA-37621B2DAF1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C513AB3-9C79-4D4B-BCDA-37621B2DAF1C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
17 changes: 17 additions & 0 deletions src/Sample/Galery/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Application x:Class="Galery.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
DispatcherUnhandledException="OnDispatcherUnhandledException"
Exit="OnExit"
Startup="OnStartup">
<Application.Resources>
<ResourceDictionary>
<FlowDirection x:Key="MainDirection">LeftToRight</FlowDirection>
<ResourceDictionary.MergedDictionaries>
<ui:ThemesDictionary Theme="Light" />
<ui:ControlsDictionary />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
112 changes: 112 additions & 0 deletions src/Sample/Galery/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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 Lepo.i18n Contributors.
// All Rights Reserved.

using Galery.Services;
using Galery.ViewModels.Pages;
using Galery.ViewModels.Windows;
using Galery.Views.Pages;
using Galery.Views.Windows;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Lepo.i18n.DependencyInjection;
using Lepo.i18n.Json;
using System.IO;
using System.Reflection;
using System.Windows.Threading;
using Wpf.Ui;
using System.Globalization;
using Newtonsoft.Json;
using Galery.Models;

namespace Galery;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
// The.NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
private static readonly IHost _host = Host
.CreateDefaultBuilder()
.ConfigureAppConfiguration(c => { c.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)); })
.ConfigureServices((context, services) =>
{
_ = services.AddStringLocalizer(b =>
{
_ = b.FromJson(Assembly.GetExecutingAssembly(), "Resources.Translations-fa-IR.json", new CultureInfo("fa-IR"));
_ = b.FromJson(Assembly.GetExecutingAssembly(), "Resources.Translations-en-US.json", new CultureInfo("en-US"));
var cul = new CultureInfo((JsonConvert.DeserializeObject<LanInfo>(File.ReadAllText(@"CultureInfos.json")).CultureInfo));
b.SetCulture(cul);
Application.Current.Resources["MainDirection"] = cul.EnglishName == "English (United States)" ? FlowDirection.LeftToRight : FlowDirection.RightToLeft;
});


_ = services.AddHostedService<ApplicationHostService>();

// Page resolver service
_ = services.AddSingleton<IPageService, PageService>();

// Theme manipulation
_ = services.AddSingleton<IThemeService, ThemeService>();

// TaskBar manipulation
_ = services.AddSingleton<ITaskBarService, TaskBarService>();

// Service containing navigation, same as INavigationWindow... but without window
_ = services.AddSingleton<INavigationService, NavigationService>();

// Main window with navigation
_ = services.AddSingleton<INavigationWindow, MainWindow>();
_ = services.AddSingleton<MainWindowViewModel>();

_ = services.AddSingleton<DashboardPage>();
_ = services.AddSingleton<DashboardViewModel>();
_ = services.AddTransient<DataPage>();
_ = services.AddTransient<DataViewModel>();
_ = services.AddSingleton<SettingsPage>();
_ = services.AddSingleton<SettingsViewModel>();
}).Build();

/// <summary>
/// Gets registered service.
/// </summary>
/// <typeparam name="T">Type of the service to get.</typeparam>
/// <returns>Instance of the service or <see langword="null"/>.</returns>
public static T GetService<T>()
where T : class
{
return _host.Services.GetService(typeof(T)) as T;
}

/// <summary>
/// Occurs when the application is loading.
/// </summary>
private void OnStartup(object sender, StartupEventArgs e)
{
_host.Start();
}

/// <summary>
/// Occurs when the application is closing.
/// </summary>
private async void OnExit(object sender, ExitEventArgs e)
{
await _host.StopAsync();

_host.Dispose();
}

/// <summary>
/// Occurs when an exception is thrown by an application but not handled.
/// </summary>
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
}
}
15 changes: 15 additions & 0 deletions src/Sample/Galery/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 Lepo.i18n Contributors.
// All Rights Reserved.

using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Binary file added src/Sample/Galery/Assets/wpfui-icon-1024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Sample/Galery/Assets/wpfui-icon-256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/Sample/Galery/CultureInfos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CultureInfo": "en-US"
}
51 changes: 51 additions & 0 deletions src/Sample/Galery/Galery.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>wpfui-icon.ico</ApplicationIcon>
<UseWPF>true</UseWPF>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<Content Include="wpfui-icon.ico" />
</ItemGroup>

<ItemGroup>
<None Remove="Assets\wpfui-icon-256.png" />
<None Remove="Assets\wpfui-icon-1024.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="WPF-UI" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Lepo.i18n.DependencyInjection\Lepo.i18n.DependencyInjection.csproj" />
<ProjectReference Include="..\..\Lepo.i18n.Json\Lepo.i18n.Json.csproj" />
<ProjectReference Include="..\..\Lepo.i18n.Wpf\Lepo.i18n.Wpf.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Translations-en-US.json" />
<EmbeddedResource Include="Resources\Translations-fa-IR.json" />
</ItemGroup>

<ItemGroup>
<Resource Include="Assets\wpfui-icon-256.png" />
<Resource Include="Assets\wpfui-icon-1024.png" />
</ItemGroup>

<ItemGroup>
<None Update="CultureInfos.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions src/Sample/Galery/Helpers/EnumToBooleanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 Lepo.i18n Contributors.
// All Rights Reserved.

using System.Globalization;
using System.Windows.Data;
using Wpf.Ui.Appearance;

namespace Galery.Helpers;
internal class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter is not String enumString)
{
throw new ArgumentException("ExceptionEnumToBooleanConverterParameterMustBeAnEnumName");
}

if (!Enum.IsDefined(typeof(ApplicationTheme), value))
{
throw new ArgumentException("ExceptionEnumToBooleanConverterValueMustBeAnEnum");
}

var enumValue = Enum.Parse(typeof(ApplicationTheme), enumString);

return enumValue.Equals(value);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter is not String enumString)
{
throw new ArgumentException("ExceptionEnumToBooleanConverterParameterMustBeAnEnumName");
}

return Enum.Parse(typeof(ApplicationTheme), enumString);
}
}
13 changes: 13 additions & 0 deletions src/Sample/Galery/Models/AppConfig.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 Lepo.i18n Contributors.
// All Rights Reserved.

namespace Galery.Models;

public class AppConfig
{
public string ConfigurationsFolder { get; set; }

public string AppPropertiesFileName { get; set; }
}
12 changes: 12 additions & 0 deletions src/Sample/Galery/Models/DataColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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 Lepo.i18n Contributors.
// All Rights Reserved.

using System.Windows.Media;

namespace Galery.Models;
public struct DataColor
{
public Brush Color { get; set; }
}
5 changes: 5 additions & 0 deletions src/Sample/Galery/Models/LanInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Galery.Models;
public class LanInfo
{
public string CultureInfo { get; set; } = default!;
}
53 changes: 53 additions & 0 deletions src/Sample/Galery/Resources/Translations-en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"version": "1.0",
"strings": [
{
"name": "Test",
"value": "Text in english"
},
{
"name": "Titele",
"value": "WPF UI - Galery"
},
{
"name": "Lan",
"value": "Language"
},
{
"name": "Menu.Settings",
"value": "Settings"
},
{
"name": "Menu.Dashboard",
"value": "Dashboard"
},
{
"name": "Menu.Data",
"value": "Data"
},
{
"name": "Menu.Search",
"value": "Search"
},
{
"name": "Setting.Personalization",
"value": "Personalization"
},
{
"name": "Setting.Theme",
"value": "Theme"
},
{
"name": "Setting.Theme.Light",
"value": "Light"
},
{
"name": "Setting.Theme.Dark",
"value": "Dark"
},
{
"name": "Dashboard.Click",
"value": "ClickMe !"
}
]
}
Loading

0 comments on commit 15125fe

Please sign in to comment.