【问题标题】:List Azure blobs in a ListBox in a WPF App在 WPF 应用程序的 ListBox 中列出 Azure Blob
【发布时间】: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


【解决方案1】:

目前,您只是将描述写入控制台,而不是 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}" />

【讨论】:

  • 感谢您的回复。我已经根据您的回答编辑了我的代码,但我无法让它工作。我错过了什么吗?
猜你喜欢
  • 2012-10-27
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
  • 2012-03-02
  • 2014-01-17
  • 2020-02-28
  • 2019-08-17
相关资源
最近更新 更多