11
11
// ReSharper disable once CheckNamespace
12
12
namespace Microsoft . Maui . Hosting ;
13
13
14
+ /// <summary>
15
+ /// An enum
16
+ /// </summary>
17
+ public enum RegisterEventBinderMethod
18
+ {
19
+ /// <summary>
20
+ /// Registers the services directly... unable to inject the MvvM integration this way
21
+ /// </summary>
22
+ Directly ,
23
+ /// <summary>
24
+ /// Register with the service provider
25
+ /// </summary>
26
+ ServiceProvider ,
27
+ /// <summary>
28
+ /// Instantiate the options directly and invoke the config callback on it
29
+ /// </summary>
30
+ InvokeConfigOptions
31
+ }
32
+
14
33
/// <summary>
15
34
/// Sentry extensions for <see cref="MauiAppBuilder"/>.
16
35
/// </summary>
@@ -39,9 +58,10 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, string dsn)
39
58
/// </summary>
40
59
/// <param name="builder">The builder.</param>
41
60
/// <param name="configureOptions">An action to configure the options.</param>
61
+ /// <param name="eventBinderRegistrationMethod"></param>
42
62
/// <returns>The <paramref name="builder"/>.</returns>
43
63
public static MauiAppBuilder UseSentry ( this MauiAppBuilder builder ,
44
- Action < SentryMauiOptions > ? configureOptions )
64
+ Action < SentryMauiOptions > ? configureOptions , RegisterEventBinderMethod eventBinderRegistrationMethod = RegisterEventBinderMethod . ServiceProvider )
45
65
{
46
66
var services = builder . Services ;
47
67
@@ -57,14 +77,43 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder,
57
77
services . AddSingleton < Disposer > ( ) ;
58
78
59
79
// Resolve the configured options and register any element event binders from these
60
- IServiceProvider serviceProvider = services . BuildServiceProvider ( ) ;
61
- var options = serviceProvider . GetRequiredService < IOptions < SentryMauiOptions > > ( ) . Value ;
62
- services . TryAddSingleton < SentryOptions > ( options ) ; // Ensure this doesn't get resolved again in AddSentry
63
- foreach ( var eventBinder in options . DefaultEventBinders )
80
+ switch ( eventBinderRegistrationMethod )
64
81
{
65
- eventBinder . Register ( services ) ;
82
+ case RegisterEventBinderMethod . Directly :
83
+ services . AddSingleton < IMauiElementEventBinder , MauiButtonEventsBinder > ( ) ;
84
+ services . AddSingleton < IMauiElementEventBinder , MauiImageButtonEventsBinder > ( ) ;
85
+ services . AddSingleton < IMauiElementEventBinder , MauiGestureRecognizerEventsBinder > ( ) ;
86
+ services . AddSingleton < IMauiElementEventBinder , MauiVisualElementEventsBinder > ( ) ;
87
+ break ;
88
+ case RegisterEventBinderMethod . InvokeConfigOptions :
89
+ var options = new SentryMauiOptions ( ) ;
90
+ configureOptions ? . Invoke ( options ) ;
91
+ services . TryAddSingleton < SentryOptions > ( options ) ; // Ensure this doesn't get resolved again in AddSentry
92
+ foreach ( var eventBinder in options . DefaultEventBinders )
93
+ {
94
+ eventBinder . Register ( services ) ;
95
+ }
96
+ break ;
97
+ case RegisterEventBinderMethod . ServiceProvider :
98
+ IServiceProvider serviceProvider = services . BuildServiceProvider ( ) ;
99
+ options = serviceProvider . GetRequiredService < IOptions < SentryMauiOptions > > ( ) . Value ;
100
+ services . TryAddSingleton < SentryOptions > ( options ) ; // Ensure this doesn't get resolved again in AddSentry
101
+ foreach ( var eventBinder in options . DefaultEventBinders )
102
+ {
103
+ eventBinder . Register ( services ) ;
104
+ }
105
+ break ;
66
106
}
67
107
108
+ // // Resolve the configured options and register any element event binders from these
109
+ // IServiceProvider serviceProvider = services.BuildServiceProvider();
110
+ // var options = serviceProvider.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
111
+ // services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
112
+ // foreach (var eventBinder in options.DefaultEventBinders)
113
+ // {
114
+ // eventBinder.Register(services);
115
+ // }
116
+
68
117
// This is ultimately the class that enables all the MauiElementEventBinders above
69
118
services . TryAddSingleton < IMauiEventsBinder , MauiEventsBinder > ( ) ;
70
119
0 commit comments