Skip to content

Commit 71a9d5b

Browse files
Fix unpackaged app debugging issue report at #406 - StorageFile.GetFileFromApplicationUriAsync doesn't support in unpackaged app (#407)
* Fix StorageFile cannot load in Unpackaged App situation. * Rename the pub file, given we don't add 10 as part of win10 already in any latest sample, moreover build warning show cannot find the win-arm64.xml given the csproj define the name without 10 as well. * Update Readme.md to mention how to build or dotnet run as unpackaged app as example. * Remove the nuget.config given we don't need it, and we cannot point to public nuget feed.
1 parent bbba82d commit 71a9d5b

File tree

9 files changed

+68
-31
lines changed

9 files changed

+68
-31
lines changed

Diff for: Samples/WindowsCopilotRuntime/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ join the [Windows Insider Program](https://insider.windows.com).
3232

3333
- Open the solution file (`.sln`) in Visual Studio.
3434
- From Visual Studio, either **Start Without Debugging** (Ctrl+F5) or **Start Debugging** (F5).
35+
36+
## Special Considerations for Debugging as an Unpackaged App in Visual Studio
37+
38+
- This project is not designed to be fully functional in an unpackaged app. However, Windows Copilot Runtime does support unpackaged app.
39+
- To enable proper startup as an unpackaged app, you need to bootstrap the Windows App SDK either Programmatically or By adding the following configuration to the `.csproj` file during the build process:
40+
```xml
41+
<WindowsAppSdkBootstrapInitialize>true</WindowsAppSdkBootstrapInitialize>
42+
```
43+
- Alternatively, if you are using the Developer Command Prompt for Visual Studio, you can run the app as an ARM64 version using the following command, with the bootstrap property provided:
44+
```powershell
45+
dotnet run -p:Configuration=Debug -p:Platform=ARM64 -p:WindowsPackageType=None -p:WindowsAppSdkBootstrapInitialize=true
46+
```

Diff for: Samples/WindowsCopilotRuntime/cs-winui/Controls/CodeBlockControl.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async void ExampleCode_Loaded(object sender, RoutedEventArgs e)
3838
{
3939
if (SourceFile != null)
4040
{
41-
_content = await SourceFile.ReadFromStorageFileAsync();
41+
_content = await SourceFile.ReadTextAsync();
4242
codeBlock.Blocks.Clear();
4343
codeBlock.Blocks.Add(HighlightSyntax(_content));
4444
}

Diff for: Samples/WindowsCopilotRuntime/cs-winui/Util/SoftwareBitmapExtensions.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading.Tasks;
77
using Windows.Foundation;
88
using Windows.Graphics.Imaging;
9-
using Windows.Storage;
109
using Windows.Storage.Streams;
1110

1211
namespace WindowsCopilotRuntimeSample.Util;
@@ -32,10 +31,7 @@ public static async Task<SoftwareBitmapSource> ToSourceAsync(this SoftwareBitmap
3231

3332
public static async Task<SoftwareBitmap> FilePathToSoftwareBitmapAsync(this string filePath)
3433
{
35-
var uri = new Uri("ms-appx:///" + filePath);
36-
StorageFile inputFile = await StorageFile.GetFileFromApplicationUriAsync(uri);
37-
38-
using IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read);
34+
using IRandomAccessStream stream = await StorageFileExtensions.CreateStreamAsync(filePath);
3935
// Create the decoder from the stream
4036
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
4137
// Get the SoftwareBitmap representation of the file
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,64 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33
using System;
4+
using System.IO;
45
using System.Threading.Tasks;
56
using Windows.Storage;
7+
using Windows.Storage.Streams;
68

79
namespace WindowsCopilotRuntimeSample.Util;
810

911
internal static class StorageFileExtensions
1012
{
11-
public static async Task<string> ReadFromStorageFileAsync(this string filepath)
13+
private static bool IsPackagedApp = (Environment.GetEnvironmentVariable("PACKAGED_PRODUCT_ID") != null);
14+
15+
internal static async Task<string> ReadTextAsync(this string filepath)
1216
{
13-
if (filepath == null)
17+
if (string.IsNullOrWhiteSpace(filepath))
1418
{
1519
return string.Empty;
1620
}
17-
var uri = new Uri("ms-appx:///" + filepath);
18-
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
1921

20-
string content = await FileIO.ReadTextAsync(file);
21-
return content;
22+
if (IsPackagedApp)
23+
{
24+
StorageFile file = await CreateStorageFile(filepath);
25+
string content = await FileIO.ReadTextAsync(file);
26+
return content;
27+
}
28+
else
29+
{
30+
string filePath = CombineWithBasePath(filepath);
31+
string content = await File.ReadAllTextAsync(filePath);
32+
return content;
33+
}
2234
}
2335

36+
internal static async Task<IRandomAccessStream> CreateStreamAsync(this string filepath)
37+
{
38+
if (string.IsNullOrWhiteSpace(filepath))
39+
{
40+
return MemoryStream.Null.AsRandomAccessStream();
41+
}
42+
43+
if (IsPackagedApp)
44+
{
45+
StorageFile file = await CreateStorageFile(filepath);
46+
return await file.OpenAsync(FileAccessMode.Read);
47+
}
48+
else
49+
{
50+
string filePath = CombineWithBasePath(filepath);
51+
return File.OpenRead(filePath).AsRandomAccessStream();
52+
}
53+
}
54+
private static Task<StorageFile> CreateStorageFile(string filepath)
55+
{
56+
var uri = new Uri("ms-appx:///" + filepath);
57+
return StorageFile.GetFileFromApplicationUriAsync(uri).AsTask();
58+
}
59+
60+
private static string CombineWithBasePath(string filepath)
61+
{
62+
return Path.Combine(AppContext.BaseDirectory, filepath);
63+
}
2464
}

Diff for: Samples/WindowsCopilotRuntime/cs-winui/WindowsCopilotRuntimeSample.csproj

+8-9
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@
3939
</ItemGroup>
4040

4141
<ItemGroup>
42-
<Content Include="Assets\SplashScreen.scale-200.png" />
43-
<Content Include="Assets\LockScreenLogo.scale-200.png" />
44-
<Content Include="Assets\Square150x150Logo.scale-200.png" />
45-
<Content Include="Assets\Square44x44Logo.scale-200.png" />
46-
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
47-
<Content Include="Assets\StoreLogo.png" />
48-
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
49-
<Content Include="Examples\*.md">
50-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
42+
<Content Include="Assets\*.jpg">
43+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
44+
</Content>
45+
<Content Include="Assets\*.png">
46+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
47+
</Content>
48+
<Content Include="Examples\*.md">
49+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5150
</Content>
5251
</ItemGroup>
5352

Diff for: Samples/WindowsCopilotRuntime/cs-winui/nuget.config

-10
This file was deleted.

0 commit comments

Comments
 (0)