将资源应用于 SemanticZoom ZoomedInView 抛出异常
Apply resource to SemanticZoom ZoomedInView throws Exception
我已将案例简化为以下内容:
<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值不在预期范围内。
但是如果我删除评论并设置一个新的实例,那就行得通了。那么区别是什么呢?为什么我不能将资源应用到 semanticzoom zoomedinview?
问题发生在这部分,而不是将 ListView/Gridview 应用到 ZoomInView/ZoomOutView。
(ListView)this.Resources["TestList"];
我认为我们不能将 GirdView 或 ListView 放在这里,因为它们不可共享,请参阅 ResourceDictionary and XAML resource references 的 "XAML resources must be shareable" 部分,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;
}
}
我已将案例简化为以下内容:
<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值不在预期范围内。
但是如果我删除评论并设置一个新的实例,那就行得通了。那么区别是什么呢?为什么我不能将资源应用到 semanticzoom zoomedinview?
问题发生在这部分,而不是将 ListView/Gridview 应用到 ZoomInView/ZoomOutView。
(ListView)this.Resources["TestList"];
我认为我们不能将 GirdView 或 ListView 放在这里,因为它们不可共享,请参阅 ResourceDictionary and XAML resource references 的 "XAML resources must be shareable" 部分,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;
}
}