Xamarin使用.NET Standard 2.0时C#7.3不支持async streams如何解决
问题原因
错误触发是因为await foreach属于C# 8.0新增的异步流特性,你的Xamarin项目当前默认使用C# 7.3版本,所以触发编译报错,两种解决方案可选:
方案1:调整项目配置启用C# 8.0
netstandard2.0完全支持C# 8.0,只需要修改项目配置即可保留原有代码不变,操作步骤:
- 右键Xamarin共享类库项目,选择编辑项目文件
- 在顶层
<PropertyGroup>节点中添加如下配置:
<LangVersion>8.0</LangVersion>
保存后重新编译项目,原有代码即可正常运行。
方案2:修改代码兼容C# 7.3
如果不希望调整语言版本,可以将异步流枚举逻辑替换为低版本兼容的分页遍历写法,修改后的代码如下:
static async Task AddType() { string connectionString = "xxx"; // 创建BlobServiceClient对象用于创建容器客户端 BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); // 获取容器客户端对象 BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("raspberryuploads"); // 兼容C#7.3的Blob枚举逻辑 var pageEnumerator = containerClient.GetBlobsAsync().AsPages().GetAsyncEnumerator(); try { while (await pageEnumerator.MoveNextAsync()) { foreach (BlobItem blobItem in pageEnumerator.Current.Values) { BlobClient blobClient = new BlobClient("xxx", "yyy", blobItem.Name); IDictionary<string, string> metadata = new Dictionary<string, string>(); // 向字典中添加元数据键值对 metadata["Type"] = "Result"; // 设置Blob的元数据 await blobClient.SetMetadataAsync(metadata); } } } finally { await pageEnumerator.DisposeAsync(); } }
内容的提问来源于stack exchange,提问作者Fooourier
相关产品推荐
相关产品推荐

