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!