C#WPF带有图像的嵌套类的数组

本文关键字:嵌套 数组 图像 C#WPF | 更新日期: 2023-09-27 18:21:23

我正在努力自学C#,长时间的谷歌搜索都没有找到这个答案:

我有一个类Rom,游戏和艺术。Rom.Game.Art是用于图像的,定义如下:

public class art
{
    public Image Screen { get; set; }
    public Image Marquis { get; set; }
    public Image Logo { get; set; }
    public art ()
    {
        BitmapImage Default_image = new BitmapImage();
        Default_image.BeginInit();
        Default_image.UriSource = new Uri(@"C:'Users'Major Major'Documents'Visual Studio 2013'Projects'Robin'Robin'images'bee_ace.png", UriKind.Absolute);
        Default_image.EndInit();
        this.Screen = new Image();
        this.Screen.Source = Default_image;
        this.Marquis = new Image();
        this.Marquis.Source = Default_image;
        this.Logo = new Image();
        this.Logo.Source = Default_image;
    }
}

我想创建一个罗姆数组,并在循环中填充DataTable中的许多嵌套属性和子类。不过,在进入循环之前,我只想让一块测试代码开始工作。以下操作很好:

        InitializeComponent();
        BitmapImage Image_0 = new BitmapImage();
        Image_0.BeginInit();
        Image_0.UriSource = new Uri(@"E:'Arcade'Art'Box'Intellivision'Congo Bongo.jpg", UriKind.Absolute);
        Image_0.EndInit();
        ROMS.Insert(0, new Rom());
        ROMS[0].Name = "Congo Bongo";
        ROMS[0].Game.Art.Screen.Source = Image_0;
        ROMS[0].Game.Date = "1983";
        ROMS[0].Game.Platform = "Intellivision";
        ROM_list.ItemsSource = ROMS;

我的抱怨是,如果我把它放在一个循环中,数组中的所有图像都将是相同的——它们将是最后输入的图像。似乎表达式"ROMS[0]。Game.Art.Screen.Source=Image_0;"将ROMS[0]、Game.Art.Screen.Ssource与变量Image_0联系在一起,而不仅仅是传递值(这对我来说似乎是一种令人惊讶的行为,但不用担心)。因此,为了继续填充ROMS[j].Game.Art.Screen.Source,我必须创建一个与ROM阵列分离的BitmapImages阵列作为参考。我更喜欢的是在数组中实例化(?)BitmapImages,如下所示:

        ROMS[0].Game.Art.Screen.Source = new BitmapImage();
        InitializeComponent();
        ROMS[0].Game.Art.Screen.Source.BeginInit();
        ROMS[0].Game.Art.Screen.Source.UriSource = new Uri(@"E:'Arcade'Art'Box'Intellivision'Congo Bongo.jpg", UriKind.Absolute);
        ROMS[0].Game.Art.Screen.Source.EndInit();

这行不通。Rom.Game.Art.Screen.Source不是BitMapImage,我找不到为其分配图像的方法。

所以我的问题,特别是:1.有没有一种方法可以将图像源分配给ROMS[0]。Game.Art.Screen.source,而不创建单独存在的BitmapImage,或者这很愚蠢?

  1. 有没有更聪明的方法来做我想做的事?

  2. 有没有参考资料可以帮助我理解C#中无数图像类之间的关系?

很抱歉问了这个冗长的问题,谢谢。

C#WPF带有图像的嵌套类的数组

我认为您应该对DataBinding进行一些审查。你会节省更多的时间。

你只需要创建一些类似模型的东西:

ROM.Game.Art.Screen.Path="meuPath.Jpg";

myItemSource = ROMS;

最后,做一些类似的事情:

            <ItemsControl x:Name="myItemSource">
                    <TextBlock Text="{Binding ROM.Game.Art.Screen.Name}"/>
                    <Image Source="{Binding ROM.Game.Art.Screen.Path,Converter=MyConverterImageToPath"/>
            </ItemsControl>

您需要为每个数组成员创建一个新的BitMapImage实例。如果将对象分配给变量,C#将使用引用。

ROMS[0].Game.Art.Screen.Source = Image_0;

在这里,您正在创建对Image_0的引用,如果您现在执行以下操作。。。

ROMS[1].Game.Art.Screen.Source = Image_0;

roms[0]和roms[1].screen.source引用Image_0。如果你现在这样做:

ROMS[0].Game.Art.Screen.Source.UriSource = new Uri(@"E:'Arcade'Art'Box'Intellivision'Congo Bongo.jpg", UriKind.Absolute);

更新Image_0,并且所有rom.screen.source都引用Image_0并更新

如果您想使用Image_0作为所有对象的基本"图像",您可以调用位图图像的.Clone()函数,该函数将创建对象的深度副本(一个新对象,而不是引用)。

所以在一开始的某个地方:

InitializeComponent();
BitmapImage Image_0 = new BitmapImage();
Image_0.BeginInit();
Image_0.UriSource = new Uri(@"E:'Arcade'Art'Box'Intellivision'Congo Bongo.jpg", UriKind.Absolute);
Image_0.EndInit();

在你的循环中,你有

ROMS[i].Name = "Congo Bongo";
ROMS[i].Game.Art.Screen.Source = Image_0.Clone();
ROMS[i].Game.Date = "1983";
ROMS[i].Game.Platform = "Intellivision";

非常感谢您的关注。

深度复制的概念很有启发性,让我走上了正轨。但是仍然没有方法可以让我修改ROMS[i].Game.Art.Screen.Source(这个是Image.Source的类)。有趣的是,我可以直接将BitmapImage分配给Image.Source,但我不能通过类嵌套来更改BitmapImage。换句话说,

ROMS[1]。Game.Art.Screen.Source.UriSource

不存在,即使ROMS[1].Game.Art.Screen.Source被设置为等于具有.UriSource.的BitmapImage

因此,以您的注释为起点,我提出了以下内容,这些内容将在循环中工作,显然循环变量将取代每个字符串:

        InitializeComponent();
        ROMS.Insert(0, new Rom());
        ROMS.Insert(1, new Rom());
        ROMS[0].Name = "Congo Bongo";
        ROMS[0].Game.Art.Screen.Source = new BitmapImage(new Uri(@"E:'Arcade'Art'Box'Intellivision'Congo Bongo.jpg", UriKind.Absolute));
        ROMS[0].Game.Date = "1983";
        ROMS[0].Game.Platform = "Intellivision";
        ROMS[1].Name = "Donkey Kong";
        ROMS[1].Game.Art.Screen.Source = new BitmapImage(new Uri(@"E:'Arcade'Art'Box'Intellivision'Donkey Kong.jpg", UriKind.Absolute));
        ROMS[1].Game.Date = "1982";
        ROMS[1].Game.Platform = "Intellivision";
        ROM_list.ItemsSource = ROMS;

尽管如此,每次修改Game.Image.Source时,我都必须创建一个新的BitMapImage和Uri。我想知道旧的BitMapImage和Uri会发生什么,它们甚至没有名称,也无法修改。它们只是在记忆中永远漂浮,萦绕在生活的变量中吗?我还在做傻事吗?