-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blazor Hybrid: Static files topic #25674
Comments
@guardrex Is the ask here for documentation on how to access absolute file paths on the native file system, for all platforms? cc/ @danroth27 |
I wasn't told what the content would be. |
@danroth27 did you have a suggestion for what we wanted this issue to cover? |
Oh is this to be a MAUI version of https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files? |
If so on ...
@danroth27 ... general ❓ ... is the intention is to circle around later to address any code example deltas for WPF and WinForms? I understand that the concepts will the same (or similar), but devs prefer to see doc examples for the framework that they're working with. We found that out PDQ when RP was released and the docs were mostly MVC-based. |
The idea behind this topic is that static files are a bit different in Blazor Hybrid apps. In web apps static files are deployed to the server and made available over HTTP. In a Blazor Hybrid app, static files are deployed into the app as static assets. How those static assets get handled is different on every platform. From what I understand .NET MAUI does some work to try to abstract this with their MAUI assets stuff. Then there's the question of how should you refer to static assets in a Blazor Hybrid app. From what I understand you simply use a relative path, similar to what you would do in a web app. Maybe we also need to say something about the base address for the Blazor Hybrid app - not sure. So this doc is about explaining about how static assets in a native app are similar & different than in a web app, and link to the appropriate .NET MAUI content on static asset handling. |
For Blazor Hybrid with WinForms & WPF, we'll be using the native
@using System.Resources
@AssetText
@code {
public string AssetText { get; set; } = "Loading asset...";
protected override void OnInitialized()
{
var resources = new ResourceManager(typeof(INSERT_PROJECT_NAME_HERE.Properties.Resources));
var assetObject = resources.GetObject("MyAsset");
if (assetObject is null)
{
throw new KeyNotFoundException("'MyAsset' could not be found");
}
AssetText = (string)assetObject;
}
}
For Blazor Hybrid MAUI, we'll be using the @guardrex could you please create the appropriate docs page based on the content above? |
Here's the Blazor Hybrid specific sample: @using System.IO
@using Microsoft.Maui.Storage
<button @onclick="LoadMauiAsset">Load Asset</button>
<br />
@Contents
@code {
public string Contents = "Press 'Load Asset' to get started";
async Task LoadMauiAsset()
{
using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt");
using var reader = new StreamReader(stream);
Contents = reader.ReadToEnd();
}
} This reads the default Complete instructions in how to use |
@TanayParikh ... WRT WinForms/WPF: I understand that taking the Resource Manager route would be useful in localized content situations, but what about the proverbial low-hanging fruit approach for loading static assets for Razor components in the normal way out of
Output ... I also published this app and ran the executable out of the Is this valid? ... at all?? ... perhaps, as a Razor components only approach for a components-only section??? Docs for localized resources (i.e., Resource Manager) for WPF seem to be articles under https://docs.microsoft.com/dotnet/desktop/wpf/advanced/how-to-localize-an-application) ... especially this one 👉 https://docs.microsoft.com/dotnet/desktop/wpf/advanced/how-to-use-resources-in-localizable-applications, so I'll see if that's a good cross-link and if there are any deltas with it. BTW ... valid approach or not ... do you know why the text file in |
btw @TanayParikh ... I only mention that ☝️ wondering about it as an alternate approach (only for components). I'll put your steps into the PR as the primary, recommended approach. If what I hacked up is valid tho, should it be in a separate section further down the topic's content? |
Hummmm 🤔 ... IntelliSense can't find |
It's composing a hair different to what you posted (working with WinForms at the moment) ... I mean a
@using System.Resources
<p>@@dataResourceText: @dataResourceText</p>
@code {
public string dataResourceText = "Loading resource ...";
protected override void OnInitialized()
{
var resources = new ResourceManager("WinFormsBlazor.Form1", typeof(WinFormsBlazor.StaticFileExample).Assembly);
dataResourceText = resources.GetString("Data") ?? "'Data' could not be found";
}
} |
... and I think we can avoid the hoops and get the assembly with ye olde var resources = new ResourceManager("WinFormsBlazor.Form1", this.GetType().Assembly); |
WPF mirrored your code as ... @using System.Resources
<p>@@dataResourceText: @dataResourceText</p>
@code {
public string dataResourceText = "Loading resource ...";
protected override void OnInitialized()
{
var resources =
new ResourceManager(typeof(WpfBlazor.Properties.Resources));
dataResourceText = resources.GetString("Data") ?? "'Data' not found";
}
} |
@TanayParikh ... Ok ... good start today. I have the WinForms and WPF bits laid out in my draft locally. Besides my RAMBLINGS ON HERE ☝️ 🙈 , can you let me know if my earlier remarks ☝️ about the Razor component-only approach with On Wednesday morning, I'll add the .NET MAUI piece to the draft and polish the bits further from this morning. I plan to put the PR up tomorrow and ping u for review, but I might need one more day. UPDATE (5/18, Wednesday 5:30AM CST): Yes, I'll have the draft PR up in a couple of hours. I'm putting final touches on it now. If you don't have time to review it today (Wednesday), that will be fine with me ... sleeping on it 🛌💤 a night and editing it again on Thursday morning will be good for it. ... and BTW ... I do like the component-only static asset web root approach ( UPDATE (5/18, 8AM CST): The PR is up now. Tanay to review first. After Tanay's approval, I'll ping DR and Artak on and offline. |
Yep totally valid, we can definitely include this approach as well. Going the Resource route is just another option (as resources don't necessarily have to be used with localization (but often are).
Not really sure why that would be. |
When content is provided in an issue comment here, please remove the PU member assignment and assign the issue to @guardrex 🦖. PU member will be pinged on the PR when it goes up.
The text was updated successfully, but these errors were encountered: