【问题标题】:WPF - Windows Media Import Importable Item List View Data BindingWPF - Windows Media 导入可导入项列表视图数据绑定
【发布时间】:2021-10-19 16:39:43
【问题描述】:

我以Microsoft Docs import-media-from-a-device 为例,在 WPF 应用程序中使用 Windows Media Import API。我现在几乎让它工作了,但还没有让数据绑定工作来提供可导入项目的列表,所以对此的任何帮助将不胜感激。源码可以下载here.

XAML 主窗口

enter<Window x:Class="ImportProofOfConcept.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ImportProofOfConcept"
    mc:Ignorable="d"
    Title="Media Import" Height="450" Width="800" Loaded="Window_Loaded">
<Grid Margin="6">
    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="*"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.ColumnSpan="3">
        <Label  Content="Select the required source to import photos and videos from and then select the required files and click OK."/>
        <Label  Content="Source:"/>
        <ComboBox x:Name="CboSources" SelectionChanged="CboSources_SelectionChanged"/>
        <Label  Content="Files:"/>
    </StackPanel>
    <ListView Grid.Row="1" Grid.ColumnSpan="3" MinHeight="10"  VerticalAlignment="Stretch" x:Name="fileListView" 
                HorizontalAlignment="Left" Margin="10,11,0,21" 
                Width="756" 
                BorderBrush="#FF858585"   
                BorderThickness="1" 
                ScrollViewer.VerticalScrollBarVisibility="Visible">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.05*"/>
                        <ColumnDefinition Width="0.20*"/>
                        <ColumnDefinition Width="0.75*"/>
                    </Grid.ColumnDefinitions>
                    <CheckBox Grid.Column="0" IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                    <Image Grid.Column="1" Source="{Binding Thumbnail}" Width="120" Height="120" Stretch="Uniform"/>
                    <TextBlock Grid.Column="2" Text="{Binding Name}" VerticalAlignment="Center" Margin="10,0"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Grid.Row="2" Grid.Column="1" Click="ButtonOK_Click" IsDefault="True" Margin="6">OK</Button>
    <Button Grid.Row="2" Grid.Column="2" Click="ButtonCancel_Click" IsCancel="True" Margin="6">Cancel</Button>
    <ProgressBar Grid.Row="3"  Grid.ColumnSpan="3" x:Name="progressBar" SmallChange="0.01" LargeChange="0.1" Maximum="1"/>
</Grid>
代码背后
 private async void FindItems()
    {
        this.cts = new CancellationTokenSource();

        try
        {
            this.importSession = this.importSource.CreateImportSession();

            // Progress handler for FindItemsAsync
            var progress = new Progress<uint>((result) =>
            {
                System.Diagnostics.Debug.WriteLine(String.Format("Found {0} Files", result.ToString()));
            });

            this.itemsResult =
                await this.importSession.FindItemsAsync(PhotoImportContentTypeFilter.ImagesAndVideos, PhotoImportItemSelectionMode.SelectAll)
                .AsTask(this.cts.Token, progress);

            // GeneratorIncrementalLoadingClass is used to incrementally load items in the Listview view including thumbnails
            this.itemsToImport = new GeneratorIncrementalLoadingClass<ImportableItemWrapper>(this.itemsResult.TotalCount,
            (int index) =>
            {
                return new ImportableItemWrapper(this.itemsResult.FoundItems[index]);
            });

            // Set the items source for the ListView control
            this.fileListView.ItemsSource = this.itemsToImport;

            // Log the find results
            if (this.itemsResult != null)
            {
                var findResultProperties = new System.Text.StringBuilder();
                findResultProperties.AppendLine(String.Format("Photos\t\t\t :  {0} \t\t Selected Photos\t\t:  {1}", itemsResult.PhotosCount, itemsResult.SelectedPhotosCount));
                findResultProperties.AppendLine(String.Format("Videos\t\t\t :  {0} \t\t Selected Videos\t\t:  {1}", itemsResult.VideosCount, itemsResult.SelectedVideosCount));
                findResultProperties.AppendLine(String.Format("SideCars\t\t :  {0} \t\t Selected Sidecars\t:  {1}", itemsResult.SidecarsCount, itemsResult.SelectedSidecarsCount));
                findResultProperties.AppendLine(String.Format("Siblings\t\t\t :  {0} \t\t Selected Sibilings\t:  {1} ", itemsResult.SiblingsCount, itemsResult.SelectedSiblingsCount));
                findResultProperties.AppendLine(String.Format("Total Items Items\t :  {0} \t\t Selected TotalCount \t:  {1}", itemsResult.TotalCount, itemsResult.SelectedTotalCount));
                System.Diagnostics.Debug.WriteLine(findResultProperties.ToString());
            }

            if (this.itemsResult.HasSucceeded)
            {
                // Update UI to indicate success
                System.Diagnostics.Debug.WriteLine("FindItemsAsync succeeded.");
            }
            else
            {
                // Update UI to indicate that the operation did not complete
                System.Diagnostics.Debug.WriteLine("FindItemsAsync did not succeed or was not completed.");
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Photo import find items operation failed. " + ex.Message);
        }


        this.cts = null;
    }

【问题讨论】:

    标签: wpf windows-runtime


    【解决方案1】:

    从 XAML 标记中的绑定路径中删除“ImportableItem”:

    <ListView Grid.Row="1" Grid.ColumnSpan="3" MinHeight="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="LstItems">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.05*"/>
                        <ColumnDefinition Width="0.20*"/>
                        <ColumnDefinition Width="0.75*"/>
                    </Grid.ColumnDefinitions>
                    <CheckBox Grid.Column="0" IsChecked="{Binding IsSelected, Mode=TwoWay}" Click="CheckBox_Click"/>
                    <TextBlock Grid.Column="2" Text="{Binding Name}" VerticalAlignment="Center" Margin="10,0"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    Image元素中显示缩略图时,需要转换返回的IRandomAccessStreamReference

    Bind XAML Image to IRandomAccessStreamReference

    【讨论】:

    • 所以你的代码没有事件编译...?显然,您需要定义或导入您正在使用的类型。
    • 现在差不多了...现在修复了引用并且正在编译代码。仍在解决剩余问题:- 抛出异常:PresentationFramework.dll 中的“System.InvalidOperationException”照片导入查找项目操作失败。在使用 ItemsSource 之前,项目集合必须为空。
    • DataTemplate 在 XAML 中位于 &lt;ListView.ItemTemplate&gt; 内部,而不是直接在 &lt;ListView 内部。
    • 请看我的编辑。
    • 使用最新的 XAML,我不再在 PresentationFramework.dll 中获得 System.InvalidOperationException。但遗憾的是,可导入项目 FileListView 中仍然没有出现任何内容。您能看到我缺少的其他任何东西来使数据绑定工作吗?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    相关资源
    最近更新 更多