温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中怎么创建一个Silverlight控件

发布时间:2021-07-05 16:41:30 来源:亿速云 阅读:148 作者:Leah 栏目:编程语言

这篇文章给大家介绍Python中怎么创建一个Silverlight控件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

XAML文件app.xaml

  1. < Canvas xmlns="http://schemas.microsoft.com/client/2007"   

  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   

  3. x:Class="System.Windows.Controls.Canvas" x:Name="Page" 
    Width="400" Height="300">   

  4. < TextBlock x:Name="MsgText" Canvas.Top="10" Canvas.Left="10"> 

  5. < /TextBlock>   

  6. < Button x:Name="TestButton" Canvas.Top="40" Canvas.Left="10" 
    Content="Test">< /Button>   

  7. < /Canvas> 

这里是Python文件app.py

from System.Windows import Application   from System.Windows.Controls import *   from System.Windows.Browser import *   class App:   def __init__(self):   self.scene = Application.Current.LoadRootVisual(Canvas(), "app.xaml")   def start(self):   self.scene.TestButton.Click += self.TestButton_Click   def TestButton_Click(self, sender, eventArgs):   self.scene.MsgText.Text = "Hello, world!"   App().start()

其他的没什么悬念了,Chiron /d,开始跑,http://localhost:2060/index.htm上出现了效果,一个文本框,一个按钮,点一下按钮,框里面出来一个hello, world!

于是我就很激动,原来是这么好玩的啊,貌似Python创建Silverlight控件写起来也很简单的哇,那我来搞两个复杂一点的控件。什么控件比较帅呢,我一想,日历吧,这个东西好,那我就开始了,我加了一行

< Calendar x:Name="TestCalendar" Canvas.Top="10" Canvas.Left="10"/>

好了,开始跑,咦,不对,说找不到Calendar这个东西,研究了一下文档,发现这个是包含在System.Windows.Controls.Extended.dll里面,于是我把这个dll拷到app目录下,还是不行,怪啊,这要怎么办呢,到处找了一遍,发现竟然没有有关Python调用SL扩展控件的文章,这可痛苦了。

但是我不甘心,我相信微软不会这么不友好,于是我继续查帮助,咦,Calendar前面要加一个前缀做命名空间,但是XAML头上怎么定义呢,我想了想,实在想不出来,然后我很猥琐地想到了Blend,嘿嘿,我下载了一个,安装,创建项目,然后创建控件,这个时候只有内部控件可以用,然后我在项目上添加引用到SDK目录下的System.Windows.Controls.Extended.dll,于是就可以创建日历了。

我要的可不是这个哦,切换到XAML栏,看到没有,这行代码:

xmlns:System_Windows_Controls_Extended="clr-namespace:System.Windows.Controls; assembly=System.Windows.Controls.Extended",原来就是它在起作用呵,于是我照样定义了一遍,仍然不行。。。

这是怎么回事?继续发奋研究文档,终于发现,在Python文件里面也要写东西的,我贴Python创建Silverlight控件的完整代码了哦,app.py

import clr   clr.AddReference("System.Windows.Controls.Extended")   from System.Windows import Application   from System.Windows.Controls import *   from System.Windows.Browser import *   class App:   def __init__(self):   self.scene = Application.Current.LoadRootVisual(Canvas(), "app.xaml")   def start(self):   # TODO: replace this with your application start logic   self.scene.TestButton.Click += self.TestButton_Click   def TestButton_Click(self, sender, eventArgs):   HtmlPage.Window.Alert(self.scene.TestCalendar.SelectedDate.ToString())   App().start()

呵呵,看到没有,最开始那两句,它是起作用的关键。下面是app.xaml

  1. < Canvas xmlns="http://schemas.microsoft.com/client/2007"   

  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:System_Windows_Controls_Extended="clr-namespace:
    System.Windows.Controls;assembly=System.Windows.Controls.Extended"   

  3. x:Class="System.Windows.Controls.Canvas" x:Name="Page" 
    Width="400" Height="300">   

  4. < System_Windows_Controls_Extended:Calendar 
    x:Name="TestCalendar" Canvas.Top="10" Canvas.Left="10"/>   

  5. < Button x:Name="TestButton" Canvas.Top="180" 
    Canvas.Left="10" Content="Test">< /Button>   

  6. < /Canvas> 

关于Python中怎么创建一个Silverlight控件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI