1
votes

Getting Error when Set the value to DefaultViewModel in async method

    async void SpotlightVideo_FadeIn_Completed(object sender, object e)
    {
        this.DefaultViewModel["SpotlightImageSource"] = (await ExtensionMethods.GetSingleImage(1, SpotlightVideo.Image)).Source;
        Spotlight_Videos s = this.DefaultViewModel["SpotlightVideos"] as Spotlight_Videos;
        //s.Discription = SpotlightVideo.Discription;
        //s.Image = SpotlightVideo.Image;
        //s.Video_Id = SpotlightVideo.Video_Id;
        //s.Video_Name = SpotlightVideo.Video_Name;
        //s.VideoPath = SpotlightVideo.VideoPath;

        this.DefaultViewModel["SpotlightVideos"] = SpotlightVideo;
        //s = SpotlightVideo;

        SpotlightVideo_FadeOut.Begin();
    }

the first line in the mehtod runs fine DefaultViewModel["SpotlightImageSource"] but this.DefaultViewModel["SpotlightVideos"] = SpotlightVideo; gives me error of:

"Value does not fall within the expected range."

but when I set values to individual properties it runs fine with no error.

When I pass whole object to s variable it runs wioth no error but didnt give result as expected.

I debuged the code and basicallty the error is generating from ObservableDictionary.cs

private void InvokeMapChanged(CollectionChange change, string key)
    {
        var eventHandler = MapChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new ObservableDictionaryChangedEventArgs(change, key));
        }
    }

The error line is:

eventHandler(this, new ObservableDictionaryChangedEventArgs(change, key)); 

Please Help me out.

1

1 Answers

0
votes

The question is pretty old but I ran into the same issue and the answer could help others.

It is because your object this.DefaultViewModel["SpotlightVideos"] expect a collection, but you give it a single object.

I don't see your XAML, but in my case, the DefaultViewModel item had a reference in a CollectionViewSource in <Page.Resources>section.