每次用户单击“新建”按钮时,我都试图在屏幕上放置一个图块。我的实现工作,但我无法让任何动态生成的图块(从“新”按钮单击事件创建)在应用程序关闭然后重新加载后持续存在。我不知道问题出在哪里,因为当我导航到应用程序中的其他页面然后返回 Tile 页面时,tile 仍然存在。我所拥有的如下:
TilePage.xaml.cs
public TilePage()
{
InitializeComponent();
CreateTileList(); //create main tile first always and check if others exist
}
private void CreateTileList()
{
tileItems = new ObservableCollection<TileItem>()
{
new TileItem() { ImageUri = mainImage, Title = "main", /*Notification = "",*/ Message = "main", GroupTag = "MainGroup", TileName = "main" },
};
//Set the first tile item
this.tileList.ItemsSource = tileItems; //sets the tileList Listbox ItemsSource to tileItems ObservableCollection
if (Settings.TileList.Value.Count > 0) //add other tiles accordingly
{
foreach (var existingItem in Settings.TileList.Value)
{
tileItems.Add(existingItem);
}
}
this.tileList.ItemsSource = tileItems;
}
void addNew_Click(object sender, EventArgs e)
{
BitmapImage newTileImage = new BitmapImage();
var newItem = new TileItem() { ImageUri = newTileImage, Title = "new", /*Notification = "",*/ Message = "new browser", GroupTag = "TileGroup", TileName = "new" };
tileItems.Add(newItem); //update UI immediately
Settings.TileList.Value.Add(newItem); //update saved TileList
}
我有一个 Settings.cs 类,用于通过另一个 Setting.cs 类将数据保存在隔离存储中
public class Settings
{
public static Setting<ObservableCollection<TileItem>> TileList = new Setting<ObservableCollection<TileItem>>("TileList", new ObservableCollection<TileItem>());
}
我总是在构造函数中调用该CreateTileList()
方法来生成第一个图块(无论如何它总是有效),然后检查用户是否创建了其他图块。如果是这样,我也尝试添加这些图块(仅在应用程序尚未关闭然后重新打开时才有效)。