locked
Binding ListView to collection from from C# WinRT component

    Question

  • I have a simple method in C# that returns a list of objects like this:

     
           public IList<Object> GetTasks()
    
            {
                var tasks = new System.Collections.Generic.List<Object>();
                for (int i = 0; i < 5; i++)
                {
                    var t = new MyTask  { name = "task " + i, projectName="project " + i, projectId=i };
                    tasks.Add(t);
    
                }
    
                return tasks;
           }

    I call this in my javascript and bind it to a ListView but nothing shows up...  If I create the same list in javascript and bind it, it works!!!  I read somewhere that you don't support binding to winrt objects in javascript - is that what is going on here?


    www.emadibrahim.com

    Wednesday, May 9, 2012 2:06 AM

Answers

  • Hi Emad,

    For example in your template you would do something like this:

        <div class="itemtemplate" data-win-control="WinJS.Binding.Template">
            <div class="item-overlay">
                <h4 class="item-title" data-win-bind="textContent: title WinJS.Binding.oneTime"></h4>
                <h6 class="item-subtitle win-type-ellipsis" data-win-bind="textContent: subtitle WinJS.Binding.oneTime">
                </h6>
            </div>
        </div>
    
    -Jeff

    Jeff Sanders (MSFT)

    Wednesday, May 9, 2012 2:45 PM
    Moderator

All replies

  • In your application, try setting the binding to onetime binding.  Let me know if that fixes your issue!

    Jeff Sanders (MSFT)

    Wednesday, May 9, 2012 1:15 PM
    Moderator
  • how and where do you do that in Javascript?

    www.emadibrahim.com

    Wednesday, May 9, 2012 1:16 PM
  • Hi Emad,

    For example in your template you would do something like this:

        <div class="itemtemplate" data-win-control="WinJS.Binding.Template">
            <div class="item-overlay">
                <h4 class="item-title" data-win-bind="textContent: title WinJS.Binding.oneTime"></h4>
                <h6 class="item-subtitle win-type-ellipsis" data-win-bind="textContent: subtitle WinJS.Binding.oneTime">
                </h6>
            </div>
        </div>
    
    -Jeff

    Jeff Sanders (MSFT)

    Wednesday, May 9, 2012 2:45 PM
    Moderator
  • Thanks a lot that worked...  My other somewhat unrelated questions is how the heck would have I ever figured this out...  I know there is documentation and forums but why is it so hard to do a lot of this stuff???

    Do you have any recommended resource for working with WinRT from Javascript?


    www.emadibrahim.com

    Wednesday, May 9, 2012 3:23 PM
  • Hi Emad,

    Your best best is to ask questions here in the Forums if you run up against a problem that is not covered in the Quickstarts or samples.  Unfortunately all of the documentation has not been finished yet (sorry).  The writers are hard at work however to get updates and more samples code developed!

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, May 9, 2012 5:01 PM
    Moderator
  • This is a related question...  I can now bind and it is working but when I try to refresh the data i.e. retrieve the data again using the c# winRT component, the list view does not refresh...  I can see javascript retrieve the code and bind the list view but nothing happens.  

    How do I tell the listview that the underlying data changed and it needs to refresh?


    www.emadibrahim.com

    Wednesday, May 9, 2012 10:39 PM
  • Hi Emad,

    Good question.  I had assumed you were using a static data source.  I don't play in the C# world much but I believe if you use IList as your element (or IList<T>) changes should be reflected do the listview.  I did a quick look for a sample but I did not find any.  I will keep looking however for you.

    Update:  You want to use an Observable Collection.  Check out this thread:

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/aec84969-0947-4fe8-bcf6-ab533e7ab894

    -Jeff


    Jeff Sanders (MSFT)


    Thursday, May 10, 2012 8:02 PM
    Moderator
  • Hi,

    I have a WinMD component which I am calling from a JavaScript Metro App. The component has a method 

      public IAsyncOperation<SomeObject> DoSomethingAsync(string url, IList<Credentials> credentials)
            {//does something}

    How do I create a list of credentials from JavaScript .. I have tried an array but get an error

    var credentials=new Array();
    var credential = new Credentials("user","password");
    credentials[0] = credential;

    Thanks



    • Edited by timbie Friday, July 27, 2012 3:02 PM
    • Proposed as answer by timbie Monday, July 30, 2012 7:40 AM
    Friday, July 27, 2012 3:01 PM
  • Hi,

    If you pass IEnumerable rather than IList it works


    Monday, July 30, 2012 7:41 AM