Windows Store App: Calling Async Method at Design Time

100 views Asked by At

I'm experimenting with using d:DesignInstance to specify a fake data class to use for data binding at design time in a Windows Store app (win8.1).

I'm trying to initialize the fake data class from an embedded JSON file. However, it's only partly working. I call a LoadData() method from the constructor. This LoadData() is an async method, but it's called synchronously:

    public DesignTimePetitionInfo()
    {
        #pragma warning disable 4014
        LoadData();
        #pragma warning restore 4014
    }

    private async Task LoadData()
    {
        Uri dataUri = new Uri( "ms-appx:///DataModel/SampleData.json" );

        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync( dataUri );
        string jsonText = await FileIO.ReadTextAsync( file );
        JsonObject jsonObject = JsonObject.Parse( jsonText );

        URL = jsonObject.GetNamedString( "URL" );
        ChangeOrgID = Convert.ToInt32( jsonObject.GetNamedNumber( "ChangeOrgID" ) );
        Author = jsonObject.GetNamedString( "Author" );
        Content = jsonObject.GetNamedString( "Content" );

        JsonArray cities = jsonObject.GetNamedArray( "Communities" );

        foreach( JsonValue cityValue in cities )
        {
            JsonObject curCity = cityValue.GetObject();

            foreach( JsonValue sigValue in curCity.GetNamedArray( "Signatures" ) )
            {
                JsonObject sigObject = sigValue.GetObject();

                SignatureInfo curSig = new SignatureInfo( sigObject.GetNamedString( "Name" ),
                    sigObject.GetNamedString( "Reason" ) );

                CommunityInfo curComm = new CommunityInfo( sigObject.GetNamedString( "City" ),
                    sigObject.GetNamedString( "State" ), sigObject.GetNamedString( "Country" ) );

                CommunityInfo community = Communities.Where( c => c.CommunityID.Equals( curComm.CommunityID ) ).FirstOrDefault();
                if( community == null )
                {
                    community = curComm;
                    Communities.Add( community );
                }

                community.Signatures.Add( curSig );
            }
        }
    }

Interestingly, Communities gets populated in the designer window, so it looks like retrieving the Json file is working. However, the individual properties (e.g., URL) don't get set and their corresponding designer TextBlocks are empty.

But if I add the line

Author = "Some Name";

immediately after the call to LoadData() in the constructor the designer is updated to show "Some Name".

Is their a limitation on calling async methods at design time? Is there another way to access the embedded JSON file at design time?

0

There are 0 answers