Juan Pablo Garcia Coello
Juan Pablo Garcia Coello

Reputation: 3232

Apply resource to SemanticZoom ZoomedInView throws Exception

I have simplified the case to the following:

<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>

And the event to follow the process:

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();
     }
 }

That throws an exception: System.ArgumentException Value does not fall within the expected range.

BUT If I remove the comments and I set a new Instance, that works. So what is the difference? Why I cannot apply a resource to the semanticzoom zoomedinview?

Upvotes: 1

Views: 71

Answers (1)

James He
James He

Reputation: 397

The problem happens on this part, instead of the applying the ListView/Gridview to the ZoomInView/ZoomOutView.

(ListView)this.Resources["TestList"]; 

I don't think we can put GirdView or ListView here because they are not shareable, see the "XAML resources must be shareable" section of the ResourceDictionary and XAML resource references, UIElement can never be shareable

Anyway, the best practice is not simply to replace the current GirdView with another existing one, but instead you should modify your binding to another data source.

    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;
        }
    }

Upvotes: 1

Related Questions