[英]C# WPF Array of nested classes with image
我正在努力地教自己C#,并且长时间使用Google搜索并没有出现以下答案:
我上了Rom,Game和Art课程。 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;
}
}
我想创建一个Roms数组,并在一个循环中填充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.Source与变量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,还是那么愚蠢?
有没有更聪明的方式来做我想做的事情?
是否有参考可帮助我理解C#中无数图像类之间的关系?
对不起罗word的问题,谢谢。
我认为您应该对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作为基本“图像”,则可以调用bitmapImage的.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会发生什么,它们甚至都没有名称并且无法修改。 他们只是为了永恒而漂浮在记忆中,困扰着生活变量吗? 我还在做傻事吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.