Posts Tagged ‘Silverlight’

People want something to happen. And me too.
So I made the MP3 player. And it really can play MP3.
With the “MediaElement”, you can play video files too.

mp3_test.jpg (17 KB)

“MainPage.xaml” looks like this.

<UserControl x:Class="MP3MediaTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <StackPanel>
            <MediaElement AutoPlay="False" x:Name="myMP3" Source="Etudo.mp3" />
            <TextBlock FontSize="20" x:Name="myStatus" />
            <StackPanel Orientation="Horizontal">
                <Button x:Name="myPlayButton" Width="100" Content="Play" Click="OnPlay" />
                <Button x:Name="myPauseButton" Width="100" Content="Pause" Click="OnPause" />
                <Button x:Name="myStopButton" Width="100" Content="Stop" Click="OnStop" />
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

And “MainPage.xaml.cs” looks like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MP3MediaTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.myMP3.CurrentStateChanged += new RoutedEventHandler(OnCurrentStateChanged);
        }

        void OnCurrentStateChanged(object sender, RoutedEventArgs e)
        {
            bool isPlaying = (this.myMP3.CurrentState == MediaElementState.Playing);
            this.myPauseButton.IsEnabled = isPlaying && this.myMP3.CanPause;
            this.myPlayButton.IsEnabled = !isPlaying;
            this.myStatus.Text = this.myMP3.CurrentState.ToString() + " - " + this.myMP3.Source.ToString();
        }

        private void OnPlay(object sender, RoutedEventArgs e)
        {
            this.myMP3.Play();
        }

        private void OnPause(object sender, RoutedEventArgs e)
        {
            this.myMP3.Pause();
        }

        private void OnStop(object sender, RoutedEventArgs e)
        {
            this.myMP3.Stop();
        }
    }
}

imagebrush.jpg (36 KB)

<UserControl x:Class="ImageBrushTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
      <Canvas>
            <Rectangle RadiusX="30" RadiusY="30" Fill="AliceBlue" Stroke="Gray" StrokeThickness="5" Canvas.Left="5" Canvas.Top="5" Width="360" Height="360" />
            <Ellipse Width="330" Height="330" Canvas.Left="20" Canvas.Top="20" Stroke="Coral" StrokeThickness="10">
                <Ellipse.Fill>
                    <ImageBrush Stretch="Uniform" ImageSource="bird.png" />
                </Ellipse.Fill>
            </Ellipse>
        </Canvas>
  </Grid>
</UserControl>

Image file should be in the same folder as the XAP file (“ClientBin” folder). If it is not the case, you must specify the path to the file (ie http://www.yoursite.com/images/bird.png).
Actually, the XAP file is a ZIP file with different extension. You can change its extension and unzip it.

I experimented a few shapes with Silverlight.
If you just want to test in your Visual Studio 2008, copy the code and paste it into your IDE.
You do not need to do anything in C# or VB.

graphic_draw.jpg (41 KB)

<UserControl x:Class="GraphicDraw.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <Canvas>
            <Path Fill="Chocolate" Data="F1 M 75, 15 L 15, 150 L 150, 150" />
            <Rectangle Fill="Blue" Canvas.Left="100" Canvas.Top="160" Width="200" Height="150" />
            <Rectangle Fill="AliceBlue" Stroke="Black" StrokeThickness="10" Canvas.Left="200" Canvas.Top="50" Width="100" Height="100" />
            <Rectangle Fill="AliceBlue" Stroke="Black" StrokeThickness="10" RadiusX="40" RadiusY="40" Canvas.Left="380" Canvas.Top="50" Width="150" Height="200" />
            <Ellipse Fill="Red" Canvas.Left="300" Canvas.Top="260" Width="200" Height="150" />
            <Rectangle Width="500" Height="50" Canvas.Left="10" Canvas.Top="420">
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint="0, 0" EndPoint="1, 1">
                        <LinearGradientBrush.GradientStops>
                            <GradientStop Color="Yellow" Offset="0"/>
                            <GradientStop Color="Blue" Offset="1"/>
                        </LinearGradientBrush.GradientStops>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
        </Canvas>
  </Grid>
</UserControl>

I hope it can be useful for you.

Node_Silverlight.jpg (85 KB)

It was the funniest I have done with this book. It is actually animating.

Since I do not write about good things, I do not write what the book is called.
When I start reading books about Silverlight, there are pictures of Visual Studio, and it is very misleading.
It looks like I can use any of Blend or Visual Studio. But it’s just pictures and Visual Studio is usually unnecessary. What you need is Blend, it is not much to do with Visual Studio – with those books anyway. So these books are unusable for me.

I read books, web and stuff who described that you could do fun things with Silverlight with VisualStudio 2008 – C Sharp of course. But it appears not.
I saw some people using C Sharp Express and where I saw the Silverlight design, but there is not. It is not design, but preview. It was so in Beta, it looks like.

I feel that I must have MS Expression Blend to work with. How do I otherwise complex curves and stuff. You do it maybe, but not me. So I quit.

The Silverlight Managed Debugging Package Isn’t Installed! What error is that?
I saw the Silverlight animation on the web and I wanted to do it too.
Microsoft Silverlight technology will not work in Visual Studio 2008, so I need to update it to SP1. It was about 850 MB. When I made it, Windows wanted to further update of SP1, which was almost 500 MB.
It took a long time but it was done.

Right now, the latest version of Silverlight 4 does not work in VS2008, I have downloaded the Silverlight tools version 3, SDK and samples from Microsofts’ downloads.

I thought, “Now it is OK for work”. But NOT.
When I open a sample solution and click “Start Debugging”, it displays the following message.

The silverlight managed debugging package isn’t installed.

I thought everything was OK, but no.
I searched through the MSDN Forums and stuff, but found nothing that helped me. Then googled, and found quite a lot.
But what is correct? I did not want to test all possible. Anyway, I tried some of them, and did not work.
I thought Silverlight addon has something to do with it. It was a very reasoned to me. Since it’s made for ordinary users, not for developers. In addition, I saw the MS Silverlight plugin will always installed the latest (version 4). But I am trying to do is 3.

Conclusion

  • MSVS2008 must be updated to SP1
  • If you have Silverlight browser plugin version 4, uninstall it
  • If you have Silverlight4 Tools, uninstall them all
  • Download and install Silverlight3_Tools
  • Download and install Silverlight3_SDK
  • Download and install Silverlight_Developer (version 3)

You do not need to change the registry or such a complex.
I hope that helps if you have similar problems.