我制作了一个由许多图像组成的用户控件。但图像没有显示出来。
如果我导航到其他一些直接在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)));
}