用IronRuby创建WPF应用程序

我曾在早期的博文中介绍过IronRuby。在文章中,我扩展了IronRuby的基础 知识,来解释需要在Rail应用程序所做的额外工作,好让大家继续深入.NET所实 现Ruby语言,但这方面的内容并不够。所以现在我想深入地谈谈IronRuby与项目 的兼容性,以便开发全新的应用程序来说明IronRuby和.NET之间的互操作性。实 际上,我们会使用WPF(Windows Presentation Foundation),它是.NET Framework的组件,我们可以用它创建富媒体和图形界面。

WPF基础

再次申明,WPF是.NET Framework组件之一,负责呈现富用户界面和其他媒体 。它不是.NET Framework中唯一可完成该功能的函数库集,Window Form也可以 完成类似工作,在我们需要创建炫目效果的时候,WPF会显得十分有用。无论是 演示文档、视频、数据录入表格、某些类型的数据可视化(这是我最希望做的, 尤其用IronRuby完成,后面的故事更精彩)抑或用动画把以上的都串联起来,你 很可能会发现在给Windows开发这些应用程序的时候 WPF可以满足你的需求。

举例说明。某一天午饭时间,我创建了基于WPF的类似于时钟的应用程序—— 我喜欢参考WPF的“Hello,Wold”应用程序——于是决定使用IronRuby。

注:学习本示例的过程中,需要参考WPF文档。

示例程序

require 'WindowsBase'
require 'PresentationFramework'
require 'PresentationCore'
require 'System.Core, Version=3.5.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089'

class Clock

   CLOCK_WIDTH   = 150
   CLOCK_HEIGHT  = 150
   LABEL_HEIGHT  = CLOCK_HEIGHT / 7
   LABEL_WIDTH   = CLOCK_WIDTH / 7
   RADIUS     = CLOCK_WIDTH / 2
   RADS      = Math::PI / 180
   MIN_LOCATIONS  = {}
   HOUR_LOCATIONS = {}

   def run!
     plot_locations

     # build our window
     @window = System::Windows::Window.new
     @window.background =  System::Windows::Media::Brushes.LightGray
     @window.width = CLOCK_WIDTH * 2
     @window.height = CLOCK_HEIGHT * 2
     @window.resize_mode =  System::Windows::ResizeMode.NoResize

     @canvas = System::Windows::Controls::Canvas.new
     @canvas.width = CLOCK_WIDTH
     @canvas.height = CLOCK_HEIGHT

     # create shapes to represent clock hands
     @minute_hand = System::Windows::Shapes::Line.new
     @minute_hand.stroke =  System::Windows::Media::Brushes.Black
     @minute_hand.stroke_thickness = 1 
     @minute_hand.x1 = CLOCK_WIDTH / 2
     @minute_hand.y1 = CLOCK_HEIGHT / 2

     @hour_hand = System::Windows::Shapes::Line.new
     @hour_hand.stroke =  System::Windows::Media::Brushes.Black
     @hour_hand.stroke_thickness = 3
     @hour_hand.x1 = CLOCK_WIDTH / 2
     @hour_hand.y1 = CLOCK_HEIGHT / 2

     # .. and stick them to our canvas
     @canvas.children.add(@minute_hand)
     @canvas.children.add(@hour_hand)

     plot_face # draw a clock face
     plot_labels # draw clock numbers
     plot_hands # draw minute / hour hands

     @window.content = @canvas
     app = System::Windows::Application.new
     app.run(@window)
     # the Application object handles the lifecycle of  our app
     # including the execution loop
   end

   # determine 2 sets of equidistant points around the  circumference of a circle
   # of CLOCK_WIDTH and CLOCK_HEIGHT dimensions.
   def plot_locations
     for i in (0..60) # 60 minutes, and 12 hours
       a = i * 6
       x = (RADIUS * Math.sin(a * RADS)).to_i +  (CLOCK_WIDTH / 2)
       y = (CLOCK_HEIGHT / 2) - (RADIUS * Math.cos(a  * RADS)).to_i
       coords = [x, y]
       HOUR_LOCATIONS[i / 5] = coords if i % 5 == 0  # is this also an 'hour' location (ie. every 5 minutes)? 
       MIN_LOCATIONS[i] = coords
     end
   end

   # draws a circle to represent the clock's face
   def plot_face
     extra_x = (CLOCK_WIDTH * 0.15) # pad our circle a  little
     extra_y = (CLOCK_HEIGHT * 0.15)
     face = System::Windows::Shapes::Ellipse.new
     face.fill = System::Windows::Media::Brushes.White
     face.width = CLOCK_WIDTH + extra_x
     face.height = CLOCK_HEIGHT + extra_y
     face.margin = System::Windows::Thickness.new(0 -  (extra_x/2), 0 - (extra_y/2), 0, 0)
     face.stroke = System::Windows::Media::Brushes.Gray #  give it a slight border
     face.stroke_thickness = 1
     System::Windows::Controls::Canvas.set_z_index(face, -1) #  send our circle to the back
     @canvas.children.add(face) # add the clock face to  our canvas
   end

   # at each point along the hour locations, put a  number
   def plot_labels
     HOUR_LOCATIONS.each_pair do |p, coords|
       unless p == 0
         lbl = System::Windows::Controls::Label.new
         lbl.horizontal_content_alignment =  System::Windows::HorizontalAlignment.Center
         lbl.width = LABEL_WIDTH
         lbl.height = LABEL_HEIGHT
         lbl.content = p.to_s
         lbl.margin = System::Windows::Thickness.new (coords[0] - (LABEL_WIDTH / 2), coords[1] - (LABEL_HEIGHT /  2), 0, 0)
         lbl.padding = System::Windows::Thickness.new(0,  0, 0, 0)
         @canvas.children.add(lbl)
       end
     end
   end

   def plot_hands
     time = Time.now
     hours = time.hour
     minutes = time.min

     if !@minutes || minutes != @minutes
       @hours = hours >= 12 ? hours - 12 :  hours
       @minutes = minutes == 0 ? 60 : minutes
       # Dispatcher.BeginInvoke() is asynchronous, though  it probably doesn't matter too much here
       @minute_hand.dispatcher.begin_invoke (System::Windows::Threading::DispatcherPriority.Render,  System::Action.new {
         @minute_hand.x2 = MIN_LOCATIONS[@minutes][0]
         @minute_hand.y2 = MIN_LOCATIONS[@minutes][1]
         @hour_hand.x2 = HOUR_LOCATIONS[@hours][0]
         @hour_hand.y2 = HOUR_LOCATIONS[@hours][1]
       })
     end
   end

