0

I would like to derive a class from Canvas in WPF so that I can override the onRender function, but am having a lot of difficulty as I am just beginning to familiarize myself with C# and .NET. My rudimentary understanding of the topic is that I must first create the class in code-behind and then add it to the MainWindow.xaml. Is there more to it than this? I have tried the following so far but come up with errors stating that

InitializeComponent does not exist in the current context 

in code-behind and then in xaml I get

The attribute 'Class' from the XAML namespace is only accepted on the root element

Here is the Code-behind I've tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace MyCanvas
{
    public partial class RenderCanvas : Canvas
    {
        public RenderCanvas()
        {
            InitializeComponent();
        }
    }
}

Here is my MainWindow.xaml:

<Window x:Class="MyCanvas.MainWindow"
        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"
        xmlns:local="clr-namespace:MyCanvas"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Canvas x:Class="MyCanvas.RenderCanvas"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
        </Canvas>
    </Grid>
</Window>

Thanks for your time!

MarkInTheDark
  • 244
  • 2
  • 15
  • The constructor is redundant, as there is no InitializeComponent method (and hence no need to call one). You also don't need a partial class declaration. In XAML, just write `...`. Don't confuse a derived Canvas with a UserControl. – Clemens Apr 24 '18 at 19:02
  • Ok, the your first sentence makes sense. However, when I changed the xaml to – MarkInTheDark Apr 24 '18 at 19:06
  • Actually, it all makes sense. Just rebuild your project. – Clemens Apr 24 '18 at 19:08
  • Well I'll be darned. Cleaning and building fixed everything, thanks! Is it going to be this bothersome throughout all WPF development or have I just hit a beginner's hiccough? – MarkInTheDark Apr 24 '18 at 19:11
  • There is typically no need to derive from controls. You usually prefer to use control and data templates. – Clemens Apr 24 '18 at 19:13
  • I need to draw charts/graphs/grids and bitmaps in the Canvas area. Think of something like CAD/Blender/Maya. What would be the correct way to handle this sort of rendering? – MarkInTheDark Apr 24 '18 at 19:16
  • Hard to tell, but these elements might well be regular child elements of the Canvas. If it is really necessary to override OnRender to do DrawingContext-based drawings, you might do this in a derived FrameworkElement, which may be in the same parent Panel as the Canvas, or another Canvas child. – Clemens Apr 24 '18 at 19:21
  • What are the benefits of using a derived FrameworkElement compared to overriding OnRender? Which method is more performant as the component will need to redraw its entire contents with any user interaction? – MarkInTheDark Apr 24 '18 at 19:25
  • Well, I can't write a WPF book for you here. Start reading this: https://learn.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/ – Clemens Apr 24 '18 at 19:34
  • @MarkInTheDark Apologies, but I feel that you're confusing how WPF's XAML works based on other platforms. You shouldn't need to override the onRender or work on the lower level portions of rendering at all. I mean, I'm sure you can come up with a legit reason but I'd be willing to bet that 99+% of the time you don't need to manage any of the lower level drawing mechanics. I just want to suggest reading up on how WPF / XAML works because; if this is how you're thinking, you're going to be amazed when you realize what's really going on... and then you'll love WPF even more. – Michael Puckett II Apr 25 '18 at 03:32

0 Answers0