“ FileSystem” 在. net maui 9 预览闪光混合“ Web App” 项目上不工作
原标题:"FileSystem" doesn t work on .net maui 9 preview blazor hybrid "Web App" project
SCENARIO
I am using .Net Maui 9.0 Preview, Blazor Hybrid Web App.
I need to initiate some code before the project starts (or in parallel) but it must display on home page after fetching it from some files.
This code must work as Maui App (for Android, iOS, Mac) and as a Web App.
That is why, I am initiating the services in the MainLayout.razor.cs ( I added MainLayout.razor.cs) file, because I didn t find any where else to start (entrypoint to the apps)!?!
THE PROBLEM
Couple of tasks which must be completed first, It is working fine on Maui Apps side. But it gives an error when running it on Web App.
ERROR
Microsoft.Maui.ApplicationModel.NotImplementedInReferenceAssemblyException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.
(Full Log is attached) ErrorLog.txt
Full Code (solution) is attached MauiApp5.zip
CODE
public class MyFileAccess
{
private string jsonDataFileName = "Resources/Raw/languages.json";
public async Task> ReadJsonDataAsync()
{
ObservableCollection? genericTypeList = new();
Debug.WriteLine(FileSystem.AppDataDirectory);
var isExist = await FileSystem.Current.AppPackageFileExistsAsync(jsonDataFileName);
if (isExist)
{
var stream = await FileSystem.Current.OpenAppPackageFileAsync(jsonDataFileName);
var reader = new StreamReader(stream);
var contents = await reader.ReadToEndAsync();
genericTypeList = JsonSerializer.Deserialize>(contents);
}
return genericTypeList!;
}
}
The documentation link suggests that it is part of Microsoft.Maui.Storage (and includes Maui.Essentials). Not sure what (which assembly) am I missing?
问题回答
@wafers Microsoft.Maui.Storage will work on apps installed on computers, but I do not think it will work in a website. Websites are not allowed to access the user s filesystem.
You could just add a class that return an array of Language Objects. Put this service in the Shared Project Services folder. Then you can call it from all projects anywhere anytime.
using System.ComponentModel.DataAnnotations;
namespace MauiApp5.Shared.Services
{
public static class LanguageService
{
public static List
GetLanguages()
{
var languages = new List();
var li1 = new Language { LanguageId = 1, Name = "Albanian", TextAlignment = "Left" };
var li2 = new Language { LanguageId = 2, Name = "English", TextAlignment = "Left" };
var li3 = new Language { LanguageId = 3, Name = "Persian", TextAlignment = "Right" };
languages.Add(li1);
languages.Add(li2);
languages.Add(li3);
return languages;
}
}
public class Language : IEquatable, IComparable
{
[Required]
public int? LanguageId { get; set; }
[Required]
public string? Name { get; set; } = string.Empty;
[Required]
public string? TextAlignment { get; set; } = string.Empty;
public override bool Equals(object? obj)
{
if (obj == null) return false;
Language? objAsLanguage = obj as Language;
if (objAsLanguage == null) return false;
else return Equals(objAsLanguage);
}
public int SortByNameAscending(string name1, string name2)
{
return name1.CompareTo(name2);
}
// Default comparer for Language type.
public int CompareTo(Language? compareName)
{
// A null value means that this object is greater.
if (compareName == null)
return 1;
else
return Name!.CompareTo(compareName.Name);
}
public bool Equals(Language? other)
{
if (other == null) return false;
return Name!.Equals(other.Name);
}
public override int GetHashCode()
{
throw new NotImplementedException();
}
}
Here is the Home page using the LanguageService
@page "/"
@using MauiApp5.Shared.Services
Home
Hello, Languages!
@if(Languages != null)
{
foreach(var language in Languages)
{
@language.LanguageId
@language.Name
@language.TextAlignment
}
}
@code {
protected List Languages { get; set; }
protected override void OnInitialized()
{
Languages = LanguageService.GetLanguages();
base.OnInitialized();
}
}
相关问题
What is the use of `default` keyword in C#?
What is the use of default keyword in C#?
Is it introduced in C# 3.0 ?
Anyone feel like passing it forward?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs.
...
NSArray s, Primitive types and Boxing Oh My!
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits.
Now here s the question: I feel really inclined to create some type of ...
C# Marshal / Pinvoke CBitmap?
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class.
My import looks like this:
[DllImport(@"test.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr ...
ADO.NET Entity Framework Association of Entities by Value Range
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber.
I want to create a many to many association ...
How to Use Ghostscript DLL to convert PDF to PDF/A
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
What is the most efficient keyvalue pair for ordering?
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Linqy no matchy
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control.
...