wpf 如何在 XAML 中使用 C# 自定义子类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13351652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:09:57  来源:igfitidea点击:

How to use a C# custom subclass in XAML?

c#wpfxamlinheritance

提问by

Here is my issue : I would like to use a subclass of SurfaceInkCanvas in my MyWindow. I created a C# class like this :

这是我的问题:我想在 MyWindow 中使用 SurfaceInkCanvas 的子类。我创建了一个这样的 C# 类:

namespace MyNamespace
{
    public class SubSurfaceInkCanvas : SurfaceInkCanvas
    {
       private MyWindow container;

       public SubSurfaceInkCanvas()
           : base()
       {
       }

       public SubSurfaceInkCanvas(DrawingWindow d) : base()
       {
           container = d;
       }

       protected override void OnTouchDown(TouchEventArgs e)
        {
            base.OnTouchDown(e);     
        }
    }
}

And I would like to use it in my XAML window. Is it something like this ?

我想在我的 XAML 窗口中使用它。是这样的吗?

<MyNamespace:SubSurfaceInkCanvas
    x:Name="canvas"
    Background="White"
    TouchDown="OnTouchDown"/>

Am I totally on the wrong way ?

我完全走错路了吗?

采纳答案by m-y

You need to import an Xml Namespace in order to use classes...

您需要导入一个 Xml 命名空间才能使用类...

<Window x:Class="Namespace.SomeWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> ... </Window>

Notice how the namespaces are imported. The default one (no prefix) can be whatever you want, but it's probably best to leave that to Microsoft's presentation namespace. Then there is the "x" namespace, which is the base xaml namespace (of course you could change the prefix, but you should leave it as it is).

注意命名空间是如何导入的。默认的(无前缀)可以是您想要的任何内容,但最好将其留给 Microsoft 的表示命​​名空间。然后是“x”命名空间,它是基本的 xaml 命名空间(当然您可以更改前缀,但您应该保持原样)。

So, in order to add your own namespace to it there are two ways of doing it (one if it's local).

因此,为了向其中添加您自己的命名空间,有两种方法(如果是本地的,则为一种)。

  • CLR-Namespaces: xmlns:<prefix>="clr-namespace:<namespace>;Assembly=<assemblyName>"
  • URI-Namespaces: xmlns:<prefix>="<uri>"
  • CLR-命名空间: xmlns:<prefix>="clr-namespace:<namespace>;Assembly=<assemblyName>"
  • URI 命名空间: xmlns:<prefix>="<uri>"

In your case you'd probably want to set the prefix as "local" and use the CLR Namespace (since it is all you can use).

在您的情况下,您可能希望将前缀设置为“本地”并使用 CLR 命名空间(因为您只能使用它)。

Import: xmlns:local="clr-namespace:MyNamespace;Assembly=???"
Usage: <local:SubSurfaceInkCanvas ... />

进口:xmlns:local="clr-namespace:MyNamespace;Assembly=???"
用途:<local:SubSurfaceInkCanvas ... />



Alternatively, if these classes are inside of an external library, you can map your CLR-Namespaces to XML-Namespaces... see this answerfor an explenation on that.

或者,如果这些类在外部库中,您可以将您的 CLR-Namespaces 映射到 XML-Namespaces ......请参阅此答案以获取有关该内容的说明。

回答by Johan Larsson

You need to add the namespace (xmlns:myControls), try like this:

你需要添加命名空间(xmlns:myControls),试试这样:

<Window ...
        xmlns:myControls="clr-namespace:MyNamespace;assembly=MyNamespace"
        ...>
    <myControls:SubSurfaceInkCanvas x:Name="canvas"
                                    Background="White"
                                    TouchDown="OnTouchDown"/>
</Window>