【问题标题】:Inheriting from Canvas从画布继承
【发布时间】:2014-02-23 09:05:32
【问题描述】:

我想实现一个允许拖动对象的画布。所以我想我可以从 Canvas 继承并覆盖鼠标事件:

namespace ISC_CG
{
    public class DragCanvas : Canvas
    {
        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        ...

在 XAML 中:

<Window x:Class="ISC_CG.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="mainWindow"
        DataContext="{Binding ElementName=mainWindow}"
        xmlns:cg="clr-namespace:ISC_CG"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <cg:DragCanvas Name="mainCanvas" Background="{StaticResource checkers}" ClipToBounds="True" Focusable="True">

但我得到了错误:

命名空间中不存在名称“DragCanvas” “clr 命名空间:ISC_CG”。

我在这里错过了一些步骤吗?

谢谢。

【问题讨论】:

  • 画布和窗口在同一个程序集中吗?
  • 您尝试构建项目了吗?在编译之前,您会收到此错误。
  • @thumbmunkeys 是的,都在同一个项目中。
  • @tencntraze 项目未构建。设计器错误:命名空间“clr-namespace:ISC_CG”中不存在“DragCanvas”。编译器错误:“当前上下文中不存在名称‘mainCanvas’”

标签: c# wpf xaml inheritance namespaces


【解决方案1】:

仅供参考 - 这是我修复它的方法:

  1. 我不得不删除代码隐藏中对“mainCanvas”的所有引用
  2. 然后我重新编译并得到一个新的 Xaml 错误:在行中

告诉我必须将“Name”替换为“x:Name”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 2013-01-01
    • 2011-08-24
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多