[IronPython] Silverlight code need help (Repost)

Jimmy Schementi Jimmy.Schementi at microsoft.com
Mon Jan 11 22:26:55 CET 2010

The error you're probably seeing is "AttributeError: 'Duration' object has no attribute 'CurrentState'," correct (line highlighted below)?. This is because MediaElement.NaturalDuration gives you a System.Windows.Duration object (http://msdn.microsoft.com/en-us/library/system.windows.duration(VS.95).aspx), which in-fact doesn't have a "CurrentState" member. However, MediaElement has a "CurrentState" member, so I think you intending that line to read " if self.video. CurrentState == MediaElementState.Playing:". You'll also need to add "from System.Windows.Media import MediaElementState" to make sure MediaElementState is available.

Let me know if you have any other issues.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Andrew Evans
Sent: Monday, January 11, 2010 1:04 PM
To: users at lists.ironpython.com
Subject: [IronPython] Silverlight code need help (Repost)

I am having problems with my code. I am not sure how to debug silverlight. I receive no error messages, but this is what happens. When this app runs it starts for a second then disappears I am not sure where I am going wrong, any ideas? Perhaps in the threading?

I am not a C# programmer but I am doing my best to find examples to compare to so I can translate into python

I would really appreciate any help *Cheers Andrew

Here is the full code hope that's alright :D

from System.Windows import Application
from System.Windows.Controls import MediaElement
from System.Windows.Controls import Button, Orientation, StackPanel, Slider
from System import Uri, UriKind
from System.Windows.Media.Imaging import BitmapImage
from System.Windows.Threading import *
from System import TimeSpan
class Gui():
    def __init__(self):

        self.main = StackPanel()

        self.layer1 = StackPanel()
        self.layer1.Orientation = Orientation.Horizontal

        self.stopB = Button()
        self.stopB.Width = 25
        self.stopB.Height = 20
        self.stopB.Content = BitmapImage(Uri("stop.jpg", UriKind.Relative))

        self.pauseB = Button()
        self.pauseB.Width = 25
        self.pauseB.Height = 20
        self.pauseB.Content = BitmapImage(Uri("pause.jpg", UriKind.Relative))

        self.playB = Button()
        self.playB.Width = 25
        self.playB.Height = 20
        self.playB.Content = BitmapImage(Uri("play.jpg", UriKind.Relative))

        # Assign Buttons to Functions
        self.stopB.Click += self.StopPlay
        self.pauseB.Click += self.PausePlay
        self.playB.Click += self.StartPlay

        #add a video slider
        self.myslide = Slider()
        self.myslide.Width = 250

        # Add buttons to sub stack panel


        # create new sub stack panel
        self.layer2 = StackPanel()
        self.layer2.Orientation = Orientation.Vertical

        #create Media Element
        self.video = MediaElement()
        self.source = Uri("counter.wmv", UriKind.Relative)
        self.video.Volume = 0.4
        self.video.Source = self.source
        self.video.Width = 450
        self.video.Height = 400
        self.video.CurrentStateChanged += self.videoChanged

        self.timer = DispatcherTimer()
        self.timer.Interval = TimeSpan.FromMilliseconds(45)
        self.timer.Tick += self.MyTimeToTick

        # Add media Element to Sub Stack panel

        # Add sub stack panels to place holde stack panel

        # Load the UI
        Application.Current.RootVisual = self.main

    def MyTimeToTick(self, s, e):
        if self.video.NaturalDuration.TimeSpan.TotalSeconds > 0:
            self.myslide.Value = self.video.Position.TotalSeconds / self.video.NaturalDuration.TimeSpan.TotalSeconds

    def videoChanged(self, s, e):
        if self.video.NaturalDuration.CurrentState == MediaElementState.Playing:

    def StopPlay(self, s, e):
        # stop the video

    def PausePlay(self, s, e):
        #pause the video

    def StartPlay(self, s, e):
        #play the video

gui = Gui()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100111/d303dbf2/attachment.html>

More information about the Ironpython-users mailing list