簡體   English   中英

C#WPF帶有圖像的嵌套類的數組

[英]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,還是那么愚蠢?

  1. 有沒有更聰明的方式來做我想做的事情?

  2. 是否有參考可幫助我理解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";

非常感謝您對此的關注。

深層復制概念很有啟發性,可讓我步入正軌。 但是仍然沒有方法可以讓我修改ROM​​S [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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM