リビジョン | 65f6e40d206f286362c779283578a8ef746753ca (tree) |
---|---|
日時 | 2022-08-16 00:39:30 |
作者 | yoshy <yoshy.org.bitbucket@gz.j...> |
コミッター | yoshy |
[MOD] ResourceHelper の実装を CleanAuLait.Prism.WinUI3 からフィードバック
@@ -3,6 +3,7 @@ using System.IO; | ||
3 | 3 | using System.Reflection; |
4 | 4 | using System.Windows; |
5 | 5 | using System.Windows.Media.Imaging; |
6 | +using Windows.Storage; | |
6 | 7 | |
7 | 8 | namespace CleanAuLait.Core.Resource |
8 | 9 | { |
@@ -12,9 +13,15 @@ namespace CleanAuLait.Core.Resource | ||
12 | 13 | |
13 | 14 | public static string LoadTextFromResource(string resourcePathPrefix, string resourceRelativePath, Assembly assembly = null) |
14 | 15 | { |
16 | + Uri resourceUri = CreateResourceUri(assembly, resourcePathPrefix, resourceRelativePath); | |
17 | + | |
18 | + return LoadTextFromUri(resourceUri); | |
19 | + } | |
20 | + | |
21 | + private static string LoadTextFromUri(Uri resourceUri) | |
22 | + { | |
15 | 23 | try |
16 | 24 | { |
17 | - Uri resourceUri = CreateResourceUri(assembly, resourcePathPrefix, resourceRelativePath); | |
18 | 25 | var sri = Application.GetResourceStream(resourceUri); |
19 | 26 | |
20 | 27 | using var sr = new StreamReader(sri.Stream); |
@@ -25,23 +32,27 @@ namespace CleanAuLait.Core.Resource | ||
25 | 32 | catch (SystemException e) |
26 | 33 | { |
27 | 34 | logger.Error(e); |
28 | - throw new ApplicationException($"リソーステキスト {resourcePathPrefix}{resourceRelativePath} の読み込みに失敗しました", e); | |
35 | + throw new ApplicationException($"リソーステキスト {resourceUri} の読み込みに失敗しました", e); | |
29 | 36 | } |
30 | 37 | } |
31 | - | |
32 | - /// <see cref="https://stackoverflow.com/questions/347614/storing-wpf-image-resources"/> | |
33 | 38 | public static BitmapImage LoadBitmapFromResource(string resourcePathPrefix, string resourceRelativePath, Assembly assembly = null) |
34 | 39 | { |
40 | + Uri resourceUri = CreateResourceUri(assembly, resourcePathPrefix, resourceRelativePath); | |
41 | + | |
42 | + return LoadBitmapFromUri(resourceUri); | |
43 | + } | |
44 | + | |
45 | + private static BitmapImage LoadBitmapFromUri(Uri resourceUri) | |
46 | + { | |
35 | 47 | try |
36 | 48 | { |
37 | - Uri resourceUri = CreateResourceUri(assembly, resourcePathPrefix, resourceRelativePath); | |
38 | - | |
49 | + /// <see cref="https://stackoverflow.com/questions/347614/storing-wpf-image-resources"/> | |
39 | 50 | return new BitmapImage(resourceUri); |
40 | 51 | } |
41 | 52 | catch (SystemException e) |
42 | 53 | { |
43 | 54 | logger.Error(e); |
44 | - throw new ApplicationException($"リソース画像 {resourcePathPrefix}{resourceRelativePath} の読み込みに失敗しました", e); | |
55 | + throw new ApplicationException($"リソース画像 {resourceUri} の読み込みに失敗しました", e); | |
45 | 56 | } |
46 | 57 | } |
47 | 58 |
@@ -52,17 +63,24 @@ namespace CleanAuLait.Core.Resource | ||
52 | 63 | assembly = Assembly.GetCallingAssembly(); |
53 | 64 | } |
54 | 65 | |
66 | + string assemblyName = assembly.GetName().Name; | |
67 | + | |
68 | + string resourcePath = CreateResourcePath(resourcePathPrefix, resourceRelativePath); | |
69 | + | |
70 | + Uri resourceUri = CreateResourceUri(assemblyName, resourcePath); | |
71 | + | |
72 | + return resourceUri; | |
73 | + } | |
74 | + | |
75 | + private static string CreateResourcePath(string resourcePathPrefix, string resourceRelativePath) | |
76 | + { | |
55 | 77 | if (resourcePathPrefix[0] == '/') |
56 | 78 | { |
57 | 79 | resourcePathPrefix = resourcePathPrefix[1..]; |
58 | 80 | } |
59 | 81 | |
60 | - string assemblyName = assembly.GetName().Name; | |
61 | 82 | string resourcePath = resourcePathPrefix + resourceRelativePath; |
62 | - | |
63 | - Uri resourceUri = CreateResourceUri(assemblyName, resourcePath); | |
64 | - | |
65 | - return resourceUri; | |
83 | + return resourcePath; | |
66 | 84 | } |
67 | 85 | |
68 | 86 | private static Uri CreateResourceUri(string assemblyName, string resourcePath) |