0

我制作了一个包含许多图像的用户控件。但图像不显示。

如果我导航到直接在 xaml 中包含图像的其他页面(所有图像与源共享相同的 png),然后我导航回来并显示用户控件中的图像。

为什么?关于缓存的东西?

谢谢。

public partial class PatternView : UserControl
{
    List<List<Cube>> pattern;
    ObservableCollection<ObservableCollection<Image>> image;
    public PatternView()
    {
        InitializeComponent();
    }
    public void Init(int row, int col)
    {
        LayoutRoot.RowDefinitions.Clear();
        LayoutRoot.ColumnDefinitions.Clear();
        List<List<Cube>> a=new List<List<Cube>> ();
        ObservableCollection<ObservableCollection<Image>> b = new ObservableCollection<ObservableCollection<Image>>();
        for (int i = 0; i < row; i++)
        {
            LayoutRoot.RowDefinitions.Add(new RowDefinition());
            a.Add(new List<Cube>());
            b.Add(new ObservableCollection<Image>());
        }
        for (int i = 0; i < col; i++)
        {
            LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition());
        }
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                a[i].Add(new StoneCube());
                Image img = new Image() 
                { Source = a[i][j].icon };

                img.Source = new BitmapImage(new Uri("Resources/CubeImage/Stone.png", UriKind.Relative));
                b[i].Add(img);

                LayoutRoot.Children.Add(img);
                Grid.SetRow(img, i);
                Grid.SetColumn(img, j);
            }
        }
        pattern = a;
        image = b;
    }

“图像”中的所有元素都不会出现。但是,如果我转到另一个仅显示相同资源的图像的页面,然后返回,一切都很好。

public class Cube
{
    public string name { get; set; }
    public int type;
    public BitmapImage icon { get; protected set; }
    protected int rowSpan;
    protected int colSpan;

    public Cube() 
    {
        name = "basic Cube";
    }
    public Cube(int type)
    {
        name = "basic Cube";
        this.type = type;
    }
}
public class NullCube : Cube
{
    public NullCube() : base()
    {
        name = "null";
        icon = ProgramData.CubeImages["Null"];
    }
}
public class UsedCube : Cube
{
    public UsedCube() : base()
    {
        name = "used";
        icon = ProgramData.CubeImages["Null"];
    }
}
public class StoneCube : Cube
{
    public StoneCube() : base()
    {
        name = "stone";
        icon = ProgramData.CubeImages["Stone"];
    }
}
public class AmmoCube : Cube
{
    public AmmoCube() : base()
    {
        name = "ammo";
        icon = ProgramData.CubeImages["Ammo"];
    }
}

static class ProgramData
{
    public static Dictionary<string,BitmapImage> CubeImages = new Dictionary<string, BitmapImage>();
    public static ObservableCollection<Cube> ChooseCubeBar { get; set; }


    static ProgramData()
    {
        CubeImages.Add("Stone", new BitmapImage(new Uri("Resources/CubeImage/Stone.png", UriKind.Relative)));
        CubeImages.Add("Null", new BitmapImage(new Uri("Resources/CubeImage/Null.png", UriKind.Relative)));
        CubeImages.Add("Ammo", new BitmapImage(new Uri("Resources/CubeImage/Ammo.png", UriKind.Relative)));
        CubeImages.Add("Cannon", new BitmapImage(new Uri("Resources/CubeImage/Cannon.png", UriKind.Relative)));
    }
4

0 回答 0