Skip to content

Commit eccb899

Browse files
authored
Fixed sample source not displaying on Wasm, WPF and GTK (CommunityToolkit#88)
* Fixed sample source not displaying on Wasm, WPF and GTK * Removed irrelevant note * Remove unused variable
1 parent 7171abf commit eccb899

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

CommunityToolkit.Labs.Shared/Renderers/ToolkitSampleRenderer.xaml.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,24 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
154154

155155
public static async Task<string?> GetMetadataFileContents(ToolkitSampleMetadata metadata, string fileExtension)
156156
{
157-
var filePath = GetPathToFileWithoutExtension(metadata.SampleControlType);
157+
var filePath = GetRelativePathToFileWithoutExtension(metadata.SampleControlType);
158158

159159
try
160160
{
161-
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"{filePath}.{fileExtension.Trim('.')}"));
162-
var textContents = await FileIO.ReadTextAsync(file);
161+
// Workaround for https://github.com/unoplatform/uno/issues/8649
162+
if (fileExtension.Contains(".cs"))
163+
{
164+
fileExtension = fileExtension.Replace(".cs", ".cs.dat");
165+
}
163166

164-
// Remove toolkit attributes
165-
textContents = Regex.Replace(textContents, @$"\s+?\[{nameof(ToolkitSampleAttribute).Replace("Attribute", "")}.+\]", "");
166-
textContents = Regex.Replace(textContents, @$"\s+?\[{nameof(ToolkitSampleOptionsPaneAttribute).Replace("Attribute", "")}.+\]", "");
167+
var finalPath = $"ms-appx:///{filePath}.{fileExtension.Trim('.')}";
168+
169+
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///{filePath}.{fileExtension.Trim('.')}"));
170+
var textContents = await FileIO.ReadTextAsync(file);
167171

168172
return textContents;
169173
}
170-
catch (Exception)
174+
catch
171175
{
172176
return null;
173177
}
@@ -177,7 +181,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
177181
/// Compute path to a code file bundled in the app using type information.
178182
/// Assumes file path in project matches namespace.
179183
/// </summary>
180-
public static string GetPathToFileWithoutExtension(Type type)
184+
private static string GetRelativePathToFileWithoutExtension(Type type)
181185
{
182186
var simpleAssemblyName = type.Assembly.GetName().Name;
183187
var typeNamespace = type.Namespace;
@@ -188,9 +192,11 @@ public static string GetPathToFileWithoutExtension(Type type)
188192
if (string.IsNullOrWhiteSpace(typeNamespace))
189193
throw new ArgumentException($"Unable to find namespace for provided type {type}.", nameof(typeNamespace));
190194

195+
var sampleName = simpleAssemblyName.Replace(".Sample", "");
196+
191197
var folderPath = typeNamespace.Replace(simpleAssemblyName, "").Trim('.').Replace('.', '/');
192198

193-
return $"ms-appx:///{simpleAssemblyName}/{folderPath}/{type.Name}";
199+
return $"SourceAssets/{sampleName}/samples/{simpleAssemblyName}/{folderPath}/{type.Name}";
194200
}
195201
}
196202
}

Labs.Head.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@
2222
<Analyzer Include="$(RepositoryDirectory)common\CommunityToolkit.Labs.Core.SourceGenerators\bin\$(Configuration)\netstandard2.0\CommunityToolkit.Labs.Core.SourceGenerators.dll" />
2323
</ItemGroup>
2424

25+
<ItemGroup>
26+
<Content Include="$(RepositoryDirectory)labs\**\samples\*.Sample\**\*.xaml" Exclude="$(RepositoryDirectory)labs\**\samples\*.Sample\obj\**\*.xaml;$(RepositoryDirectory)labs\**\samples\*.Sample\bin\**\*.xaml" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension)"/>
27+
28+
<!-- Link/.dat is a workaround for https://github.com/unoplatform/uno/issues/8649 -->
29+
<Content Include="$(RepositoryDirectory)labs\**\samples\*.Sample\**\*.cs" Exclude="$(RepositoryDirectory)labs\**\samples\*.Sample\obj\**\*.cs;$(RepositoryDirectory)labs\**\samples\*.Sample\bin\**\*.cs" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension).dat" />
30+
</ItemGroup>
31+
2532
</Project>

0 commit comments

Comments
 (0)