end

clock = Clock.new

timer = System::Timers::Timer.new
timer.interval = 1000
timer.elapsed { clock.plot_hands }
timer.enabled = true

clock.run!

时间: 2024-08-02 20:17:10

用IronRuby创建WPF应用程序的相关文章

WPF and Silverlight学习笔记(五):WPF应用程序管理

一.WPF应用程序由System.Windows.Application类进行管理 二.创 建WPF应用程序 创建WPF应用程序有两种方式: 1.Visual Studio 和Expression Blend默认的方式,使用App.xaml文件定义启动应用程序 App.xaml文件的内容大致如下: 1: <Application x:Class="WpfApplicationLifeCycle.App" 2: xmlns="http://schemas.microsof

wpf-VS2010创建的WPF浏览器程序运行时浏览器就停止工作了求指导

问题描述 VS2010创建的WPF浏览器程序运行时浏览器就停止工作了求指导 我用的WIN7操作系统(旗舰版),浏览器是用IE9.VS2010创建的WPF浏览器程序运行时浏览器就停止工作了,代码没有错误,请各位高手指导下. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xml

WPF and Silverlight学习笔记(四):WPF应用程序结构——HelloWorld

按照惯例,创建一个WPF的应用程序,点击按钮,在文本框中显示 "Hello WPF World",我们通过此例来分析WPF应用程序的结构. XAML文件如下: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation& quot; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class=

WPF应用程序中的发声功能

几个星期以前,我坐在一辆崭新的丰田普锐斯汽车中,听着租车公司的销售代理讲解着仪表盘上遍布 的陌生控制开关和指示器."哇,"我想,"虽然技术和车一样都那么陈旧了,制造商仍继续美化着用 户界面". 从最广义的层面上说,用户界面是人机交互的地方.虽然这一概念与技术本身一样历史悠久,但用户 界面作为一种艺术形式大放异彩倚仗的却是个人计算机革命. 现在,恐怕只有很小一部分个人计算机用户能够记得 Apple Macintosh 和 Microsoft Windows 图形 用户

WPF应用程序支持多国语言解决方案

原文:WPF应用程序支持多国语言解决方案 促使程序赢得更多客户的最好.最经济的方法是使之支持多国语言,而不是将潜在的客户群限制为全球近70亿人口中的一小部分.本文介绍四种实现WPF应用程序支持多国语言的解决方案. 效果如下图: Language - en-US (英文) Language - zh-CN (中文) 阅读目录 一.使用LocBaml工具 二.使用资源字典文件 三.使用.resx资源文件 四.实现动态切换程序显示语言 附:实现MessageBox支持多语言 一.使用LocBaml工具

【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)

原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 3.3 Calling a Web API From a WPF Application (C#) 3.3 通过WPF应用程序调用Web API(C#) 本文引自:http://www.asp.net/web-api/overview/web-api-clients/calling-a-we

【翻译】WPF应用程序模块化开发快速入门(使用Prism+MEF)【下】

索引 [翻译]WPF应用程序模块化开发快速入门(使用Prism框架)[上] [翻译]WPF应用程序模块化开发快速入门(使用Prism+MEF)[中] 系统启动 系统使用Bootstrapper类型来启动程序,并初始化主窗口 /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { protected override void On

捕捉WPF应用程序中XAML代码解析异常

由于WPF应用程序中XAML代码在很多时候是运行时加载处理的.比如DynamicResource,但是在编译或者运行的过程中,编写的XAML代码很可能有错误,此时XAML代码解析器通常会抛出称为XamlParseException的异常.但是抛出的XamlParseException异常提供的信息非常简单,或者是很不准确.此时我们关于通过对变通的方法来获取更多的异常信息: 我们知道,WPF应用程序中的XAML代码是在InitializeComponent方法中解析的.而这个方法通常位于窗口对象的

利用数据绑定和模板创建Atlas应用程序

程序|创建|模板|数据 一. 简介 本文将向你展示如何使用微软新的Web开发技术(代码名为Atlas)来实现数据绑定和模板.如果你已经理解什么是Atlas,其主要设计目的及其主要组件,那么你在阅读本文时将最大程度地受益. 本文将向你展示: · 把一个客户端listView控件绑定到一个dataSource控件. · 使用模板显示数据. 前提 为了完成本文中的示例程序,你需要具备下列条件: · Microsoft Visual Studio 2005和.NET Framework 2.0.有关下载