简体   繁体   English

Xamarin/C# 扩展现有类

[英]Xamarin / C# extending an existing class

I've been learning C# for a while most recently MVVM but I've failed to grasp something regarding extending classes, obviously ...我最近学习 C# 有一段时间了,最​​近是MVVM但我未能掌握有关扩展类的知识,显然......

In the example below, SkiaSharp is a Xamarin vector graphics package, I've been supplied some vector icons and I'm looking to have a reusable class to scale and color them at various points in the app在下面的示例中, SkiaSharp是一个 Xamarin 矢量图形包,我提供了一些矢量图标,我希望有一个可重用的类来在应用程序的各个点对它们进行缩放和着色

I figured if I subclassed 'SKCanvasView' then I could pass in the variables (icon, color) - all works fine when the code is in the related view but I wanted it in shared code, so I reference it like so我想如果我将“SKCanvasView”子类化,那么我可以传入变量(图标、颜色)——当代码在相关视图中时一切正常,但我希望它在共享代码中,所以我像这样引用它

         <ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         xmlns:local="clr-namespace:Custom"
         x:Class="NavigationIcons">

        <local:RenderSVG StyleId="F9001A"
                         Grid.Row="0"
                         Grid.Column="1"
                         HorizontalOptions="CenterAndExpand"
                         VerticalOptions="CenterAndExpand"
                         ClassId="vector.svg" 
                         PaintSurface="OnCanvasViewPaintSurface">

        </local:RenderSVG>

It builds, but then it complains the method for 'PaintSurface' isn't in the view code-behind file, which of course I didn't think it needed to be if it's referencing another class它构建,但随后它抱怨“PaintSurface”的方法不在视图代码隐藏文件中,当然,如果它引用另一个类,我认为不需要

I would have assumed that "local:SVGRender" would mean that all code would be run from that shared class, rather than the class file behind the XAML view, but it appears not?我会假设"local:SVGRender"意味着所有代码都将从该共享类运行,而不是从 XAML 视图后面的类文件运行,但它似乎不是?

public class RenderSVG : SKCanvasView
{


    void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
    {
        try
        { 
            // rendering code is here

        }
    }

}

The error that's being thrown is "EventHandler "Custom.RenderSVG.OnCanvasViewPaintSurface" with correct signature not found in type "NavigationIcons", so it's not even trying to look at the derived class I made抛出的错误是“EventHandler”Custom.RenderSVG.OnCanvasViewPaintSurface”,在“NavigationIcons”类型中找不到正确的签名,所以它甚至没有试图查看我制作的派生类

As @FreakyAli said PaintSurface is an event.正如@FreakyAli 所说, PaintSurface是一个事件。 When using SKCanvasView inside a ContentView , you bind the event to a method (event handler) of the container.ContentView使用SKCanvasView ,您将事件绑定到容器的方法(事件处理程序)。 SKCanvasView will fire the event and the container method gets called. SKCanvasView将触发事件并调用容器方法。

But when you subclass SKCanvasView you have to override OnPaintSurface .但是当您SKCanvasView您必须覆盖OnPaintSurface Alternatively you can at some point attach the OnCanvasViewPaintSurface event handler to PaintSurface event through code (eg at construction) and at some point you will also need to detach it (before destruction).或者,您可以在某个时候通过代码(例如在构造时)将OnCanvasViewPaintSurface事件处理程序附加到PaintSurface事件,并且在某个时候您还需要将其分离(在销毁之前)。 Overriding OnPaintSurface is the preferred way.覆盖OnPaintSurface是首选方式。 For example:例如:

public class RenderSVG : SKCanvasView
{
    protected override void OnPaintSurface(SKPaintSurfaceEventArgs args)
    {
        try
        { 
            // rendering code is here
        }
    }
}

Of course you need to remove PaintSurface="OnCanvasViewPaintSurface" from your XAML file:当然,您需要从 XAML 文件中删除PaintSurface="OnCanvasViewPaintSurface"

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     xmlns:d="http://xamarin.com/schemas/2014/forms/design"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d"
     xmlns:local="clr-namespace:Custom"
     x:Class="NavigationIcons">

    <local:RenderSVG StyleId="F9001A"
                     Grid.Row="0"
                     Grid.Column="1"
                     HorizontalOptions="CenterAndExpand"
                     VerticalOptions="CenterAndExpand"
                     ClassId="vector.svg">
    </local:RenderSVG>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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