Question

I have a Silverlight 5 OOB app. I want to create 'toast'. I don't want to use NotificationWindows because of the limitations.

I've tried creating an S5 Window and sliding it onto screen. [Crude, but it's a start.] If I use one button n my main window to trigger the creation and another to trigger the animation, it works. If I put the animation into the first method, the Window just appears, sans animation. Does anyone know how I can make this work.

To recreate, inside the Grid in MainPage.xaml, add:

<Button Content="Show" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0"
        Name="show" VerticalAlignment="Top" Width="75" Click="show_Click" />
<Button Content="Move" Height="23" HorizontalAlignment="Left" Margin="12,41,0,0"
        Name="move" VerticalAlignment="Top" Width="75" Click="move_Click" />

Inside MainPage.xaml.cs, add:

    private Window toastWindow;

    private void show_Click(object sender, RoutedEventArgs e)
    {
        toastWindow = new Window();
        toastWindow.Width = 280;
        toastWindow.Height = 58;
        toastWindow.Left = 975;
        toastWindow.Top = 755;
        toastWindow.WindowStyle = WindowStyle.None;
        toastWindow.WindowState = WindowState.Normal;
        toastWindow.Content = new Toast();
        toastWindow.Show();
        toastWindow.Activate();

        //for (int i = 0; i < 20; i++)
        //{
        //    toastWindow.Top = toastWindow.Top - 5;
        //    System.Threading.Thread.Sleep(25);
        //}
    }

    private void move_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 20; i++)
        {
            toastWindow.Top = toastWindow.Top - 5;
            System.Threading.Thread.Sleep(25);
        }
    }

You'll also need a UserControl called Toast (in Toast.xaml) in which I gave the LayoutRoot Grid a Background colour so that it was obvious, even through the Start bar, with Width="300" Height="100".

On the Silverlight 5 Properties page you'll need to Enable running application out of the browser and in the Out-of-Browser Settings set Require elevated trust when running outside the browser.

If you run it and click Show you'll see the toast behind the Start bar/clock area. If you click Move, it slides up. If you uncomment the end of the show_Click method and run it again and click Show, it will appear in its final position without any sliding.

Any ideas?

P.S. Ultimately, I'm looking to be able to have multiple 'slices' on screen at any one time with independent life-spans.

Was it helpful?

Solution

Your 'move' code is currently synchronous, which means it will block the UI thread, which is very very bad.

You can use a DispatcherTimer to do that kind of effect instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top