我已将案例简化为以下内容:
<Page>
<Page.Resources>
<GridView x:Key="TestGrid"/>
<ListView x:Key="TestList"/>
</Page.Resources>
</Page>
<Grid >...
<VisualStateManager.VisualStateGroups >
<VisualStateGroup x:Name="VisualStateGroup" CurrentStateChanged="VisualStateGroup_CurrentStateChanged">
<VisualState x:Name="WideView">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="720" />
</VisualState.StateTriggers>
<VisualState.Setters>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="NarrowView">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<SemanticZoom...>
<SemanticZoom.ZoomedInView>
<GridView/>
</SemanticZoom.ZoomedInView>
<SemanticZoom.ZoomedOutView>
<GridView/>
</SemanticZoom.ZoomedOutView>
要遵循这个过程的事件:
private void VisualStateGroup_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
{
if (e.NewState.Name == "NarrowView")
{
ZoomView.ZoomedInView = (ListView)this.Resources["TestList"]; //new ListView();
}
else
{
ZoomView.ZoomedInView = (GridView)this.Resources["TestGrid"]; //new GridView();
}
}
抛出异常:System.ArgumentException Value不在预期范围内。
但是如果我删除了评论并设置了一个新的实例,那就可行了。那么区别是什么呢?为什么我不能将资源应用于semanticzoom zoomedinview?
答案 0 :(得分:1)
此问题发生在此部分,而不是将ListView / Gridview应用于ZoomInView / ZoomOutView。
(ListView)this.Resources["TestList"];
我认为我们不能在这里放置GirdView或ListView,因为它们不可共享,请参阅ResourceDictionary and XAML resource references的“XAML资源必须可共享”部分,UIElement永远不可共享
无论如何,最佳做法不仅仅是将当前的GirdView替换为另一个现有的GirdView,而是应该修改对另一个数据源的绑定。
private void VisualStateGroup_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
{
if (e.NewState.Name == "NarrowView")
{
//assign the new data source to ZoomedInView
(zoomview.ZoomedInView as ListView).ItemsSource = xxxxx;
}
else
{
//assign the new data source to ZoomedOutView
(zoomview.ZoomedOutView as GridView).ItemsSource = xxxxx;
}
}