为什么此代码不填充我的 GridView 图像/如何将嵌套图像填充到 GridView?

问题描述 投票:0回答:0

我正在尝试在我的 C# 代码中使用图像填充 GridView。 GridView 有一个包含 Grid 的数据模板,其中有一个 Image - 对于上下文,我这样做是因为我想在用户选择它而不是默认的 GridView 选择行为(不透明度)时在图像上显示颜色图像减少到 0.5 并且网格背景颜色显示出来)。

当我尝试在独立的 Image 对象(未嵌套在任何对象中并明确命名)上运行与此类似的代码时,它工作得很好。 但是当我尝试运行它来填充 GridView 中的图像时,填充了 Grid 对象,但没有填充其中的图像。

可能值得注意的是,如果我故意强制执行导致 catch 循环执行的条件(通常不会执行),同样的事情会发生(显示网格,但其中没有图像)。

我假设这可能与我将项目添加到 GridView、控件模板或这背后的 XAML 的方式有关,但我不确定。

Edit:经过更多实验后,我注意到图像源不是唯一被忽略的属性。例如,如果我在生成对象时更改网格名称或背景或图像名称,则这些更改均不适用 - 它始终使用来自控件的 DataTemplate 的信息,而不使用来自我添加的项目的任何信息(除了有一个项目的事实)。

这是代码隐藏。我没有包含在此之前或 WorkingImage 类的代码,因为它没有添加任何上下文 - WorkingImage 类包含一个 StorageFile“文件”变量,该变量在此处引用并存储了其他不相关的数据。

public async void AddImage(WorkingImage img)
    {
        BitmapImage bmp = new BitmapImage();
        Windows.UI.Xaml.Controls.Image i = new Windows.UI.Xaml.Controls.Image();
        try
        {
            FileRandomAccessStream stream = (FileRandomAccessStream)await img.file.OpenAsync(FileAccessMode.Read);
            await bmp.SetSourceAsync(stream);
            i.Source = bmp;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Failed to add image source" + ex.Message);
            i.Source = new BitmapImage(new Uri("ms-appx:///Assets/Resources/FailedImageRender.png"));
        }
        Grid g = new Grid();
            g.Children.Add(i);

        gridView.Items.Add(g);
    }

这里是自定义控件的 XAML。如果我在 XAML 的 DataTemplate 中设置 Source,图像确实显示可能毫无价值,但如果在代码隐藏中设置它不起作用,如上所示。

<UserControl
x:Class="MyProjectName.Classes.Controls.IUAreaExpander"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ViperContentManager.Classes.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Control.Resources>
    <ResourceDictionary>
        <SolidColorBrush x:Key="OverlayColor">#FFD0D0D0</SolidColorBrush>
    </ResourceDictionary>
</Control.Resources>

<controls:Expander
    x:Name="expander"
    ExpandDirection="Down"
    IsExpanded="True">

    <controls:AdaptiveGridView
        x:Name="gridView"
        DesiredWidth="140"
        IsItemClickEnabled="True"
        ItemHeight="140"
        SelectionMode="Extended"
        StretchContentForSingleRow="False">
        <controls:AdaptiveGridView.ItemTemplate>
            <DataTemplate x:DataType="Image">
                <Grid
                    Width="NaN"
                    BorderBrush="{StaticResource OverlayColor}"
                    BorderThickness="5,5,5,5">
                    <Image
                        x:Name="EquipImage"
                        Opacity="1"
                        Stretch="UniformToFill">
                    </Image>
                </Grid>
            </DataTemplate>
        </controls:AdaptiveGridView.ItemTemplate>
        <controls:AdaptiveGridView.ItemContainerStyle>
            <Style TargetType="GridViewItem">
                <Setter Property="Margin" Value="0.5" />
                <Setter Property="CornerRadius" Value="0" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="Background" Value="{StaticResource OverlayColor}" />
            </Style>
        </controls:AdaptiveGridView.ItemContainerStyle>
    </controls:AdaptiveGridView>
</controls:Expander>
c# user-interface gridview uwp uwp-xaml
© www.soinside.com 2019 - 2024. All rights reserved.