列出WPF应用程序中ListBox中的Azure blob
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列出WPF应用程序中ListBox中的Azure blob相关的知识,希望对你有一定的参考价值。
我在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" />
答案
目前,您只是将描述写入控制台,而不是WPF视图。
您需要将描述添加到View可以绑定的ObservableCollection
将您的代码更改为
//This is the collection containing your descriptions
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}";
}
// add your descriptions to the collection
Blobs.Add(description);
}
}
并将ListControl更改为
<ListControl ItemsSource="{Binding Blobs}" />
以上是关于列出WPF应用程序中ListBox中的Azure blob的主要内容,如果未能解决你的问题,请参考以下文章
UI响应性并使用WPF中的“SelectedItem”ListView / ListBox