Skip to content

Commit

Permalink
Fix unpackaged app debugging issue report at #406 - StorageFile.GetFi…
Browse files Browse the repository at this point in the history
…leFromApplicationUriAsync 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.
  • Loading branch information
yeelam-gordon authored Feb 20, 2025
1 parent bbba82d commit 71a9d5b
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 31 deletions.
12 changes: 12 additions & 0 deletions Samples/WindowsCopilotRuntime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ join the [Windows Insider Program](https://insider.windows.com).

- Open the solution file (`.sln`) in Visual Studio.
- From Visual Studio, either **Start Without Debugging** (Ctrl+F5) or **Start Debugging** (F5).

## Special Considerations for Debugging as an Unpackaged App in Visual Studio

- This project is not designed to be fully functional in an unpackaged app. However, Windows Copilot Runtime does support unpackaged app.
- 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:
```xml
<WindowsAppSdkBootstrapInitialize>true</WindowsAppSdkBootstrapInitialize>
```
- 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:
```powershell
dotnet run -p:Configuration=Debug -p:Platform=ARM64 -p:WindowsPackageType=None -p:WindowsAppSdkBootstrapInitialize=true
```
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async void ExampleCode_Loaded(object sender, RoutedEventArgs e)
{
if (SourceFile != null)
{
_content = await SourceFile.ReadFromStorageFileAsync();
_content = await SourceFile.ReadTextAsync();
codeBlock.Blocks.Clear();
codeBlock.Blocks.Add(HighlightSyntax(_content));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;

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

public static async Task<SoftwareBitmap> FilePathToSoftwareBitmapAsync(this string filePath)
{
var uri = new Uri("ms-appx:///" + filePath);
StorageFile inputFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

using IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read);
using IRandomAccessStream stream = await StorageFileExtensions.CreateStreamAsync(filePath);
// Create the decoder from the stream
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
// Get the SoftwareBitmap representation of the file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,64 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace WindowsCopilotRuntimeSample.Util;

internal static class StorageFileExtensions
{
public static async Task<string> ReadFromStorageFileAsync(this string filepath)
private static bool IsPackagedApp = (Environment.GetEnvironmentVariable("PACKAGED_PRODUCT_ID") != null);

internal static async Task<string> ReadTextAsync(this string filepath)
{
if (filepath == null)
if (string.IsNullOrWhiteSpace(filepath))
{
return string.Empty;
}
var uri = new Uri("ms-appx:///" + filepath);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);

string content = await FileIO.ReadTextAsync(file);
return content;
if (IsPackagedApp)
{
StorageFile file = await CreateStorageFile(filepath);
string content = await FileIO.ReadTextAsync(file);
return content;
}
else
{
string filePath = CombineWithBasePath(filepath);
string content = await File.ReadAllTextAsync(filePath);
return content;
}
}

internal static async Task<IRandomAccessStream> CreateStreamAsync(this string filepath)
{
if (string.IsNullOrWhiteSpace(filepath))
{
return MemoryStream.Null.AsRandomAccessStream();
}

if (IsPackagedApp)
{
StorageFile file = await CreateStorageFile(filepath);
return await file.OpenAsync(FileAccessMode.Read);
}
else
{
string filePath = CombineWithBasePath(filepath);
return File.OpenRead(filePath).AsRandomAccessStream();
}
}
private static Task<StorageFile> CreateStorageFile(string filepath)
{
var uri = new Uri("ms-appx:///" + filepath);
return StorageFile.GetFileFromApplicationUriAsync(uri).AsTask();
}

private static string CombineWithBasePath(string filepath)
{
return Path.Combine(AppContext.BaseDirectory, filepath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@
</ItemGroup>

<ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\Square150x150Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
<Content Include="Assets\StoreLogo.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
<Content Include="Examples\*.md">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Content Include="Assets\*.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Assets\*.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Examples\*.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

Expand Down
10 changes: 0 additions & 10 deletions Samples/WindowsCopilotRuntime/cs-winui/nuget.config

This file was deleted.

0 comments on commit 71a9d5b

Please sign in to comment.