Search code examples
c#uwptemplate10

How can I retrieve the ObserableCollection Object Passed, to be used in the SecondPage


Template10 Hamburger. How do I retrieve the Object to be used in the SecondPage. what would be the best way to achieve this.

I have tried to convert Object to a new collection but get this error: cannot convert from 'object' to 'System.Collections.Generic.IEnumerable'

MainPageViewModel.cs has a ObservableCollection ScoreData.

public class MainPageViewModel : ViewModelBase  
   public ObservableCollection<string> ScoreData { get; set; }  

   public MainPageViewModel()
    {List<string> Data = new List<string>();
        Data.Add("10");
        Data.Add("5");
        ScoreData = new ObservableCollection<string>(Data);

and I pass it to SecondPage with navigationService

    {NavigationService.Navigate(typeof(Views.SecondPage), ScoreData);}

SecondPageViewModel.cs

   public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode,
        IDictionary<string, object> suspensionState)
    {
        Value = (suspensionState.ContainsKey(nameof(Value)))
            ? suspensionState[nameof(Value)]?.ToString() : parameter?.ToString();
        await Task.CompletedTask;


        // ERROR
        //cannot convert from 'object' to 'System.Collections.Generic.IEnumerable<string>' 
        ObservableCollection<string> ScoreData1 = new ObservableCollection<string> (parameter);
    }

The Object is there with the items, I cant figure out how to use the object items in the SecondPage.xaml Textbox's. Trying to create a new observablecollection to do this gives me the error show above.

    Text="{x:Bind ViewModel.ScoreData1[0], Mode=OneWay}" />
    Text="{x:Bind ViewModel.ScoreData1[1], Mode=OneWay}" />

Solution

  •        try {
                var items=parameter as IEnumerable<Score>;
                foreach(var item in items)
                {
                    _score.Add(item);
    
                }
            }
            catch (Exception ex) {     
            }