Skip to main content

Microsoft Silverlight

Answered Question ListBox and other controls not in namespace - bug?RSS Feed

(0)

Neko
Neko

Member

Member

34 points

9 Posts

ListBox and other controls not in namespace - bug?

My problem is in IronPython, see below for a complete example.

This works:
from System.Windows.Controls import Border
This does not:
from System.Windows.Controls import ListBox

Probably because it is not listed as one of the classes here:
http://msdn2.microsoft.com/en-us/library/system.windows.controls(VS.95).aspx

But it does live in that namespace, look:
http://msdn2.microsoft.com/en-us/library/system.windows.controls.listbox(VS.95).aspx

It also applies to WatermarkedTextBox, Slider, RadioButton, etc. 

But where does ListBox live then? How do I import it from my code? If I look at the symbols that System.Windows.Controls exports for IronPython, it is the list that is listed in the first MSDN article.

Relevant example files (to reproduce it); served with chiron /b (testpage excluded, because it is the default one):

app/app.py:

import clr
clr.AddReference("Microsoft.Scripting")
clr.AddReference("IronPython")
from System import Uri
from System.Windows import Application
from System.Windows.Controls import UserControl
from System.Windows.Controls import Border
# enabling this line gives an error:
#from System.Windows.Controls import ListBox

class App:
    def __init__(self):
        self.scene = Application.Current.LoadRootVisual(UserControl(), "app.xaml")

    def start(self):
        pass

App().start()

app/app.xaml

<UserControl x:Class="System.Windows.Controls.UserControl" x:Name="Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls">

    <ListBox>
        <ListBoxItem Content="Item1" />
        <ListBoxItem Content="Item2" />
    </ListBox>
</UserControl>

app/AppManifest.xml

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" RuntimeVersion="2.0.30226.00" EntryPointAssembly="Microsoft.Scripting.Silverlight" EntryPointType="Microsoft.Scripting.Silverlight.DynamicApplication">
  <Deployment.Parts>
    <!-- Add additional assemblies here -->
    <AssemblyPart Name="Microsoft.Scripting.Silverlight" Source="Microsoft.Scripting.Silverlight.dll" />
    <AssemblyPart Source="Microsoft.Scripting.dll" />
    <AssemblyPart Source="IronPython.dll" />
    <AssemblyPart Source="IronPython.Modules.dll" />

    <!-- Silverlight SDK controls assemblies -->
    <AssemblyPart Source="System.Windows.Controls.dll" Name="System.Windows.Controls" />
  </Deployment.Parts>
</Deployment>

 traceback

ManagedRuntimeError- IronPython.Runtime.Exceptions.ImportException: Cannot import name ListBox
   at IronPython.Runtime.Importer.ImportFrom(CodeContext context, Object from, String name)
   at IronPython.Runtime.Operations.PythonOps.ImportFrom(CodeContext context, Object module, String name)
   at OptimizedScope$1$0.Initialize(CodeContext ) in app.py:line 10
   at Microsoft.Scripting.ScriptCode.Run(CodeContext codeContext, Boolean tryEvaluate)
   at Microsoft.Scripting.ScriptCode.Run(Scope scope)
   at Microsoft.Scripting.Hosting.CompiledCode.Evaluate(IScriptScope scope)
   at Microsoft.Scripting.Hosting.CompiledCode.Execute(IScriptScope scope)
   at Microsoft.Scripting.Silverlight.DynamicApplication.StartMainProgram()
   at Microsoft.Scripting.Silverlight.DynamicApplication.DynamicApplication_Startup(Object sender, StartupEventArgs e)
   at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Neko
Neko

Member

Member

34 points

9 Posts

Answered Question

Re: ListBox and other controls not in namespace - bug?

Solved, needed to use fully qualified name to import the assembly (the default System.Windows.Controls is not yet the complete namespace/assembly, as it is loaded from System.dll, the rest should come from System.Windows.Controls.dll):

clr.AddReference("System.Windows.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

desroche
desroche

Member

Member

8 points

8 Posts

Re: ListBox and other controls not in namespace - bug?

I am having the same problem but I am using Blend 2.5 and XAML.  Could someone point me in the right direction as to how to put an S.L. 2 ListBox into a custom user control.  I can put one in the main page but it won't let me put one in any user controls.

Thanks for your time,

Tom

Neko
Neko

Member

Member

34 points

9 Posts

Re: ListBox and other controls not in namespace - bug?

You can add references to assemblies as well in XAML files (the Visual Studio/VB.Net did the following for me automatically when adding a ListBox): Add xmlns:c="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" to your UserControl tag. The c is the namespace prefix, this has to be unique for the thing you are importing. Similarly, to add a reference to the S.W.Controls.Extended assembly, you'd add something like this: xmlns:e="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Extended"

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities