在Windows中的metro风格应用中自动对齐瓷砖

时间:2012-07-06 09:51:02

标签: windows-8 windows-runtime winjs

我有一个都市风格的应用程序,在主页中有一些随机顺序的瓷砖。如果我删除其中一个瓷砖,则所有其他瓷砖应重新排列以填充通过移除单个瓷砖创建的间隙。您可以在Windows 8桌面视图中观察到相同的现象。是否可以使用WinJS或WinRT实现它。

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的需求,如果您使用绑定到IObservableCollection的ListView或GridView,则在删除/添加项目时,这些项目将自动适合。

使用 IObservableCollection (或 ObservableList )可以添加ChildrenTransitions(选择 AddRemoveTransitions 以自动添加“正确”动画以添加/删除项目。

我没有在JS / Html中测试过这个,但是在Xaml / C#中它运行得很好。

答案 1 :(得分:0)

您可以直接修改ListView数据源。

例如,

// create a reference of your data source so that it can be accessed
// when you need to modify it
var yourBindingList; 

...

// code where you bind your data to your list view
yourBindingList = WinJS.Binding.List([{id: 1, name: 'one'}, 
    {id: 2, name: 'two'}, {id: 3, name: 'three'}]);
var listViewControl = document.getElementById('yourListViewDiv').winControl;
WinJS.UI.setOptions(listViewControl, {
    selectionMode: 'single',
    itemDataSource: yourBindingList.dataSource,
    itemTemplate: yourItemTemplate, 
});

...

// code where you remove the first item in your list view
yourBindingList.splice(0, 1);

希望这有帮助。