我正在嘗試使用GridView
而不是HubSection
從嘗試實現Transform3DAnimations
的平臺樣本實現的UWP應用程序。除了取得GridViewItem
之外,我已經明白了。下面的代碼是從上下文中的示例到hubsection。從代碼隱藏的GridView中獲取gridViewItem UWP
private void UpdateRandomSection()
{
// Updating the section triggers a cool animation!
// See SectionView.xaml and SectionView.xaml.cs
var sectionsInView = HeadlinesHub.SectionsInView;
var sectionsCount = sectionsInView.Count;
if (sectionsCount > 0)
{
var sectionToUpdate = sectionsInView[_random.Next(sectionsCount)];
sectionToUpdate.DataContext = new HeroArticlesViewModel();
}
}
我試圖獲取GridViewItem
,但我無法獲取它總是返回我GridViewItem
的數據模型GridViewItem
。如何從GridView
獲得GridViewItem
?我的代碼如下:
private Random InAppLiveTileRandomTileNumberGenerator;
private void UpdateRandomSection()
{
var sectionsInView = AllDevicesGridView.Items;
var sectionsCount = sectionsInView.Count;
if (sectionsCount > 0)
{
var sectionToUpdate = (GridViewItem)AllDevicesGridView.Items[InAppLiveTileRandomTileNumberGenerator.Next(sectionsCount)]; //Invalid Cast exception here
sectionToUpdate.DataContext = new InappLiveTileViewModelModel();
}
}
Solution I tried from stack answers
在示例中,他們使用該方法獲取hubsection當前視圖中的項目,然後獲取當前視圖中隨機對象的容器,然後更改數據上下文。我不想獲取有選定事件或已被選擇的對象的容器。在計時器滴答聲上,我想獲取gridview的當前視圖中的項目,然後從它們中獲取一個隨機數並獲取它的容器(類型爲gridviewitem),然後更新它的數據上下文。能否請你幫忙。對不起,我正在使用移動應用程序 –
我試過了你的代碼,var container = AllDevicesGridView.ContainerFromItem(AllDevicesGridView.Items [1]);'確實返回了一個容器,但該容器的數據上下文爲空。我錯過了什麼嗎? –
@AdityaSharma,如果您需要獲取或更改項目的數據上下文,您可能需要從'GridViewItem'獲取'ListViewItemPresenter'對象。我將更新代碼以獲取數據上下文。 –