Search code examples
windows-phone-8tileargumentnullexception

Windows Phone link from Tile error


I have a list of theaters and I created a secondary tile from my application to navigate directly to specific theater. I pass the id of the theater in query string :

I load the theaters from a WCF service in the file "MainViewModel.cs"

In my home page, I have a list of theaters and I can navigate to a details page.

But when I want to navigate from the tile, I have an error...

The Tile :

ShellTile.Create(new Uri("/TheaterDetails.xaml?selectedItem=" + theater.idTheater, UriKind.Relative), tile, false);

My TheaterDetails page :

 public partial class TheaterDetails : PhoneApplicationPage
{

    theater theater = new theater();



    public TheaterDetails()
    {
        InitializeComponent();

    }


    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();

        }


        if (DataContext == null)
        {

            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                int index = int.Parse(selectedIndex);

                    theater = (from t in App.ViewModel.Theaters
                               where t.idTheater == index
                               select t).SingleOrDefault();

                    DataContext = theater;

....
....
....

The error :

https://dl.dropboxusercontent.com/u/9197067/error.png

Like if the data were not loaded...

Do you have an idea where the problem come from ?

The solution could be easy but I am a beginner... Maybe it's because I load the data asynchronously and the application doesn't wait until it's done...

Thanks

EDIT :

My LoadData() method :

 public void LoadData()
    {

        client.GetTheatersCompleted += new EventHandler<ServiceReference1.GetTheatersCompletedEventArgs>(client_GetTheatersCompleted);
        client.GetTheatersAsync();

 // Other get methods...

        this.IsDataLoaded = true;
        }


private void client_GetTheatersCompleted(object sender,       ServiceReference1.GetTheatersCompletedEventArgs e)
    {
        Theaters = e.Result;
    }

Solution

  • So the solution that I found, thanks to Servy in this post : Using async/await with void method

    I managed to use async/await to load the data.

    I replaced my LoadData() method by :

        public static Task<ObservableCollection<theater>> WhenGetTheaters(ServiceClient client)
        {
            var tcs = new TaskCompletionSource<ObservableCollection<theater>>();
            EventHandler<ServiceReference1.GetTheatersCompletedEventArgs> handler = null;
            handler = (obj, args) =>
            {
                tcs.SetResult(args.Result);
                client.GetTheatersCompleted -= handler;
            };
            client.GetTheatersCompleted += handler;
            client.GetTheatersAsync();
            return tcs.Task;
        }
    
    
    
        public async Task LoadData()
        {
    
            var theatersTask = WhenGetTheaters(client);
            Theaters = await theatersTask;
    
    
            IsDataLoaded = true;
    
    
        }
    

    And in my page :

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
               await App.ViewModel.LoadData();
            }