為什么其他人需要使用Current(如版本 A)?
// Version A
using var stream = await FileSystem.Current.OpenAppPackageFileAsync("monkeys.json");
// Version B
using var stream = await FileSystem.OpenAppPackageFileAsync("monkeys.json");
這是詳細資訊:
public interface IFileSystem
{
string CacheDirectory { get; }
string AppDataDirectory { get; }
Task<bool> AppPackageFileExistsAsync(string filename);
Task<Stream> OpenAppPackageFileAsync(string filename);
}
public static class FileSystem
{
public static string CacheDirectory { get; }
public static string AppDataDirectory { get; }
public static IFileSystem Current { get; }
public static Task<bool> AppPackageFileExistsAsync(string filename);
public static Task<Stream> OpenAppPackageFileAsync(string filename);
}
uj5u.com熱心網友回復:
這大概是這里發生的歷史遺留下來的一點點吧。從歷史上看,Xamarin.Essentials 沒有(很好)支持依賴項注入。
對于 .NET MAUI,我們確實添加了這一點,但是,我們也想確保不使用依賴注入的人仍然可以按照他們喜歡的方式使用它。這就是為什么所有 Essentials API 現在都有一個您可以訪問的Current或Default靜態實體,以及您可以與依賴項注入一起使用的介面版本。
在這個具體案例中,差異為零。見下文當時的出處FileSystem(省略無關代碼):
public static class FileSystem
{
public static Task<Stream> OpenAppPackageFileAsync(string filename)
=> Current.OpenAppPackageFileAsync(filename);
public static Task<bool> AppPackageFileExistsAsync(string filename)
=> Current.AppPackageFileExistsAsync(filename);
static IFileSystem? currentImplementation;
public static IFileSystem Current =>
currentImplementation ??= new FileSystemImplementation();
internal static void SetCurrent(IFileSystem? implementation) =>
currentImplementation = implementation;
}
在這里你可以看到FileSystem.OpenAppPackageFileAsyncjust 呼叫Current.OpenAppPackageFileAsyncunderneat
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/534224.html
標籤:C#赛马林毛伊岛
上一篇:發布時UWP打開pdf失敗
下一篇:如何獲取當前登錄用戶的UID
