locked
Add listbox items RRS feed

  • Question

  • Please help me!!!

    I'm a rather newbie in Silverlight. My biggest problem at the moment is how to populate a listbox using javascript.

    Found something on the internet about CreateFromXaml (msdn.microsoft.com/en-us/library/bb980072(v=vs.95).aspx) but even the examples ther raise an exception.

    My idea how it should be possible (but isn't). lb is the Xaml -name of the listbox.

           function doit(sender, args) {

                var control = sender.getHost();

                var lb = sender.findName("lb"); 

              var xamlFragment = '<ListBoxItem Content="some text"/>'

                var textBlock = control.content.createFromXaml(xamlFragment);

                lb.children.add(textBlock);

            }

    The exeption is caused by the last line of code. What's the point I'm missing?

    Tuesday, May 19, 2015 8:34 PM

Answers

All replies

  • I don't follow what you're doing there.
    Why would you want to populate a Silverlight listbox from javascript at all?

    The way to do it is using a c# or VB method in your Silverlight application and call that using the html bridge.

    But usually your entire page is full of the Silverlight application so maybe what you should explain what you're trying to achieve.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    Wednesday, May 20, 2015 7:52 AM
  • The reason why asked this question is a simple one. I'm developing 3D models in Google Sketchup. The platform I'm using is Windows. Under these circumstances is Internet Explorer required to create dialogswindows. Basicly I need a HTMLpage and a few lines of Javascript to perform the communication between such dialog and the Sketchup model.

    At the moment that the HTMLpage is needed there's a lot of information in the model. This information is transferred to the HTMLpage just after the page is loaded (<body onload = ...>).

    Impressed by the possibilities that Silverlight offers I thought is was a nice challenge to use Visual Studio and Silverlight to create the HTMLpage. Just as a way of learning.


    Wednesday, May 20, 2015 9:09 AM
  • Ok.

    You can communicate both ways Sliverlight > JavaScript and JavaScript > Silverlight using the html bridge.  Calling methods/functions.

    You could also alternatively share a cookie.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    Wednesday, May 20, 2015 10:16 AM
  • Hi Andy and others,

    Thanks for your reply.

    I've made een extra class 

      Imports System.Windows.Browser 

    <ScriptableType()>

      Public Class Class1

          Function testfunc(ByVal a As String) As String

              '...

              Return UCase(a)

          End Function

      End Class


    and registered an instance in the startup-event. After Silverlight is loaded (onload event) the javascript function

            function doit(sender, args) {

                var who = sender.getHost()

                console.log(who.Content.Bridge.testfunc("silverlight"))

            }

    is executed; the console is showing the text SILVERLIGHT. It works!!! But...

    How can I refer to a listbox in the UI? The line that is commented out in the definition of Class1 is there for that purpose but Intellisense will give me no clue at all.

    Wednesday, May 20, 2015 12:51 PM
  • As I touched on above...

    A listbox will be a private member of the Silverlight application.

    You can't just reach in and change stuff in it from javascript.

    You expose a method from Silverlight and call that using your javascript.

    [ScriptableMember()]
    public void AddListBoxItem(string str)
    {
         // New up listboxitem and add to listbox
    }


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    Wednesday, May 20, 2015 4:34 PM
  • Thanks Andy,

    That is exactly what I accomplished so far but it doesn't work.

    Imports System.Windows.Browser
    Partial Public Class MainPage
        Inherits UserControl
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Public Sub AddListBoxItem(ByVal newItem As String)
            lb.Items.Add(newItem)
        End Sub
    End Class
    
    <ScriptableType()>
    Public Class Class1
        Public Sub testfunc(ByVal newItem As String)
            AddListBoxItem(newItem)
        End Sub
    End Class
    In Javascript i can see the subroutine testfunc but this routine doesn't see AddListBoxItem.

    Wednesday, May 20, 2015 5:47 PM
  • You haven't marked either testfunc of addlistboxitem as scriptable.
    I forgot to mention you also have to register any object explicitly.

    https://msdn.microsoft.com/en-gb/library/cc221414%28v=vs.95%29.aspx?f=255&MSPPError=-2147217396


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Marked as answer by AHJKuipers Wednesday, May 20, 2015 6:49 PM
    Wednesday, May 20, 2015 6:13 PM
  • Thanks Andy,

    The solution:

    In app.xaml.vb

        Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup

            Me.RootVisual = New MainPage()

            System.Windows.Browser.HtmlPage.RegisterScriptableObject("Bridge", Me.RootVisual)

        End Sub


    And in the class Mainpage

        <ScriptableMember()>
        Public Sub AddListBoxItem(ByVal newItem As String)
            lb.Items.Add(newItem)
        End Sub


    And (only for testing purposes) in the onload handler

            function doit(sender, args) {
                var who = sender.getHost()
                who.Content.Bridge.AddListBoxItem("silverlight")
            }

    That's all. 

    P.S. You mentioned that scriptfunc was not scriptable. But Class1 was (by <ScriptableType()>) and then automatically all the members of Class1 are also scriptable.

    Wednesday, May 20, 2015 6:49 PM