如何使用XAML C#的自定义子类? [英] How to use a C# custom subclass in XAML?

查看:125
本文介绍了如何使用XAML C#的自定义子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:我想用SurfaceInkCanvas的子类在我mywindow的。
我创建了一个C#类是这样的:

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

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);     
        }
    }
}

和我想用它在我的XAML窗口。难道这样的事情

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

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



我是完全在错误的方式?

Am I totally on the wrong way ?

推荐答案

您需要为使用类导入XML命名空间...

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

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

的命名空间

注意如何进口的。默认的(无前缀)可以任何你想要的,但它可能是最好的留给微软的演示空间。再有就是X命名空间,这是基本XAML命名空间(当然你可以更改前缀,但你应该离开它,因为它是)。

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).

所以, 。为了你自己的命名空间添加到它有这样做(如果其中一个是本地)的两种方式。

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

CLR的命名空间:的xmlns :其中前缀> =CLR的命名空间:其中,空间取代;装配=<的AssemblyName>中
URI的命名空间:的xmlns:其中,前缀GT; =< URI>中

CLR-Namespaces: xmlns:<prefix>="clr-namespace:<namespace>;Assembly=<assemblyName>" URI-Namespaces: xmlns:<prefix>="<uri>"

在你的情况下,你可能要设置的前缀为本地,并使用CLR命名空间(因为它是所有可以使用)

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).

导入:的xmlns:地方=CLR的命名空间:myNameSpace对象;汇编= ???

用法:<局部:SubSurfaceInkCanvas ... />

另外,如果这些类是外部库里面,你可以映射你的CLR的命名空间XML的命名空间......看的有关一个explenation这个答案

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

这篇关于如何使用XAML C#的自定义子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