【发布时间】:2023-03-03 02:34:01
【问题描述】:
我在 WPF 应用程序中有一个 ListBox,我想在其中列出我存储在 Azure 中的所有 Blob。下面的代码是我目前正在尝试但没有成功。
来自 xaml.cs 的代码
public ObservableCollection<string> Blobs = new ObservableCollection<string>();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
const string StorrageAccountName = "****";
const string StorageAccountKey = "****==";
var storageAccount = new CloudStorageAccount(
new Microsoft.Azure.Storage.Auth.StorageCredentials(StorrageAccountName, StorageAccountKey), true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("****");
var description = string.Empty;
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
description = $"Block blob of length {blob.Properties.Length}: {blob.Uri}";
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item;
description = $"Page blob of length {pageBlob.Properties.Length}: {pageBlob.Uri}";
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory)item;
description = $"Directory: {directory.Uri}";
}
Blobs.Add(description);
}
}
代码从 .xaml 更改
<ListBox ItemsSource="{Binding Blobs}" HorizontalAlignment="Left" Height="323" Grid.RowSpan="3" VerticalAlignment="Top" Width="267" SelectionChanged="ListBox_SelectionChanged" Margin="0,-4,0,0" IsSynchronizedWithCurrentItem="True" />
【问题讨论】:
-
您不应该创建 blob 列表的可观察集合并将其绑定到列表框吗?您现在所做的只是在控制台上打印。
标签: c# visual-studio azure listbox azure-blob-storage