1- namespace CommunityToolkit . Maui . Extensions ;
1+ using System . Diagnostics . CodeAnalysis ;
2+
3+ namespace CommunityToolkit . Maui . Extensions ;
24
35/// <summary>
46/// A XAML markup extension that enables using <see cref="AppThemeColor"/> and <see cref="AppThemeObject"/> from XAML.
@@ -34,49 +36,48 @@ public BindingBase ProvideValue(IServiceProvider serviceProvider)
3436
3537 if ( TryFindResourceInVisualElement ( targetObject , Key , out var resource ) )
3638 {
37- if ( resource is AppThemeColor color )
38- {
39- return color . GetBinding ( ) ;
40- }
41- if ( resource is AppThemeObject theme )
39+ switch ( resource )
4240 {
43- return theme . GetBinding ( ) ;
41+ case AppThemeColor color :
42+ return color . GetBinding ( ) ;
43+ case AppThemeObject theme :
44+ return theme . GetBinding ( ) ;
45+ default :
46+ var info = ( serviceProvider . GetService ( typeof ( IXmlLineInfoProvider ) ) as IXmlLineInfoProvider ) ? . XmlLineInfo ;
47+ throw new XamlParseException ( $ "Resource found for key { Key } is not a valid AppTheme resource.", info ) ;
4448 }
45-
46- var info = ( serviceProvider . GetService ( typeof ( IXmlLineInfoProvider ) ) as IXmlLineInfoProvider ) ? . XmlLineInfo ;
47- throw new XamlParseException ( $ "Resource found for key { Key } is not a valid AppTheme resource.", info ) ;
4849 }
4950
5051 // Fallback to root object ResourceDictionary (e.g. page-level resources)
5152 var rootProvider = serviceProvider . GetService ( typeof ( IRootObjectProvider ) ) as IRootObjectProvider ;
5253 var root = rootProvider ? . RootObject ;
53- if ( root is IResourcesProvider rootResources && rootResources . IsResourcesCreated
54- && rootResources . Resources . TryGetValue ( Key , out resource ) )
54+ if ( root is IResourcesProvider { IsResourcesCreated : true } rootResources
55+ && rootResources . Resources . TryGetValue ( Key , out resource ) )
5556 {
56- if ( resource is AppThemeColor rootColor )
57+ switch ( resource )
5758 {
58- return rootColor . GetBinding ( ) ;
59+ case AppThemeColor rootColor :
60+ return rootColor . GetBinding ( ) ;
61+ case AppThemeObject rootTheme :
62+ return rootTheme . GetBinding ( ) ;
63+ default :
64+ var info = ( serviceProvider . GetService ( typeof ( IXmlLineInfoProvider ) ) as IXmlLineInfoProvider ) ? . XmlLineInfo ;
65+ throw new XamlParseException ( $ "Resource found for key { Key } is not a valid AppTheme resource.", info ) ;
5966 }
60- if ( resource is AppThemeObject rootTheme )
61- {
62- return rootTheme . GetBinding ( ) ;
63- }
64- var info = ( serviceProvider . GetService ( typeof ( IXmlLineInfoProvider ) ) as IXmlLineInfoProvider ) ? . XmlLineInfo ;
65- throw new XamlParseException ( $ "Resource found for key { Key } is not a valid AppTheme resource.", info ) ;
6667 }
6768
68- if ( Application . Current ? . Resources . TryGetValueAndSource ( Key , out resource , out _ ) == true )
69+ if ( Application . Current ? . Resources . TryGetValueAndSource ( Key , out resource , out _ ) is true )
6970 {
70- if ( resource is AppThemeColor color )
71+ switch ( resource )
7172 {
72- return color . GetBinding ( ) ;
73+ case AppThemeColor color :
74+ return color . GetBinding ( ) ;
75+ case AppThemeObject theme :
76+ return theme . GetBinding ( ) ;
77+ default :
78+ var info = ( serviceProvider . GetService ( typeof ( IXmlLineInfoProvider ) ) as IXmlLineInfoProvider ) ? . XmlLineInfo ;
79+ throw new XamlParseException ( $ "Resource found for key { Key } is not a valid AppTheme resource.", info ) ;
7380 }
74- if ( resource is AppThemeObject theme )
75- {
76- return theme . GetBinding ( ) ;
77- }
78- var info = ( serviceProvider . GetService ( typeof ( IXmlLineInfoProvider ) ) as IXmlLineInfoProvider ) ? . XmlLineInfo ;
79- throw new XamlParseException ( $ "Resource found for key { Key } is not a valid AppTheme resource.", info ) ;
8081 }
8182
8283 var xmlInfo = ( serviceProvider . GetService ( typeof ( IXmlLineInfoProvider ) ) as IXmlLineInfoProvider ) ? . XmlLineInfo ;
@@ -86,38 +87,39 @@ public BindingBase ProvideValue(IServiceProvider serviceProvider)
8687 /// <summary>
8788 /// Attempts to locate a resource by walking up the visual tree from a target object.
8889 /// </summary>
89- static bool TryFindResourceInVisualElement ( object element , string key , out object resource )
90+ static bool TryFindResourceInVisualElement ( object element , string key , [ NotNullWhen ( true ) ] out object ? resource )
9091 {
91- resource = default ! ;
92+ resource = null ;
9293
9394 // If the element has a Resources property via IResourcesProvider
94- if ( element is IResourcesProvider provider && provider . IsResourcesCreated )
95+ if ( element is IResourcesProvider { IsResourcesCreated : true } provider
96+ && provider . Resources . TryGetValue ( key , out resource ) )
9597 {
96- if ( provider . Resources . TryGetValue ( key , out resource ) )
97- {
98- return true ;
99- }
98+ return true ;
10099 }
101100
102- // Walk up the element tree
103- if ( element is Element elementObj )
101+ switch ( element )
104102 {
105- var parent = elementObj . Parent ;
106- while ( parent is not null )
103+ // Walk up the element tree to try to find the resource
104+ case Element elementObj :
107105 {
108- if ( parent is IResourcesProvider parentProvider && parentProvider . IsResourcesCreated
109- && parentProvider . Resources . TryGetValue ( key , out resource ) )
106+ var parent = elementObj . Parent ;
107+ while ( parent is not null )
110108 {
111- return true ;
109+ if ( parent is IResourcesProvider { IsResourcesCreated : true } parentProvider
110+ && parentProvider . Resources . TryGetValue ( key , out resource ) )
111+ {
112+ return true ;
113+ }
114+
115+ parent = parent . Parent ;
112116 }
113- parent = parent . Parent ;
114- }
115- }
116117
117- // If it's a ResourceDictionary, check it directly
118- if ( element is ResourceDictionary dict && dict . TryGetValue ( key , out resource ) )
119- {
120- return true ;
118+ break ;
119+ }
120+ // If it's a ResourceDictionary, check it directly
121+ case ResourceDictionary dict when dict . TryGetValue ( key , out resource ) :
122+ return true ;
121123 }
122124
123125 return false ;
0 commit comments