@@ -135,4 +135,62 @@ public static async Task<StorageFile> GetStorageFile(string filePath, PathType p
135135
136136 return file ;
137137 }
138+
139+ public static Stream ? GetFileFromEmbededResources ( Assembly assembly , Uri uri )
140+ {
141+ Stream ? stream = null ;
142+
143+ // Convert ms-appx:///Assets/Mask/ForegroundFocusMask.png -> Assets.Mask.ForegroundFocusMask.png
144+ var path = uri . AbsolutePath . TrimStart ( '/' ) ;
145+ var resourcePath = path . Replace ( '/' , '.' ) ;
146+
147+ string ? resourceName = assembly . GetManifestResourceNames ( )
148+ . FirstOrDefault ( name => name . EndsWith ( resourcePath , StringComparison . OrdinalIgnoreCase ) ) ;
149+ if ( resourceName != null )
150+ {
151+ stream = assembly . GetManifestResourceStream ( resourceName ) ;
152+ }
153+ return stream ;
154+ }
155+
156+ public static Stream ? GetFileFromUri ( Uri uri )
157+ {
158+ try
159+ {
160+ string ? filePath = null ;
161+
162+ if ( uri . IsFile )
163+ {
164+ filePath = uri . LocalPath ;
165+ }
166+ else if ( ! uri . IsAbsoluteUri )
167+ {
168+ filePath = Path . Combine ( AppContext . BaseDirectory , uri . OriginalString . TrimStart ( '/' ) . Replace ( '/' , Path . DirectorySeparatorChar ) ) ;
169+ }
170+ else if ( uri . Scheme . Equals ( "ms-appx" , StringComparison . OrdinalIgnoreCase ) )
171+ {
172+ var path = uri . AbsolutePath . TrimStart ( '/' ) ;
173+ filePath = Path . Combine ( AppContext . BaseDirectory , path . Replace ( '/' , Path . DirectorySeparatorChar ) ) ;
174+ }
175+
176+ if ( filePath != null && File . Exists ( filePath ) )
177+ {
178+ return File . OpenRead ( filePath ) ;
179+ }
180+ }
181+ catch
182+ {
183+ }
184+
185+ return null ;
186+ }
187+
188+ public static Stream ? GetFileFromEmbededResourcesOrUri ( Uri uri , Assembly assembly = null )
189+ {
190+ var stream = GetFileFromEmbededResources ( assembly , uri ) ;
191+ if ( stream != null )
192+ return stream ;
193+
194+ return GetFileFromUri ( uri ) ;
195+ }
138196}
0 commit comments