Use IronPython in silverlight

Source: Internet
Author: User
In Silverlight, ironruby, ironpython, and managed JScript are supported. Unfortunately, I still
I did not find any simple and easy-to-use plug-ins that can be directly developed on vs, so when I wrote this demo, I played
Start "Notepad" and write ironpython code.

There are corresponding development articles and code from the Silverlight SDK, although I compile and run it through the Code provided by it
The row is successful. But I always want to write more code in it to learn more about IronPython. However, this write only discovers some problems,
Including:

1. The input box does not support Chinese characters (however, Chinese characters can be pasted, but garbled characters will appear when the Chinese characters are written into the code)
2. If an event-bound control is declared in XAML, an error is reported.
3. If it is the only code in the corresponding event, an error is reported.

Later, I went online to find a solution and found Chapter E of Mencius in this article:

Silverlight 2 tutorial (4): Chiron.exe: Silverlight 2 Package and Dynamic Language Deployment Tool

I also mentioned similar issues. It seems that I am not alone. Fortunately, I found the method of adding a Button.

Okay, the following is the appropriate XAML code (app. xaml, note that the controls in it do not bind events, the event will be in The py
File ):

<Usercontrol xmlns = "http://schemas.microsoft.com/client/2007"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
X: class = "system. Windows. Controls. usercontrol" X: Name = "page" width = "400" Height = "300">
<Grid X: Name = "layoutroot" background = "white">
<Canvas. Top = "20">
<Textblock canvas. Top = "10" canvas. Left = "20"> Please input you name </textblock>
<Textbox X: Name = "userinput" width = "200" Height = "30" canvas. Top = "40" canvas. Left = "20">
</Textbox>
<Textblock X: Name = "MSG" canvas. Top = "90" canvas. Left = "20" foreground = "Navy" fontsize = "48">
</Textblock>
<Button X: Name = "msgbutton" canvas. Top = "130" canvas. Left = "20" content = "hello"> </button>
</Canvas>
</GRID>
</Usercontrol>



The corresponding pathon code (app. py) is as follows ):

# Namespace reference
From System. Windows import Application
From System. Windows. Controls import *
From System. Windows. Browser import *

# Class external event Declaration
Def handler1 (sender, eventArgs ):
Sender. Opacity/= 2

Class App:
Def _ init _ (self ):
Self. scene = Application. Current. LoadRootVisual (UserControl (), "app. xaml ")

Def start (self ):
# TODO: replace this with your application start logic
Self. Scene. userinput. Text = "Please input your name ."
# Bind external events
Self. Scene. userinput. mouseleftbuttonup + = handler1
# Bind internal events
Self. Scene. userinput. mouseleftbuttonup + = self. handler2
# Bind a button event here
Self. Scene. msgbutton. Click + = self. msgbutton_click

# Class internal event Declaration
Def handler2 (self, sender, eventargs ):
Sender. FontSize = sender. FontSize # If you comment on the code, an error is reported, which makes me dizzy.


Def MsgButton_Click (self, sender, eventArgs ):
HtmlPage. Window. Alert ("hello," + self. scene. UserInput. Text + "! ") # Display input information

App (). start ()


It seems that the code is much simpler than the C # We use, and the code is nearly half less.

Run chiron.exe to package and run the xap file. Run the following command line in the CMD command line:

C: \ py> "C: \ Program Files \ Microsoft SDKs \ Silverlight \ v2.0 \ Tools \ Chiron \ Chiron.exe"/w




The running result is as follows:



Now, the code is here.

For source code download, click here :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.