implemented new XamlResourceCatalog

this will not only fix the XAML resource catalog bugs, but makes it easier to implement.
This commit is contained in:
Brian Lagunas
2020-08-21 22:46:32 -06:00
parent 522799b108
commit 6efa943074
12 changed files with 171 additions and 46 deletions
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net462</TargetFrameworks>
<UseWPF>true</UseWPF>
<RootNamespace>HelloWorld</RootNamespace>
</PropertyGroup>
@@ -15,6 +15,10 @@
<Page Include="..\HelloWorld\**\*.xaml" LinkBase="%(RecurseDir)" Generator="MSBuild:Compile" Exclude="$(DefaultItemExcludes)" />
<Compile Include="..\HelloWorld\**\*.cs" LinkBase="%(RecurseDir)" Exclude="$(DefaultItemExcludes)" />
</ItemGroup>
<ItemGroup>
<Page Remove="..\HelloWorld\ModuleCatalog.xaml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Prism.Core\Prism.Core.csproj" />
@@ -24,4 +28,8 @@
<ProjectReference Include="..\Modules\HelloWorld.Modules.ModuleA\HelloWorld.Modules.ModuleA.csproj" />
</ItemGroup>
<ItemGroup>
<Resource Include="..\HelloWorld\ModuleCatalog.xaml" />
</ItemGroup>
</Project>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net462</TargetFrameworks>
<UseWPF>true</UseWPF>
</PropertyGroup>
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf" />
</configSections>
<startup>
</startup>
<modules>
<module assemblyFile="Modules/HelloWorld.Modules.ModuleA.dll" moduleType="HelloWorld.Modules.ModuleA.ModuleAModule, HelloWorld.Modules.ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleAModule" startupLoaded="True" />
</modules>
</configuration>
+14 -1
View File
@@ -23,8 +23,21 @@ namespace HelloWorld
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
base.ConfigureModuleCatalog(moduleCatalog);
moduleCatalog.AddModule<ModuleAModule>();
}
//to test various module catalogs:
//1. Comment out ConfigureModuleCatalog
//2. Remove the project reference to the module project
//3. Make sure the Modules directory exists in the output path when debugging
//4. Uncomment CreateModuleCatalog to test other modules catalogs
//protected override IModuleCatalog CreateModuleCatalog()
//{
// return new ConfigurationModuleCatalog();
// return new DirectoryModuleCatalog() { ModulePath = "Modules" };
// return new XamlResourceCatalog("ModuleCatalog.xaml");
//}
}
}
+11 -2
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net462</TargetFrameworks>
<UseWPF>true</UseWPF>
<RootNamespace>HelloWorld</RootNamespace>
<AssemblyName>HelloWorld</AssemblyName>
@@ -14,12 +14,21 @@
<ApplicationIcon>prism-sandbox.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Page Remove="ModuleCatalog.xaml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Prism.Core\Prism.Core.csproj" />
<ProjectReference Include="..\..\..\src\Wpf\Prism.DryIoc.Wpf\Prism.DryIoc.Wpf.csproj" />
<ProjectReference Include="..\..\..\src\Wpf\Prism.Wpf\Prism.Wpf.csproj" />
<ProjectReference Include="..\HelloWorld.Core\HelloWorld.Core.csproj" />
<ProjectReference Include="..\Modules\HelloWorld.Modules.ModuleA\HelloWorld.Modules.ModuleA.csproj" />
</ItemGroup>
<ItemGroup>
<Resource Include="ModuleCatalog.xaml">
<Generator>MSBuild:Compile</Generator>
</Resource>
</ItemGroup>
</Project>
+16
View File
@@ -0,0 +1,16 @@
<m:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Prism.Modularity;assembly=Prism.Wpf">
<m:ModuleInfo Ref="Modules/HelloWorld.Modules.ModuleA.dll"
ModuleName="ModuleAModule"
ModuleType="HelloWorld.Modules.ModuleA.ModuleAModule, HelloWorld.Modules.ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<!--<m:ModuleInfoGroup Ref="Modules/HelloWorld.Modules.ModuleA.dll">
<m:ModuleInfo
ModuleName="ModuleAModule"
ModuleType="HelloWorld.Modules.ModuleA.ModuleAModule, HelloWorld.Modules.ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</m:ModuleInfoGroup>-->
</m:ModuleCatalog>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net462</TargetFrameworks>
<UseWPF>true</UseWPF>
</PropertyGroup>
@@ -10,5 +10,7 @@
<ProjectReference Include="..\..\..\..\src\Wpf\Prism.Wpf\Prism.Wpf.csproj" />
<ProjectReference Include="..\..\HelloWorld.Core\HelloWorld.Core.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetDir)*.*&quot; &quot;$(SolutionDir)$(SolutionName)\$(OutDir)Modules&quot; /Y" />
</Target>
</Project>
@@ -28,7 +28,7 @@ namespace Prism.Modularity
public class ModuleCatalogBase : IModuleCatalog
{
private ModuleCatalogItemCollection _items { get; }
private bool isLoaded;
private bool _isLoaded;
/// <summary>
/// Initializes a new instance of the <see cref="IModuleCatalog"/> class.
@@ -91,7 +91,7 @@ namespace Prism.Modularity
/// </summary>
public virtual void Load()
{
isLoaded = true;
_isLoaded = true;
InnerLoad();
}
@@ -179,7 +179,7 @@ namespace Prism.Modularity
/// <exception cref="ModularityException">When validation of the <see cref="ModuleCatalogBase"/> fails, because this method calls <see cref="Validate"/>.</exception>
public virtual void Initialize()
{
if (!isLoaded)
if (!_isLoaded)
{
Load();
}
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Prism.Properties;
@@ -20,7 +17,7 @@ namespace Prism.Modularity
/// </summary>
public ConfigurationModuleCatalog()
{
this.Store = new ConfigurationStore();
Store = new ConfigurationStore();
}
/// <summary>
@@ -33,28 +30,17 @@ namespace Prism.Modularity
/// </summary>
protected override void InnerLoad()
{
if (this.Store == null)
if (Store == null)
{
throw new InvalidOperationException(Resources.ConfigurationStoreCannotBeNull);
}
this.EnsureModulesDiscovered();
}
private static string GetFileAbsoluteUri(string filePath)
{
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Host = String.Empty;
uriBuilder.Scheme = Uri.UriSchemeFile;
uriBuilder.Path = Path.GetFullPath(filePath);
Uri fileUri = uriBuilder.Uri;
return fileUri.ToString();
EnsureModulesDiscovered();
}
private void EnsureModulesDiscovered()
{
ModulesConfigurationSection section = this.Store.RetrieveModuleConfigurationSection();
ModulesConfigurationSection section = Store.RetrieveModuleConfigurationSection();
if (section != null)
{
@@ -72,5 +72,21 @@ namespace Prism.Modularity
return null;
}
/// <summary>
///
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
protected string GetFileAbsoluteUri(string filePath)
{
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Host = String.Empty;
uriBuilder.Scheme = Uri.UriSchemeFile;
uriBuilder.Path = Path.GetFullPath(filePath);
Uri fileUri = uriBuilder.Uri;
return fileUri.ToString();
}
}
}
+25 -18
View File
@@ -17,7 +17,7 @@ namespace Prism.Modularity
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
public class ModuleInfoGroup : IModuleInfoGroup
{
private readonly Collection<IModuleInfo> modules = new Collection<IModuleInfo>();
private readonly Collection<IModuleInfo> _modules = new Collection<IModuleInfo>();
/// <summary>
/// Gets or sets the <see cref="IModuleInfo.InitializationMode"/> for the whole group. Any <see cref="IModuleInfo"/> classes that are
@@ -46,9 +46,16 @@ namespace Prism.Modularity
public void Add(IModuleInfo item)
{
ForwardValues(item);
modules.Add(item);
_modules.Add(item);
}
internal void UpdateModulesRef()
{
foreach(var module in _modules)
{
module.Ref = Ref;
}
}
/// <summary>
/// Forwards <see cref="InitializationMode"/> and <see cref="Ref"/> properties from this <see cref="ModuleInfoGroup"/>
@@ -75,7 +82,7 @@ namespace Prism.Modularity
/// <summary>
/// Removes all <see cref="IModuleInfo"/>s from the <see cref="ModuleInfoGroup"/>.
/// </summary>
public void Clear() => modules.Clear();
public void Clear() => _modules.Clear();
/// <summary>
/// Determines whether the <see cref="ModuleInfoGroup"/> contains a specific value.
@@ -84,7 +91,7 @@ namespace Prism.Modularity
/// <returns>
/// true if <paramref name="item"/> is found in the <see cref="ModuleInfoGroup"/>; otherwise, false.
/// </returns>
public bool Contains(IModuleInfo item) => modules.Contains(item);
public bool Contains(IModuleInfo item) => _modules.Contains(item);
/// <summary>
/// Copies the elements of the <see cref="ModuleInfoGroup"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
@@ -106,7 +113,7 @@ namespace Prism.Modularity
/// </exception>
public void CopyTo(IModuleInfo[] array, int arrayIndex)
{
modules.CopyTo(array, arrayIndex);
_modules.CopyTo(array, arrayIndex);
}
/// <summary>
@@ -116,7 +123,7 @@ namespace Prism.Modularity
/// <returns>
/// The number of elements contained in the <see cref="ModuleInfoGroup"/>.
/// </returns>
public int Count => modules.Count;
public int Count => _modules.Count;
/// <summary>
/// Gets a value indicating whether the <see cref="ModuleInfoGroup"/> is read-only.
@@ -133,7 +140,7 @@ namespace Prism.Modularity
/// <returns>
/// true if <paramref name="item"/> was successfully removed from the <see cref="ModuleInfoGroup"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="ModuleInfoGroup"/>.
/// </returns>
public bool Remove(IModuleInfo item) => modules.Remove(item);
public bool Remove(IModuleInfo item) => _modules.Remove(item);
/// <summary>
/// Returns an enumerator that iterates through the collection.
@@ -141,7 +148,7 @@ namespace Prism.Modularity
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
public IEnumerator<IModuleInfo> GetEnumerator() => modules.GetEnumerator();
public IEnumerator<IModuleInfo> GetEnumerator() => _modules.GetEnumerator();
/// <summary>
/// Returns an enumerator that iterates through a collection.
@@ -199,7 +206,7 @@ namespace Prism.Modularity
/// <returns>
/// The index of <paramref name="value"/> if found in the list; otherwise, -1.
/// </returns>
public int IndexOf(object value) => modules.IndexOf((IModuleInfo)value);
public int IndexOf(object value) => _modules.IndexOf((IModuleInfo)value);
/// <summary>
/// Inserts an item to the <see cref="ModuleInfoGroup"/> at the specified index.
@@ -227,7 +234,7 @@ namespace Prism.Modularity
if (!(value is IModuleInfo moduleInfo))
throw new ArgumentException(Resources.ValueMustBeOfTypeModuleInfo, nameof(value));
modules.Insert(index, moduleInfo);
_modules.Insert(index, moduleInfo);
}
/// <summary>
@@ -259,7 +266,7 @@ namespace Prism.Modularity
/// <exception cref="T:System.NotSupportedException">
/// The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
/// </exception>
public void RemoveAt(int index) => modules.RemoveAt(index);
public void RemoveAt(int index) => _modules.RemoveAt(index);
/// <summary>
/// Gets or sets the <see cref="object"/> at the specified index.
@@ -293,7 +300,7 @@ namespace Prism.Modularity
/// The type of the source <see cref="T:System.Collections.ICollection"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.
/// </exception>
void ICollection.CopyTo(Array array, int index) =>
((ICollection)modules).CopyTo(array, index);
((ICollection)_modules).CopyTo(array, index);
/// <summary>
/// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe).
@@ -301,7 +308,7 @@ namespace Prism.Modularity
/// <value></value>
/// <returns>true if access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe); otherwise, false.
/// </returns>
public bool IsSynchronized => ((ICollection)modules).IsSynchronized;
public bool IsSynchronized => ((ICollection)_modules).IsSynchronized;
/// <summary>
/// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
@@ -310,7 +317,7 @@ namespace Prism.Modularity
/// <returns>
/// An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
/// </returns>
public object SyncRoot => ((ICollection)modules).SyncRoot;
public object SyncRoot => ((ICollection)_modules).SyncRoot;
/// <summary>
/// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
@@ -319,7 +326,7 @@ namespace Prism.Modularity
/// <returns>
/// The index of <paramref name="item"/> if found in the list; otherwise, -1.
/// </returns>
public int IndexOf(IModuleInfo item) => modules.IndexOf(item);
public int IndexOf(IModuleInfo item) => _modules.IndexOf(item);
/// <summary>
/// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
@@ -329,7 +336,7 @@ namespace Prism.Modularity
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
/// </exception>
public void Insert(int index, IModuleInfo item) => modules.Insert(index, item);
public void Insert(int index, IModuleInfo item) => _modules.Insert(index, item);
/// <summary>
/// Gets or sets the <see cref="IModuleInfo"/> at the specified index.
@@ -337,8 +344,8 @@ namespace Prism.Modularity
/// <value>The <see cref="IModuleInfo"/> at the specified index </value>
public IModuleInfo this[int index]
{
get => modules[index];
set => modules[index] = value;
get => _modules[index];
set => _modules[index] = value;
}
}
}
@@ -0,0 +1,57 @@
using System;
namespace Prism.Modularity
{
/// <summary>
/// A catalog built from a XAML file.
/// </summary>
public class XamlResourceCatalog : ModuleCatalog
{
private readonly Uri _resourceUri;
/// <summary>
/// Creates an instance of a XamlResourceCatalog.
/// </summary>
/// <param name="fileName">The name of the XAML file</param>
public XamlResourceCatalog(string fileName)
: this(new Uri(fileName, UriKind.Relative))
{
}
/// <summary>
/// Creates an instance of a XamlResourceCatalog.
/// </summary>
/// <param name="resourceUri">The pack url of the XAML file resource</param>
public XamlResourceCatalog(Uri resourceUri)
{
_resourceUri = resourceUri;
}
/// <summary>
/// Loads the catalog from the XAML file.
/// </summary>
protected override void InnerLoad()
{
var catalog = ModuleCatalog.CreateFromXaml(_resourceUri);
foreach(IModuleCatalogItem item in catalog.Items)
{
if (item is ModuleInfo mi)
{
if (!string.IsNullOrWhiteSpace(mi.Ref))
mi.Ref = GetFileAbsoluteUri(mi.Ref);
}
else if (item is ModuleInfoGroup mg)
{
if (!string.IsNullOrWhiteSpace(mg.Ref))
{
mg.Ref = GetFileAbsoluteUri(mg.Ref);
mg.UpdateModulesRef();
}
}
Items.Add(item);
}
}
}
}