Change SvgCanvas.Source within C# Code and a Resourcefile

Topics: Animation and Interactivity, Converters and Runtime, SVG
Oct 11, 2014 at 8:07 AM
Edited Oct 11, 2014 at 8:23 AM
Hello all,

I'am trying to change the SvgCanvas.Source property within my C# Code.

I already tried many different ways to load a different SVG file from my Resource folder but with no success.

I have a WPF Application, a Resource folder with three svg files (h1_05.svg, h1_08.svg and komplett.svg). The "komplett.svg" is the init svg file which i could easily set in the XAML-File
<svgc:SvgCanvas x:Name="canvas" 
            Source="/Resources/komplett.svg"
            Height="Auto"
            Width="Auto"
            MouseDown="Canvas_MouseDown" HorizontalAlignment="Left" VerticalAlignment="Center" />
In the Mouse_Down event i have so change the canvas.source depending on the location the mouse was clicked with a hide and show animation.
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var element = sender as UIElement;
            Point pos = e.GetPosition(element);
            if ((myController.isValidRoom(pos, rooms)) || (!isSvgKomplett))
            {
                DoubleAnimation hide = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(0.5));
                hide.Completed += (s, mouseEvent) => hideCompleted(pos);
                canvas.BeginAnimation(Canvas.OpacityProperty, hide);
            }
        }

        private void hideCompleted(Point p)
        {
            // This is the tricky part where i already tried many different ways
            canvas = new SvgCanvas
            {
                // At first it would be enough to change the svg file hardcoded
                Source = new Uri("pack://application:,,,/Resources/h1_05.svg")
            };
            DoubleAnimation show = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(0.5));
            canvas.BeginAnimation(Canvas.OpacityProperty, show);
        }
The problem is that C# dont accept this syntax and all other trys i made before.

I tried to build the svg files as Resource and as embeded resource but with no success.


Somebody know how to fix this?


kind regards Arthur
Coordinator
Oct 12, 2014 at 11:29 AM
Hello Arthur,
Please try this and let me know the results...
Uri uri = new Uri("Resources/h1_05.svg", UriKind.Relative);
Best regards,
Paul.
Marked as answer by artwel on 10/13/2014 at 1:42 AM
Oct 13, 2014 at 8:42 AM
SelormeyPaul wrote:
Uri uri = new Uri("Resources/h1_05.svg", UriKind.Relative);
Best regards,
Paul.
Wow so simple and it works. The thing i havent used before is the "UriKind.Relative" part before.

Thank you very much.

Best regards
Arthur.