简体   繁体   中英

MyCanvas is not supported in a Windows Presentation Foundation (WPF) project

I have my own Canvas implementation:

namespace FontRendererWPF
{
    public class ImageCanvas : Canvas
    {
        string _imagePath;

        public string ImagePath
        {
            get { return _imagePath; }
            set
            {
                _imagePath = value;
                InvalidateVisual();
            }
        }

        protected override void OnRender(DrawingContext dc)
        {
            if (_imagePath == null)
                return;

            BitmapImage img = new BitmapImage(new Uri(_imagePath));
            dc.DrawImage(img, new Rect(0, 0, img.PixelWidth, img.PixelHeight));
        }
    }
}

And I just renamed the element in MainWindow.xaml from Canvas to ImageCanvas:

<Window x:Class="FontRendererWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MyApp" Height="350" Width="525">
    <Grid>               
        <ImageCanvas x:Name="canvas" Margin="10,83,7,5" Background="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}"/>
    </Grid>
</Window>

I got a compilation error:

Error 1 ImageCanvas is not supported in a Windows Presentation Foundation (WPF) project.

Do I need to register my ImageCanvas class somewhere?

You have to add namespace reference to your xaml code:

<Window x:Class="FontRendererWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:FontRendererWPF"
    Title="MyApp" Height="350" Width="525">
<Grid>               
    <controls:ImageCanvas x:Name="canvas" Margin="10,83,7,5" Background="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}"/>
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM