-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow configuring current UI framework to avoid exceptions during dependency registrations #2396
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.IO; | ||
|
@@ -19,6 +20,73 @@ namespace ReactiveUI | |
/// </summary> | ||
public static class DependencyResolverMixins | ||
{ | ||
private static IReadOnlyList<RegistrationNamespace> defaultRegistrationNamespaces = | ||
(RegistrationNamespace[])Enum.GetValues(typeof(RegistrationNamespace)); | ||
|
||
private static HashSet<RegistrationNamespace> registrationNamespacesToInitialize = | ||
new HashSet<RegistrationNamespace>(defaultRegistrationNamespaces); | ||
|
||
/// <summary> | ||
/// Platforms or other registration namespaces for the dependency resolver to consider when initializing. | ||
/// </summary> | ||
public enum RegistrationNamespace | ||
{ | ||
/// <summary> | ||
/// Xamarin Forms | ||
/// </summary> | ||
XamForms, | ||
|
||
/// <summary> | ||
/// Windows Forms | ||
/// </summary> | ||
Winforms, | ||
|
||
/// <summary> | ||
/// WPF | ||
/// </summary> | ||
Wpf, | ||
|
||
/// <summary> | ||
/// Uno | ||
/// </summary> | ||
Uno, | ||
|
||
/// <summary> | ||
/// Blazor | ||
/// </summary> | ||
Blazor, | ||
|
||
/// <summary> | ||
/// Splat.Drawing | ||
/// </summary> | ||
Drawing | ||
} | ||
|
||
/// <summary> | ||
/// Gets the default registration namespaces for the dependency resolver to consider, consisting of all registration namespaces. | ||
/// </summary> | ||
public static IReadOnlyList<RegistrationNamespace> DefaultRegistrationNamespaces => | ||
defaultRegistrationNamespaces; | ||
|
||
// The reason SetRegistrationNamespaces is a separate method and not a parameter to InitializeReactiveUI | ||
// is because InitializeReactiveUI is called from within the RxApp static constructor, and there's no | ||
// way to directly pass it parameters. | ||
|
||
/// <summary> | ||
/// This method allows you to initialize which platforms <see cref="InitializeReactiveUI"/> | ||
/// attempts to discover registrations for. If this method is not called, all platforms are assumed. | ||
/// </summary> | ||
/// <remarks>Call this before <see cref="InitializeReactiveUI"/>.</remarks> | ||
/// <param name="registrationNamespaces">Which platforms to use.</param> | ||
public static void SetRegistrationNamespaces(params RegistrationNamespace[] registrationNamespaces) | ||
{ | ||
registrationNamespacesToInitialize.Clear(); | ||
foreach (var platform in registrationNamespaces) | ||
{ | ||
registrationNamespacesToInitialize.Add(platform); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This method allows you to initialize resolvers with the default | ||
/// ReactiveUI types. All resolvers used as the default | ||
|
@@ -28,16 +96,22 @@ public static class DependencyResolverMixins | |
[SuppressMessage("Globalization", "CA1307: operator could change based on locale settings", Justification = "Replace() does not have third parameter on all platforms")] | ||
public static void InitializeReactiveUI(this IMutableDependencyResolver resolver) | ||
{ | ||
var extraNs = new[] | ||
var possibleNamespaces = new Dictionary<RegistrationNamespace, string> | ||
{ | ||
"ReactiveUI.XamForms", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order we previously looked was used as part of our previous tests. It may be best to instead of using a dictionary to use a List. It's only 6 entries so a dictionary probably wouldn't perform better anyway. |
||
"ReactiveUI.Winforms", | ||
"ReactiveUI.Wpf", | ||
"ReactiveUI.Uno", | ||
"ReactiveUI.Blazor", | ||
"ReactiveUI.Drawing" | ||
{ RegistrationNamespace.XamForms, "ReactiveUI.XamForms" }, | ||
{ RegistrationNamespace.Winforms, "ReactiveUI.Winforms" }, | ||
{ RegistrationNamespace.Wpf, "ReactiveUI.Wpf" }, | ||
{ RegistrationNamespace.Uno, "ReactiveUI.Uno" }, | ||
{ RegistrationNamespace.Blazor, "ReactiveUI.Blazor" }, | ||
{ RegistrationNamespace.Drawing, "ReactiveUI.Drawing" } | ||
}; | ||
|
||
var extraNs = | ||
possibleNamespaces | ||
.Where(kvp => registrationNamespacesToInitialize.Contains(kvp.Key)) | ||
.Select(kvp => kvp.Value) | ||
.ToArray(); | ||
|
||
// Set up the built-in registration | ||
new Registrations().Register((f, t) => resolver.RegisterConstant(f(), t)); | ||
new PlatformRegistrations().Register((f, t) => resolver.RegisterConstant(f(), t)); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would prefer this to be a separate non-nested enum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that and didn't know in which file it would live - is a neighboring file okay or should it live in some other folder?