// Copyright (c) The NodeRT Contributors
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the ""License""); you may
// not use this file except in compliance with the License. You may obtain a
// copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing permissions
// and limitations under the License.

// TODO: Verify that this is is still needed..
#define NTDDI_VERSION 0x06010000

#include <v8.h>
#include "nan.h"
#include <string>
#include <ppltasks.h>
#include "CollectionsConverter.h"
#include "CollectionsWrap.h"
#include "node-async.h"
#include "NodeRtUtils.h"
#include "OpaqueWrapper.h"
#include "WrapperBase.h"

#using <Windows.WinMD>

// this undefs fixes the issues of compiling Windows.Data.Json, Windows.Storag.FileProperties, and Windows.Stroage.Search
// Some of the node header files brings windows definitions with the same names as some of the WinRT methods
#undef DocumentProperties
#undef GetObject
#undef CreateEvent
#undef FindText
#undef SendMessage

const char* REGISTRATION_TOKEN_MAP_PROPERTY_NAME = "__registrationTokenMap__";

using v8::Array;
using v8::String;
using v8::Value;
using v8::Boolean;
using v8::Integer;
using v8::FunctionTemplate;
using v8::Object;
using v8::Local;
using v8::Function;
using v8::Date;
using v8::Number;
using v8::PropertyAttribute;
using v8::Primitive;
using Nan::HandleScope;
using Nan::Persistent;
using Nan::Undefined;
using Nan::True;
using Nan::False;
using Nan::Null;
using Nan::MaybeLocal;
using Nan::EscapableHandleScope;
using Nan::HandleScope;
using Nan::TryCatch;
using namespace concurrency;

namespace NodeRT { namespace Windows { namespace UI { namespace Xaml { namespace Media { namespace Imaging { 
  v8::Local<v8::Value> WrapBitmapSource(::Windows::UI::Xaml::Media::Imaging::BitmapSource^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::BitmapSource^ UnwrapBitmapSource(Local<Value> value);
  
  v8::Local<v8::Value> WrapBitmapImage(::Windows::UI::Xaml::Media::Imaging::BitmapImage^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::BitmapImage^ UnwrapBitmapImage(Local<Value> value);
  
  v8::Local<v8::Value> WrapDownloadProgressEventArgs(::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ UnwrapDownloadProgressEventArgs(Local<Value> value);
  
  v8::Local<v8::Value> WrapRenderTargetBitmap(::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ UnwrapRenderTargetBitmap(Local<Value> value);
  
  v8::Local<v8::Value> WrapSoftwareBitmapSource(::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ UnwrapSoftwareBitmapSource(Local<Value> value);
  
  v8::Local<v8::Value> WrapSurfaceImageSource(::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ UnwrapSurfaceImageSource(Local<Value> value);
  
  v8::Local<v8::Value> WrapSvgImageSource(::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ UnwrapSvgImageSource(Local<Value> value);
  
  v8::Local<v8::Value> WrapSvgImageSourceFailedEventArgs(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ UnwrapSvgImageSourceFailedEventArgs(Local<Value> value);
  
  v8::Local<v8::Value> WrapSvgImageSourceOpenedEventArgs(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ UnwrapSvgImageSourceOpenedEventArgs(Local<Value> value);
  
  v8::Local<v8::Value> WrapVirtualSurfaceImageSource(::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ UnwrapVirtualSurfaceImageSource(Local<Value> value);
  
  v8::Local<v8::Value> WrapWriteableBitmap(::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ UnwrapWriteableBitmap(Local<Value> value);
  
  v8::Local<v8::Value> WrapXamlRenderingBackgroundTask(::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ wintRtInstance);
  ::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ UnwrapXamlRenderingBackgroundTask(Local<Value> value);
  



  static void InitBitmapCreateOptionsEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("BitmapCreateOptions").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::UI::Xaml::Media::Imaging::BitmapCreateOptions::None)));
    Nan::Set(enumObject, Nan::New<String>("ignoreImageCache").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::UI::Xaml::Media::Imaging::BitmapCreateOptions::IgnoreImageCache)));
  }

  static void InitDecodePixelTypeEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("DecodePixelType").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("physical").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::UI::Xaml::Media::Imaging::DecodePixelType::Physical)));
    Nan::Set(enumObject, Nan::New<String>("logical").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::UI::Xaml::Media::Imaging::DecodePixelType::Logical)));
  }

  static void InitSvgImageSourceLoadStatusEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("SvgImageSourceLoadStatus").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("success").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceLoadStatus::Success)));
    Nan::Set(enumObject, Nan::New<String>("networkError").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceLoadStatus::NetworkError)));
    Nan::Set(enumObject, Nan::New<String>("invalidFormat").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceLoadStatus::InvalidFormat)));
    Nan::Set(enumObject, Nan::New<String>("other").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceLoadStatus::Other)));
  }



  class BitmapSource : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("BitmapSource").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);

        Local<Function> func;
        Local<FunctionTemplate> funcTemplate;

          
            Nan::SetPrototypeMethod(localRef, "setSource", SetSource);
          

          
            Nan::SetPrototypeMethod(localRef, "setSourceAsync", SetSourceAsync);
          


          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelHeight").ToLocalChecked(), PixelHeightGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelWidth").ToLocalChecked(), PixelWidthGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("dispatcher").ToLocalChecked(), DispatcherGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);

        Nan::SetAccessor(constructor, Nan::New<String>("pixelHeightProperty").ToLocalChecked(), PixelHeightPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("pixelWidthProperty").ToLocalChecked(), PixelWidthPropertyGetter);


        Nan::Set(exports, Nan::New<String>("BitmapSource").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      BitmapSource(::Windows::UI::Xaml::Media::Imaging::BitmapSource^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::BitmapSource^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::BitmapSource^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      BitmapSource *wrapperInstance = new BitmapSource(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::BitmapSource^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::BitmapSource^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapBitmapSource(winRtInstance));
    }

    static void SetSourceAsync(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info.This())) {
        return;
      }

      if (info.Length() == 0 || !info[info.Length() -1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: No callback was given")));
        return;
      }

      BitmapSource *wrapper = BitmapSource::Unwrap<BitmapSource>(info.This());

      ::Windows::Foundation::IAsyncAction^ op;


      if (info.Length() == 2
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Streams::IRandomAccessStream^>(info[0]))
      {
        try
        {
          ::Windows::Storage::Streams::IRandomAccessStream^ arg0 = dynamic_cast<::Windows::Storage::Streams::IRandomAccessStream^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          op = wrapper->_instance->SetSourceAsync(arg0);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }

      auto opTask = create_task(op);
      uv_async_t* asyncToken = NodeUtils::Async::GetAsyncToken(info[info.Length() -1].As<Function>());

      opTask.then( [asyncToken] (task<void> t) {
        try {
          t.get();
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [](NodeUtils::InvokeCallbackDelegate invokeCallback) {


            Local<Value> args[] = {Undefined()};


            invokeCallback(_countof(args), args);
          });
        } catch (Platform::Exception^ exception) {
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [exception](NodeUtils::InvokeCallbackDelegate invokeCallback) {
            Local<Value> error = NodeRT::Utils::WinRtExceptionToJsError(exception);

            Local<Value> args[] = {error};
            invokeCallback(_countof(args), args);
          });
        }
      });
    }

    static void SetSource(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info.This())) {
        return;
      }

      BitmapSource *wrapper = BitmapSource::Unwrap<BitmapSource>(info.This());

      if (info.Length() == 1
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Streams::IRandomAccessStream^>(info[0]))
      {
        try
        {
          ::Windows::Storage::Streams::IRandomAccessStream^ arg0 = dynamic_cast<::Windows::Storage::Streams::IRandomAccessStream^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          wrapper->_instance->SetSource(arg0);
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }



    static void PixelHeightGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info.This())) {
        return;
      }

      BitmapSource *wrapper = BitmapSource::Unwrap<BitmapSource>(info.This());

      try  {
        int result = wrapper->_instance->PixelHeight;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void PixelWidthGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info.This())) {
        return;
      }

      BitmapSource *wrapper = BitmapSource::Unwrap<BitmapSource>(info.This());

      try  {
        int result = wrapper->_instance->PixelWidth;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void DispatcherGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::DependencyObject^>(info.This())) {
        return;
      }

      DependencyObject *wrapper = DependencyObject::Unwrap<DependencyObject>(info.This());

      try  {
        ::Windows::UI::Core::CoreDispatcher^ result = wrapper->_instance->Dispatcher;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Core", "CoreDispatcher", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    static void PixelHeightPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapSource::PixelHeightProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void PixelWidthPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapSource::PixelWidthProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    private:
      ::Windows::UI::Xaml::Media::Imaging::BitmapSource^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapBitmapSource(::Windows::UI::Xaml::Media::Imaging::BitmapSource^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::BitmapSource^ UnwrapBitmapSource(Local<Value> value);
  };

  Persistent<FunctionTemplate> BitmapSource::s_constructorTemplate;

  v8::Local<v8::Value> WrapBitmapSource(::Windows::UI::Xaml::Media::Imaging::BitmapSource^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(BitmapSource::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::BitmapSource^ UnwrapBitmapSource(Local<Value> value) {
     return BitmapSource::Unwrap<BitmapSource>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitBitmapSource(Local<Object> exports) {
    BitmapSource::Init(exports);
  }

  class BitmapImage : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("BitmapImage").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);


          
            Nan::SetPrototypeMethod(localRef, "play", Play);
            Nan::SetPrototypeMethod(localRef, "stop", Stop);
          


          
          Nan::SetPrototypeMethod(localRef,"addListener", AddListener);
          Nan::SetPrototypeMethod(localRef,"on", AddListener);
          Nan::SetPrototypeMethod(localRef,"removeListener", RemoveListener);
          Nan::SetPrototypeMethod(localRef, "off", RemoveListener);

          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("uriSource").ToLocalChecked(), UriSourceGetter, UriSourceSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("decodePixelWidth").ToLocalChecked(), DecodePixelWidthGetter, DecodePixelWidthSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("decodePixelHeight").ToLocalChecked(), DecodePixelHeightGetter, DecodePixelHeightSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("createOptions").ToLocalChecked(), CreateOptionsGetter, CreateOptionsSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("decodePixelType").ToLocalChecked(), DecodePixelTypeGetter, DecodePixelTypeSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("autoPlay").ToLocalChecked(), AutoPlayGetter, AutoPlaySetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("isAnimatedBitmap").ToLocalChecked(), IsAnimatedBitmapGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("isPlaying").ToLocalChecked(), IsPlayingGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelHeight").ToLocalChecked(), PixelHeightGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelWidth").ToLocalChecked(), PixelWidthGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("dispatcher").ToLocalChecked(), DispatcherGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);

        Nan::SetAccessor(constructor, Nan::New<String>("createOptionsProperty").ToLocalChecked(), CreateOptionsPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("decodePixelHeightProperty").ToLocalChecked(), DecodePixelHeightPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("decodePixelWidthProperty").ToLocalChecked(), DecodePixelWidthPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("uriSourceProperty").ToLocalChecked(), UriSourcePropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("decodePixelTypeProperty").ToLocalChecked(), DecodePixelTypePropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("autoPlayProperty").ToLocalChecked(), AutoPlayPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("isAnimatedBitmapProperty").ToLocalChecked(), IsAnimatedBitmapPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("isPlayingProperty").ToLocalChecked(), IsPlayingPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("pixelHeightProperty").ToLocalChecked(), PixelHeightPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("pixelWidthProperty").ToLocalChecked(), PixelWidthPropertyGetter);


        Nan::Set(exports, Nan::New<String>("BitmapImage").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      BitmapImage(::Windows::UI::Xaml::Media::Imaging::BitmapImage^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::BitmapImage^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::BitmapImage^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 1
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Foundation::Uri^>(info[0]))
      {
        try {
          ::Windows::Foundation::Uri^ arg0 = dynamic_cast<::Windows::Foundation::Uri^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::BitmapImage(arg0);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 0)
      {
        try {
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::BitmapImage();
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      BitmapImage *wrapperInstance = new BitmapImage(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::BitmapImage^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::BitmapImage^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapBitmapImage(winRtInstance));
    }


    static void Play(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->Play();
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void Stop(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->Stop();
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }



    static void UriSourceGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try  {
        ::Windows::Foundation::Uri^ result = wrapper->_instance->UriSource;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.Foundation", "Uri", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void UriSourceSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Foundation::Uri^>(value)) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try {

        ::Windows::Foundation::Uri^ winRtValue = dynamic_cast<::Windows::Foundation::Uri^>(NodeRT::Utils::GetObjectInstance(value));

        wrapper->_instance->UriSource = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void DecodePixelWidthGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try  {
        int result = wrapper->_instance->DecodePixelWidth;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void DecodePixelWidthSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsInt32()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try {

        int winRtValue = static_cast<int>(Nan::To<int32_t>(value).FromMaybe(0));

        wrapper->_instance->DecodePixelWidth = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void DecodePixelHeightGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try  {
        int result = wrapper->_instance->DecodePixelHeight;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void DecodePixelHeightSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsInt32()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try {

        int winRtValue = static_cast<int>(Nan::To<int32_t>(value).FromMaybe(0));

        wrapper->_instance->DecodePixelHeight = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void CreateOptionsGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try  {
        ::Windows::UI::Xaml::Media::Imaging::BitmapCreateOptions result = wrapper->_instance->CreateOptions;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void CreateOptionsSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsInt32()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try {

        ::Windows::UI::Xaml::Media::Imaging::BitmapCreateOptions winRtValue = static_cast<::Windows::UI::Xaml::Media::Imaging::BitmapCreateOptions>(Nan::To<int32_t>(value).FromMaybe(0));

        wrapper->_instance->CreateOptions = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void DecodePixelTypeGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try  {
        ::Windows::UI::Xaml::Media::Imaging::DecodePixelType result = wrapper->_instance->DecodePixelType;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void DecodePixelTypeSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsInt32()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try {

        ::Windows::UI::Xaml::Media::Imaging::DecodePixelType winRtValue = static_cast<::Windows::UI::Xaml::Media::Imaging::DecodePixelType>(Nan::To<int32_t>(value).FromMaybe(0));

        wrapper->_instance->DecodePixelType = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void AutoPlayGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try  {
        bool result = wrapper->_instance->AutoPlay;
        info.GetReturnValue().Set(Nan::New<Boolean>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void AutoPlaySetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsBoolean()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try {

        bool winRtValue = Nan::To<bool>(value).FromMaybe(false);

        wrapper->_instance->AutoPlay = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void IsAnimatedBitmapGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try  {
        bool result = wrapper->_instance->IsAnimatedBitmap;
        info.GetReturnValue().Set(Nan::New<Boolean>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void IsPlayingGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This())) {
        return;
      }

      BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());

      try  {
        bool result = wrapper->_instance->IsPlaying;
        info.GetReturnValue().Set(Nan::New<Boolean>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void PixelHeightGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info.This())) {
        return;
      }

      BitmapSource *wrapper = BitmapSource::Unwrap<BitmapSource>(info.This());

      try  {
        int result = wrapper->_instance->PixelHeight;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void PixelWidthGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info.This())) {
        return;
      }

      BitmapSource *wrapper = BitmapSource::Unwrap<BitmapSource>(info.This());

      try  {
        int result = wrapper->_instance->PixelWidth;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void DispatcherGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::DependencyObject^>(info.This())) {
        return;
      }

      DependencyObject *wrapper = DependencyObject::Unwrap<DependencyObject>(info.This());

      try  {
        ::Windows::UI::Core::CoreDispatcher^ result = wrapper->_instance->Dispatcher;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Core", "CoreDispatcher", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    static void CreateOptionsPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapImage::CreateOptionsProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void DecodePixelHeightPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapImage::DecodePixelHeightProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void DecodePixelWidthPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapImage::DecodePixelWidthProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void UriSourcePropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapImage::UriSourceProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void DecodePixelTypePropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapImage::DecodePixelTypeProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void AutoPlayPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapImage::AutoPlayProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void IsAnimatedBitmapPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapImage::IsAnimatedBitmapProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void IsPlayingPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapImage::IsPlayingProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void PixelHeightPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapSource::PixelHeightProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void PixelWidthPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapSource::PixelWidthProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void AddListener(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"wrong arguments, expected arguments are eventName(string),callback(function)")));
        return;
      }

      String::Value eventName(v8::Isolate::GetCurrent(), info[0]);
      auto str = *eventName;

      Local<Function> callback = info[1].As<Function>();

      ::Windows::Foundation::EventRegistrationToken registrationToken;
      if (NodeRT::Utils::CaseInsenstiveEquals(L"downloadProgress", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This()))
        {
          Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
      return;
        }
        BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());
      
        try {
          Persistent<Object>* perstPtr = new Persistent<Object>();
          perstPtr->Reset(NodeRT::Utils::CreateCallbackObjectInDomain(callback));
          std::shared_ptr<Persistent<Object>> callbackObjPtr(perstPtr,
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Reset();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->DownloadProgress::add(
            ref new ::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventHandler(
            [callbackObjPtr](::Platform::Object^ arg0, ::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = CreateOpaqueWrapper(arg0);
                  wrappedArg1 = WrapDownloadProgressEventArgs(arg1);


                  if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                  if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();
                }

                Local<Value> args[] = { wrappedArg0, wrappedArg1 };
                Local<Object> callbackObjLocalRef = Nan::New<Object>(*callbackObjPtr);
                NodeRT::Utils::CallCallbackInDomain(callbackObjLocalRef, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }

      }
      else if (NodeRT::Utils::CaseInsenstiveEquals(L"imageFailed", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This()))
        {
          Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
      return;
        }
        BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());
      
        try {
          Persistent<Object>* perstPtr = new Persistent<Object>();
          perstPtr->Reset(NodeRT::Utils::CreateCallbackObjectInDomain(callback));
          std::shared_ptr<Persistent<Object>> callbackObjPtr(perstPtr,
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Reset();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->ImageFailed::add(
            ref new ::Windows::UI::Xaml::ExceptionRoutedEventHandler(
            [callbackObjPtr](::Platform::Object^ arg0, ::Windows::UI::Xaml::ExceptionRoutedEventArgs^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = CreateOpaqueWrapper(arg0);
                  wrappedArg1 = NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "ExceptionRoutedEventArgs", arg1);


                  if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                  if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();
                }

                Local<Value> args[] = { wrappedArg0, wrappedArg1 };
                Local<Object> callbackObjLocalRef = Nan::New<Object>(*callbackObjPtr);
                NodeRT::Utils::CallCallbackInDomain(callbackObjLocalRef, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }

      }
      else if (NodeRT::Utils::CaseInsenstiveEquals(L"imageOpened", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This()))
        {
          Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
      return;
        }
        BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());
      
        try {
          Persistent<Object>* perstPtr = new Persistent<Object>();
          perstPtr->Reset(NodeRT::Utils::CreateCallbackObjectInDomain(callback));
          std::shared_ptr<Persistent<Object>> callbackObjPtr(perstPtr,
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Reset();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->ImageOpened::add(
            ref new ::Windows::UI::Xaml::RoutedEventHandler(
            [callbackObjPtr](::Platform::Object^ arg0, ::Windows::UI::Xaml::RoutedEventArgs^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = CreateOpaqueWrapper(arg0);
                  wrappedArg1 = NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "RoutedEventArgs", arg1);


                  if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                  if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();
                }

                Local<Value> args[] = { wrappedArg0, wrappedArg1 };
                Local<Object> callbackObjLocalRef = Nan::New<Object>(*callbackObjPtr);
                NodeRT::Utils::CallCallbackInDomain(callbackObjLocalRef, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }

      }
 else  {
        Nan::ThrowError(Nan::Error(String::Concat(v8::Isolate::GetCurrent(), NodeRT::Utils::NewString(L"given event name isn't supported: "), info[0].As<String>())));
        return;
      }

      Local<Value> tokenMapVal = NodeRT::Utils::GetHiddenValue(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked());
      Local<Object> tokenMap;

      if (tokenMapVal.IsEmpty() || Nan::Equals(tokenMapVal, Undefined()).FromMaybe(false)) {
        tokenMap = Nan::New<Object>();
        NodeRT::Utils::SetHiddenValueWithObject(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked(), tokenMap);
      } else {
        tokenMap = Nan::To<Object>(tokenMapVal).ToLocalChecked();
      }

      Nan::Set(tokenMap, info[0], CreateOpaqueWrapper(::Windows::Foundation::PropertyValue::CreateInt64(registrationToken.Value)));
    }

    static void RemoveListener(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"wrong arguments, expected a string and a callback")));
        return;
      }

      String::Value eventName(v8::Isolate::GetCurrent(), info[0]);
      auto str = *eventName;

      if ((!NodeRT::Utils::CaseInsenstiveEquals(L"downloadProgress", str)) &&(!NodeRT::Utils::CaseInsenstiveEquals(L"imageFailed", str)) &&(!NodeRT::Utils::CaseInsenstiveEquals(L"imageOpened", str))) {
        Nan::ThrowError(Nan::Error(String::Concat(v8::Isolate::GetCurrent(), NodeRT::Utils::NewString(L"given event name isn't supported: "), info[0].As<String>())));
        return;
      }

      Local<Function> callback = info[1].As<Function>();
      Local<Value> tokenMap = NodeRT::Utils::GetHiddenValue(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked());

      if (tokenMap.IsEmpty() || Nan::Equals(tokenMap, Undefined()).FromMaybe(false)) {
        return;
      }

      Local<Value> opaqueWrapperObj =  Nan::Get(Nan::To<Object>(tokenMap).ToLocalChecked(), info[0]).ToLocalChecked();

      if (opaqueWrapperObj.IsEmpty() || Nan::Equals(opaqueWrapperObj,Undefined()).FromMaybe(false)) {
        return;
      }

      OpaqueWrapper *opaqueWrapper = OpaqueWrapper::Unwrap<OpaqueWrapper>(opaqueWrapperObj.As<Object>());

      long long tokenValue = (long long) opaqueWrapper->GetObjectInstance();
      ::Windows::Foundation::EventRegistrationToken registrationToken;
      registrationToken.Value = tokenValue;

      try  {
        if (NodeRT::Utils::CaseInsenstiveEquals(L"downloadProgress", str)) {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This()))
          {
            Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return;
          }
          BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());
          wrapper->_instance->DownloadProgress::remove(registrationToken);
        }
        else if (NodeRT::Utils::CaseInsenstiveEquals(L"imageFailed", str))
        {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This()))
          {
            Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return;
          }
          BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());
          wrapper->_instance->ImageFailed::remove(registrationToken);
        }
        else if (NodeRT::Utils::CaseInsenstiveEquals(L"imageOpened", str))
        {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapImage^>(info.This()))
          {
            Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return;
          }
          BitmapImage *wrapper = BitmapImage::Unwrap<BitmapImage>(info.This());
          wrapper->_instance->ImageOpened::remove(registrationToken);
        }
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }

      Nan::Delete(Nan::To<Object>(tokenMap).ToLocalChecked(), Nan::To<String>(info[0]).ToLocalChecked());
    }
    private:
      ::Windows::UI::Xaml::Media::Imaging::BitmapImage^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapBitmapImage(::Windows::UI::Xaml::Media::Imaging::BitmapImage^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::BitmapImage^ UnwrapBitmapImage(Local<Value> value);
  };

  Persistent<FunctionTemplate> BitmapImage::s_constructorTemplate;

  v8::Local<v8::Value> WrapBitmapImage(::Windows::UI::Xaml::Media::Imaging::BitmapImage^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(BitmapImage::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::BitmapImage^ UnwrapBitmapImage(Local<Value> value) {
     return BitmapImage::Unwrap<BitmapImage>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitBitmapImage(Local<Object> exports) {
    BitmapImage::Init(exports);
  }

  class DownloadProgressEventArgs : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("DownloadProgressEventArgs").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);





          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("progress").ToLocalChecked(), ProgressGetter, ProgressSetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("DownloadProgressEventArgs").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      DownloadProgressEventArgs(::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      DownloadProgressEventArgs *wrapperInstance = new DownloadProgressEventArgs(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapDownloadProgressEventArgs(winRtInstance));
    }





    static void ProgressGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^>(info.This())) {
        return;
      }

      DownloadProgressEventArgs *wrapper = DownloadProgressEventArgs::Unwrap<DownloadProgressEventArgs>(info.This());

      try  {
        int result = wrapper->_instance->Progress;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void ProgressSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsInt32()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^>(info.This())) {
        return;
      }

      DownloadProgressEventArgs *wrapper = DownloadProgressEventArgs::Unwrap<DownloadProgressEventArgs>(info.This());

      try {

        int winRtValue = static_cast<int>(Nan::To<int32_t>(value).FromMaybe(0));

        wrapper->_instance->Progress = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      


    private:
      ::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapDownloadProgressEventArgs(::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ UnwrapDownloadProgressEventArgs(Local<Value> value);
  };

  Persistent<FunctionTemplate> DownloadProgressEventArgs::s_constructorTemplate;

  v8::Local<v8::Value> WrapDownloadProgressEventArgs(::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(DownloadProgressEventArgs::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::DownloadProgressEventArgs^ UnwrapDownloadProgressEventArgs(Local<Value> value) {
     return DownloadProgressEventArgs::Unwrap<DownloadProgressEventArgs>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitDownloadProgressEventArgs(Local<Object> exports) {
    DownloadProgressEventArgs::Init(exports);
  }

  class RenderTargetBitmap : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("RenderTargetBitmap").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);

        Local<Function> func;
        Local<FunctionTemplate> funcTemplate;


          
            Nan::SetPrototypeMethod(localRef, "renderAsync", RenderAsync);
            Nan::SetPrototypeMethod(localRef, "getPixelsAsync", GetPixelsAsync);
          


          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelHeight").ToLocalChecked(), PixelHeightGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelWidth").ToLocalChecked(), PixelWidthGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("dispatcher").ToLocalChecked(), DispatcherGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);

        Nan::SetAccessor(constructor, Nan::New<String>("pixelHeightProperty").ToLocalChecked(), PixelHeightPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("pixelWidthProperty").ToLocalChecked(), PixelWidthPropertyGetter);


        Nan::Set(exports, Nan::New<String>("RenderTargetBitmap").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      RenderTargetBitmap(::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 0)
      {
        try {
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap();
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      RenderTargetBitmap *wrapperInstance = new RenderTargetBitmap(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapRenderTargetBitmap(winRtInstance));
    }

    static void RenderAsync(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^>(info.This())) {
        return;
      }

      if (info.Length() == 0 || !info[info.Length() -1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: No callback was given")));
        return;
      }

      RenderTargetBitmap *wrapper = RenderTargetBitmap::Unwrap<RenderTargetBitmap>(info.This());

      ::Windows::Foundation::IAsyncAction^ op;


      if (info.Length() == 2
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::UIElement^>(info[0]))
      {
        try
        {
          ::Windows::UI::Xaml::UIElement^ arg0 = dynamic_cast<::Windows::UI::Xaml::UIElement^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          op = wrapper->_instance->RenderAsync(arg0);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 4
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::UIElement^>(info[0])
        && info[1]->IsInt32()
        && info[2]->IsInt32())
      {
        try
        {
          ::Windows::UI::Xaml::UIElement^ arg0 = dynamic_cast<::Windows::UI::Xaml::UIElement^>(NodeRT::Utils::GetObjectInstance(info[0]));
          int arg1 = static_cast<int>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          int arg2 = static_cast<int>(Nan::To<int32_t>(info[2]).FromMaybe(0));
          
          op = wrapper->_instance->RenderAsync(arg0,arg1,arg2);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }

      auto opTask = create_task(op);
      uv_async_t* asyncToken = NodeUtils::Async::GetAsyncToken(info[info.Length() -1].As<Function>());

      opTask.then( [asyncToken] (task<void> t) {
        try {
          t.get();
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [](NodeUtils::InvokeCallbackDelegate invokeCallback) {


            Local<Value> args[] = {Undefined()};


            invokeCallback(_countof(args), args);
          });
        } catch (Platform::Exception^ exception) {
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [exception](NodeUtils::InvokeCallbackDelegate invokeCallback) {
            Local<Value> error = NodeRT::Utils::WinRtExceptionToJsError(exception);

            Local<Value> args[] = {error};
            invokeCallback(_countof(args), args);
          });
        }
      });
    }
    static void GetPixelsAsync(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^>(info.This())) {
        return;
      }

      if (info.Length() == 0 || !info[info.Length() -1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: No callback was given")));
        return;
      }

      RenderTargetBitmap *wrapper = RenderTargetBitmap::Unwrap<RenderTargetBitmap>(info.This());

      ::Windows::Foundation::IAsyncOperation<::Windows::Storage::Streams::IBuffer^>^ op;


      if (info.Length() == 1)
      {
        try
        {
          op = wrapper->_instance->GetPixelsAsync();
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }

      auto opTask = create_task(op);
      uv_async_t* asyncToken = NodeUtils::Async::GetAsyncToken(info[info.Length() -1].As<Function>());

      opTask.then( [asyncToken] (task<::Windows::Storage::Streams::IBuffer^> t) {
        try {
          auto result = t.get();
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [result](NodeUtils::InvokeCallbackDelegate invokeCallback) {


            Local<Value> error;
            Local<Value> arg1;
            {
              TryCatch tryCatch;
              arg1 = NodeRT::Utils::CreateExternalWinRTObject("Windows.Storage.Streams", "IBuffer", result);
              if (tryCatch.HasCaught())
              {
                error = Nan::To<Object>(tryCatch.Exception()).ToLocalChecked();
              }
              else
              {
                error = Undefined();
              }
              if (arg1.IsEmpty()) arg1 = Undefined();
            }
            Local<Value> args[] = {error, arg1};


            invokeCallback(_countof(args), args);
          });
        } catch (Platform::Exception^ exception) {
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [exception](NodeUtils::InvokeCallbackDelegate invokeCallback) {
            Local<Value> error = NodeRT::Utils::WinRtExceptionToJsError(exception);

            Local<Value> args[] = {error};
            invokeCallback(_countof(args), args);
          });
        }
      });
    }




    static void PixelHeightGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^>(info.This())) {
        return;
      }

      RenderTargetBitmap *wrapper = RenderTargetBitmap::Unwrap<RenderTargetBitmap>(info.This());

      try  {
        int result = wrapper->_instance->PixelHeight;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void PixelWidthGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^>(info.This())) {
        return;
      }

      RenderTargetBitmap *wrapper = RenderTargetBitmap::Unwrap<RenderTargetBitmap>(info.This());

      try  {
        int result = wrapper->_instance->PixelWidth;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void DispatcherGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::DependencyObject^>(info.This())) {
        return;
      }

      DependencyObject *wrapper = DependencyObject::Unwrap<DependencyObject>(info.This());

      try  {
        ::Windows::UI::Core::CoreDispatcher^ result = wrapper->_instance->Dispatcher;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Core", "CoreDispatcher", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    static void PixelHeightPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap::PixelHeightProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void PixelWidthPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap::PixelWidthProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    private:
      ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapRenderTargetBitmap(::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ UnwrapRenderTargetBitmap(Local<Value> value);
  };

  Persistent<FunctionTemplate> RenderTargetBitmap::s_constructorTemplate;

  v8::Local<v8::Value> WrapRenderTargetBitmap(::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(RenderTargetBitmap::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap^ UnwrapRenderTargetBitmap(Local<Value> value) {
     return RenderTargetBitmap::Unwrap<RenderTargetBitmap>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitRenderTargetBitmap(Local<Object> exports) {
    RenderTargetBitmap::Init(exports);
  }

  class SoftwareBitmapSource : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("SoftwareBitmapSource").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);

        Local<Function> func;
        Local<FunctionTemplate> funcTemplate;

          
            Nan::SetPrototypeMethod(localRef, "close", Close);
          

          
            Nan::SetPrototypeMethod(localRef, "setBitmapAsync", SetBitmapAsync);
          


          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("dispatcher").ToLocalChecked(), DispatcherGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("SoftwareBitmapSource").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      SoftwareBitmapSource(::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 0)
      {
        try {
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource();
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      SoftwareBitmapSource *wrapperInstance = new SoftwareBitmapSource(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapSoftwareBitmapSource(winRtInstance));
    }

    static void SetBitmapAsync(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^>(info.This())) {
        return;
      }

      if (info.Length() == 0 || !info[info.Length() -1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: No callback was given")));
        return;
      }

      SoftwareBitmapSource *wrapper = SoftwareBitmapSource::Unwrap<SoftwareBitmapSource>(info.This());

      ::Windows::Foundation::IAsyncAction^ op;


      if (info.Length() == 2
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Graphics::Imaging::SoftwareBitmap^>(info[0]))
      {
        try
        {
          ::Windows::Graphics::Imaging::SoftwareBitmap^ arg0 = dynamic_cast<::Windows::Graphics::Imaging::SoftwareBitmap^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          op = wrapper->_instance->SetBitmapAsync(arg0);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }

      auto opTask = create_task(op);
      uv_async_t* asyncToken = NodeUtils::Async::GetAsyncToken(info[info.Length() -1].As<Function>());

      opTask.then( [asyncToken] (task<void> t) {
        try {
          t.get();
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [](NodeUtils::InvokeCallbackDelegate invokeCallback) {


            Local<Value> args[] = {Undefined()};


            invokeCallback(_countof(args), args);
          });
        } catch (Platform::Exception^ exception) {
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [exception](NodeUtils::InvokeCallbackDelegate invokeCallback) {
            Local<Value> error = NodeRT::Utils::WinRtExceptionToJsError(exception);

            Local<Value> args[] = {error};
            invokeCallback(_countof(args), args);
          });
        }
      });
    }


    static void Close(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^>(info.This())) {
        return;
      }

      SoftwareBitmapSource *wrapper = SoftwareBitmapSource::Unwrap<SoftwareBitmapSource>(info.This());

      if (info.Length() == 0) {
        try {
          delete wrapper->_instance;
          wrapper->_instance = nullptr;
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      } else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }



    static void DispatcherGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::DependencyObject^>(info.This())) {
        return;
      }

      DependencyObject *wrapper = DependencyObject::Unwrap<DependencyObject>(info.This());

      try  {
        ::Windows::UI::Core::CoreDispatcher^ result = wrapper->_instance->Dispatcher;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Core", "CoreDispatcher", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    private:
      ::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapSoftwareBitmapSource(::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ UnwrapSoftwareBitmapSource(Local<Value> value);
  };

  Persistent<FunctionTemplate> SoftwareBitmapSource::s_constructorTemplate;

  v8::Local<v8::Value> WrapSoftwareBitmapSource(::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(SoftwareBitmapSource::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ UnwrapSoftwareBitmapSource(Local<Value> value) {
     return SoftwareBitmapSource::Unwrap<SoftwareBitmapSource>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitSoftwareBitmapSource(Local<Object> exports) {
    SoftwareBitmapSource::Init(exports);
  }

  class SurfaceImageSource : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("SurfaceImageSource").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);





          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("dispatcher").ToLocalChecked(), DispatcherGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("SurfaceImageSource").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      SurfaceImageSource(::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 2
        && info[0]->IsInt32()
        && info[1]->IsInt32())
      {
        try {
          int arg0 = static_cast<int>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          int arg1 = static_cast<int>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource(arg0,arg1);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 3
        && info[0]->IsInt32()
        && info[1]->IsInt32()
        && info[2]->IsBoolean())
      {
        try {
          int arg0 = static_cast<int>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          int arg1 = static_cast<int>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          bool arg2 = Nan::To<bool>(info[2]).FromMaybe(false);
          
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource(arg0,arg1,arg2);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      SurfaceImageSource *wrapperInstance = new SurfaceImageSource(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapSurfaceImageSource(winRtInstance));
    }





    static void DispatcherGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::DependencyObject^>(info.This())) {
        return;
      }

      DependencyObject *wrapper = DependencyObject::Unwrap<DependencyObject>(info.This());

      try  {
        ::Windows::UI::Core::CoreDispatcher^ result = wrapper->_instance->Dispatcher;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Core", "CoreDispatcher", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    private:
      ::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapSurfaceImageSource(::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ UnwrapSurfaceImageSource(Local<Value> value);
  };

  Persistent<FunctionTemplate> SurfaceImageSource::s_constructorTemplate;

  v8::Local<v8::Value> WrapSurfaceImageSource(::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(SurfaceImageSource::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::SurfaceImageSource^ UnwrapSurfaceImageSource(Local<Value> value) {
     return SurfaceImageSource::Unwrap<SurfaceImageSource>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitSurfaceImageSource(Local<Object> exports) {
    SurfaceImageSource::Init(exports);
  }

  class SvgImageSource : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("SvgImageSource").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);

        Local<Function> func;
        Local<FunctionTemplate> funcTemplate;


          
            Nan::SetPrototypeMethod(localRef, "setSourceAsync", SetSourceAsync);
          

          
          Nan::SetPrototypeMethod(localRef,"addListener", AddListener);
          Nan::SetPrototypeMethod(localRef,"on", AddListener);
          Nan::SetPrototypeMethod(localRef,"removeListener", RemoveListener);
          Nan::SetPrototypeMethod(localRef, "off", RemoveListener);

          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("uriSource").ToLocalChecked(), UriSourceGetter, UriSourceSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("rasterizePixelWidth").ToLocalChecked(), RasterizePixelWidthGetter, RasterizePixelWidthSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("rasterizePixelHeight").ToLocalChecked(), RasterizePixelHeightGetter, RasterizePixelHeightSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("dispatcher").ToLocalChecked(), DispatcherGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);

        Nan::SetAccessor(constructor, Nan::New<String>("rasterizePixelHeightProperty").ToLocalChecked(), RasterizePixelHeightPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("rasterizePixelWidthProperty").ToLocalChecked(), RasterizePixelWidthPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("uriSourceProperty").ToLocalChecked(), UriSourcePropertyGetter);


        Nan::Set(exports, Nan::New<String>("SvgImageSource").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      SvgImageSource(::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SvgImageSource^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 0)
      {
        try {
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::SvgImageSource();
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 1
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Foundation::Uri^>(info[0]))
      {
        try {
          ::Windows::Foundation::Uri^ arg0 = dynamic_cast<::Windows::Foundation::Uri^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::SvgImageSource(arg0);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      SvgImageSource *wrapperInstance = new SvgImageSource(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SvgImageSource^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapSvgImageSource(winRtInstance));
    }

    static void SetSourceAsync(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This())) {
        return;
      }

      if (info.Length() == 0 || !info[info.Length() -1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: No callback was given")));
        return;
      }

      SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());

      ::Windows::Foundation::IAsyncOperation<::Windows::UI::Xaml::Media::Imaging::SvgImageSourceLoadStatus>^ op;


      if (info.Length() == 2
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Streams::IRandomAccessStream^>(info[0]))
      {
        try
        {
          ::Windows::Storage::Streams::IRandomAccessStream^ arg0 = dynamic_cast<::Windows::Storage::Streams::IRandomAccessStream^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          op = wrapper->_instance->SetSourceAsync(arg0);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }

      auto opTask = create_task(op);
      uv_async_t* asyncToken = NodeUtils::Async::GetAsyncToken(info[info.Length() -1].As<Function>());

      opTask.then( [asyncToken] (task<::Windows::UI::Xaml::Media::Imaging::SvgImageSourceLoadStatus> t) {
        try {
          auto result = t.get();
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [result](NodeUtils::InvokeCallbackDelegate invokeCallback) {


            Local<Value> error;
            Local<Value> arg1;
            {
              TryCatch tryCatch;
              arg1 = Nan::New<Integer>(static_cast<int>(result));
              if (tryCatch.HasCaught())
              {
                error = Nan::To<Object>(tryCatch.Exception()).ToLocalChecked();
              }
              else
              {
                error = Undefined();
              }
              if (arg1.IsEmpty()) arg1 = Undefined();
            }
            Local<Value> args[] = {error, arg1};


            invokeCallback(_countof(args), args);
          });
        } catch (Platform::Exception^ exception) {
          NodeUtils::Async::RunCallbackOnMain(asyncToken, [exception](NodeUtils::InvokeCallbackDelegate invokeCallback) {
            Local<Value> error = NodeRT::Utils::WinRtExceptionToJsError(exception);

            Local<Value> args[] = {error};
            invokeCallback(_countof(args), args);
          });
        }
      });
    }




    static void UriSourceGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This())) {
        return;
      }

      SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());

      try  {
        ::Windows::Foundation::Uri^ result = wrapper->_instance->UriSource;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.Foundation", "Uri", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void UriSourceSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Foundation::Uri^>(value)) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This())) {
        return;
      }

      SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());

      try {

        ::Windows::Foundation::Uri^ winRtValue = dynamic_cast<::Windows::Foundation::Uri^>(NodeRT::Utils::GetObjectInstance(value));

        wrapper->_instance->UriSource = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void RasterizePixelWidthGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This())) {
        return;
      }

      SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());

      try  {
        double result = wrapper->_instance->RasterizePixelWidth;
        info.GetReturnValue().Set(Nan::New<Number>(static_cast<double>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void RasterizePixelWidthSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsNumber()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This())) {
        return;
      }

      SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());

      try {

        double winRtValue = Nan::To<double>(value).FromMaybe(0.0);

        wrapper->_instance->RasterizePixelWidth = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void RasterizePixelHeightGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This())) {
        return;
      }

      SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());

      try  {
        double result = wrapper->_instance->RasterizePixelHeight;
        info.GetReturnValue().Set(Nan::New<Number>(static_cast<double>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void RasterizePixelHeightSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsNumber()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This())) {
        return;
      }

      SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());

      try {

        double winRtValue = Nan::To<double>(value).FromMaybe(0.0);

        wrapper->_instance->RasterizePixelHeight = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void DispatcherGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::DependencyObject^>(info.This())) {
        return;
      }

      DependencyObject *wrapper = DependencyObject::Unwrap<DependencyObject>(info.This());

      try  {
        ::Windows::UI::Core::CoreDispatcher^ result = wrapper->_instance->Dispatcher;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Core", "CoreDispatcher", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    static void RasterizePixelHeightPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::SvgImageSource::RasterizePixelHeightProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void RasterizePixelWidthPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::SvgImageSource::RasterizePixelWidthProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void UriSourcePropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::SvgImageSource::UriSourceProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void AddListener(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"wrong arguments, expected arguments are eventName(string),callback(function)")));
        return;
      }

      String::Value eventName(v8::Isolate::GetCurrent(), info[0]);
      auto str = *eventName;

      Local<Function> callback = info[1].As<Function>();

      ::Windows::Foundation::EventRegistrationToken registrationToken;
      if (NodeRT::Utils::CaseInsenstiveEquals(L"openFailed", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This()))
        {
          Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
      return;
        }
        SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());
      
        try {
          Persistent<Object>* perstPtr = new Persistent<Object>();
          perstPtr->Reset(NodeRT::Utils::CreateCallbackObjectInDomain(callback));
          std::shared_ptr<Persistent<Object>> callbackObjPtr(perstPtr,
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Reset();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->OpenFailed::add(
            ref new ::Windows::Foundation::TypedEventHandler<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^, ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^>(
            [callbackObjPtr](::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ arg0, ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = WrapSvgImageSource(arg0);
                  wrappedArg1 = WrapSvgImageSourceFailedEventArgs(arg1);


                  if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                  if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();
                }

                Local<Value> args[] = { wrappedArg0, wrappedArg1 };
                Local<Object> callbackObjLocalRef = Nan::New<Object>(*callbackObjPtr);
                NodeRT::Utils::CallCallbackInDomain(callbackObjLocalRef, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }

      }
      else if (NodeRT::Utils::CaseInsenstiveEquals(L"opened", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This()))
        {
          Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
      return;
        }
        SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());
      
        try {
          Persistent<Object>* perstPtr = new Persistent<Object>();
          perstPtr->Reset(NodeRT::Utils::CreateCallbackObjectInDomain(callback));
          std::shared_ptr<Persistent<Object>> callbackObjPtr(perstPtr,
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Reset();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->Opened::add(
            ref new ::Windows::Foundation::TypedEventHandler<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^, ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^>(
            [callbackObjPtr](::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ arg0, ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = WrapSvgImageSource(arg0);
                  wrappedArg1 = WrapSvgImageSourceOpenedEventArgs(arg1);


                  if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                  if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();
                }

                Local<Value> args[] = { wrappedArg0, wrappedArg1 };
                Local<Object> callbackObjLocalRef = Nan::New<Object>(*callbackObjPtr);
                NodeRT::Utils::CallCallbackInDomain(callbackObjLocalRef, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }

      }
 else  {
        Nan::ThrowError(Nan::Error(String::Concat(v8::Isolate::GetCurrent(), NodeRT::Utils::NewString(L"given event name isn't supported: "), info[0].As<String>())));
        return;
      }

      Local<Value> tokenMapVal = NodeRT::Utils::GetHiddenValue(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked());
      Local<Object> tokenMap;

      if (tokenMapVal.IsEmpty() || Nan::Equals(tokenMapVal, Undefined()).FromMaybe(false)) {
        tokenMap = Nan::New<Object>();
        NodeRT::Utils::SetHiddenValueWithObject(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked(), tokenMap);
      } else {
        tokenMap = Nan::To<Object>(tokenMapVal).ToLocalChecked();
      }

      Nan::Set(tokenMap, info[0], CreateOpaqueWrapper(::Windows::Foundation::PropertyValue::CreateInt64(registrationToken.Value)));
    }

    static void RemoveListener(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"wrong arguments, expected a string and a callback")));
        return;
      }

      String::Value eventName(v8::Isolate::GetCurrent(), info[0]);
      auto str = *eventName;

      if ((!NodeRT::Utils::CaseInsenstiveEquals(L"openFailed", str)) &&(!NodeRT::Utils::CaseInsenstiveEquals(L"opened", str))) {
        Nan::ThrowError(Nan::Error(String::Concat(v8::Isolate::GetCurrent(), NodeRT::Utils::NewString(L"given event name isn't supported: "), info[0].As<String>())));
        return;
      }

      Local<Function> callback = info[1].As<Function>();
      Local<Value> tokenMap = NodeRT::Utils::GetHiddenValue(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked());

      if (tokenMap.IsEmpty() || Nan::Equals(tokenMap, Undefined()).FromMaybe(false)) {
        return;
      }

      Local<Value> opaqueWrapperObj =  Nan::Get(Nan::To<Object>(tokenMap).ToLocalChecked(), info[0]).ToLocalChecked();

      if (opaqueWrapperObj.IsEmpty() || Nan::Equals(opaqueWrapperObj,Undefined()).FromMaybe(false)) {
        return;
      }

      OpaqueWrapper *opaqueWrapper = OpaqueWrapper::Unwrap<OpaqueWrapper>(opaqueWrapperObj.As<Object>());

      long long tokenValue = (long long) opaqueWrapper->GetObjectInstance();
      ::Windows::Foundation::EventRegistrationToken registrationToken;
      registrationToken.Value = tokenValue;

      try  {
        if (NodeRT::Utils::CaseInsenstiveEquals(L"openFailed", str)) {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This()))
          {
            Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return;
          }
          SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());
          wrapper->_instance->OpenFailed::remove(registrationToken);
        }
        else if (NodeRT::Utils::CaseInsenstiveEquals(L"opened", str))
        {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSource^>(info.This()))
          {
            Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return;
          }
          SvgImageSource *wrapper = SvgImageSource::Unwrap<SvgImageSource>(info.This());
          wrapper->_instance->Opened::remove(registrationToken);
        }
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }

      Nan::Delete(Nan::To<Object>(tokenMap).ToLocalChecked(), Nan::To<String>(info[0]).ToLocalChecked());
    }
    private:
      ::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapSvgImageSource(::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ UnwrapSvgImageSource(Local<Value> value);
  };

  Persistent<FunctionTemplate> SvgImageSource::s_constructorTemplate;

  v8::Local<v8::Value> WrapSvgImageSource(::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(SvgImageSource::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::SvgImageSource^ UnwrapSvgImageSource(Local<Value> value) {
     return SvgImageSource::Unwrap<SvgImageSource>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitSvgImageSource(Local<Object> exports) {
    SvgImageSource::Init(exports);
  }

  class SvgImageSourceFailedEventArgs : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("SvgImageSourceFailedEventArgs").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);





          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("status").ToLocalChecked(), StatusGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("SvgImageSourceFailedEventArgs").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      SvgImageSourceFailedEventArgs(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      SvgImageSourceFailedEventArgs *wrapperInstance = new SvgImageSourceFailedEventArgs(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapSvgImageSourceFailedEventArgs(winRtInstance));
    }





    static void StatusGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^>(info.This())) {
        return;
      }

      SvgImageSourceFailedEventArgs *wrapper = SvgImageSourceFailedEventArgs::Unwrap<SvgImageSourceFailedEventArgs>(info.This());

      try  {
        ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceLoadStatus result = wrapper->_instance->Status;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    private:
      ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapSvgImageSourceFailedEventArgs(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ UnwrapSvgImageSourceFailedEventArgs(Local<Value> value);
  };

  Persistent<FunctionTemplate> SvgImageSourceFailedEventArgs::s_constructorTemplate;

  v8::Local<v8::Value> WrapSvgImageSourceFailedEventArgs(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(SvgImageSourceFailedEventArgs::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceFailedEventArgs^ UnwrapSvgImageSourceFailedEventArgs(Local<Value> value) {
     return SvgImageSourceFailedEventArgs::Unwrap<SvgImageSourceFailedEventArgs>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitSvgImageSourceFailedEventArgs(Local<Object> exports) {
    SvgImageSourceFailedEventArgs::Init(exports);
  }

  class SvgImageSourceOpenedEventArgs : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("SvgImageSourceOpenedEventArgs").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);






        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("SvgImageSourceOpenedEventArgs").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      SvgImageSourceOpenedEventArgs(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      SvgImageSourceOpenedEventArgs *wrapperInstance = new SvgImageSourceOpenedEventArgs(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapSvgImageSourceOpenedEventArgs(winRtInstance));
    }







    private:
      ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapSvgImageSourceOpenedEventArgs(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ UnwrapSvgImageSourceOpenedEventArgs(Local<Value> value);
  };

  Persistent<FunctionTemplate> SvgImageSourceOpenedEventArgs::s_constructorTemplate;

  v8::Local<v8::Value> WrapSvgImageSourceOpenedEventArgs(::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(SvgImageSourceOpenedEventArgs::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::SvgImageSourceOpenedEventArgs^ UnwrapSvgImageSourceOpenedEventArgs(Local<Value> value) {
     return SvgImageSourceOpenedEventArgs::Unwrap<SvgImageSourceOpenedEventArgs>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitSvgImageSourceOpenedEventArgs(Local<Object> exports) {
    SvgImageSourceOpenedEventArgs::Init(exports);
  }

  class VirtualSurfaceImageSource : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("VirtualSurfaceImageSource").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);





          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("dispatcher").ToLocalChecked(), DispatcherGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("VirtualSurfaceImageSource").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      VirtualSurfaceImageSource(::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 2
        && info[0]->IsInt32()
        && info[1]->IsInt32())
      {
        try {
          int arg0 = static_cast<int>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          int arg1 = static_cast<int>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource(arg0,arg1);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 3
        && info[0]->IsInt32()
        && info[1]->IsInt32()
        && info[2]->IsBoolean())
      {
        try {
          int arg0 = static_cast<int>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          int arg1 = static_cast<int>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          bool arg2 = Nan::To<bool>(info[2]).FromMaybe(false);
          
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource(arg0,arg1,arg2);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      VirtualSurfaceImageSource *wrapperInstance = new VirtualSurfaceImageSource(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapVirtualSurfaceImageSource(winRtInstance));
    }





    static void DispatcherGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::DependencyObject^>(info.This())) {
        return;
      }

      DependencyObject *wrapper = DependencyObject::Unwrap<DependencyObject>(info.This());

      try  {
        ::Windows::UI::Core::CoreDispatcher^ result = wrapper->_instance->Dispatcher;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Core", "CoreDispatcher", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    private:
      ::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapVirtualSurfaceImageSource(::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ UnwrapVirtualSurfaceImageSource(Local<Value> value);
  };

  Persistent<FunctionTemplate> VirtualSurfaceImageSource::s_constructorTemplate;

  v8::Local<v8::Value> WrapVirtualSurfaceImageSource(::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(VirtualSurfaceImageSource::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::VirtualSurfaceImageSource^ UnwrapVirtualSurfaceImageSource(Local<Value> value) {
     return VirtualSurfaceImageSource::Unwrap<VirtualSurfaceImageSource>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitVirtualSurfaceImageSource(Local<Object> exports) {
    VirtualSurfaceImageSource::Init(exports);
  }

  class WriteableBitmap : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("WriteableBitmap").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);


          
            Nan::SetPrototypeMethod(localRef, "invalidate", Invalidate);
          



          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelBuffer").ToLocalChecked(), PixelBufferGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelHeight").ToLocalChecked(), PixelHeightGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("pixelWidth").ToLocalChecked(), PixelWidthGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("dispatcher").ToLocalChecked(), DispatcherGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);

        Nan::SetAccessor(constructor, Nan::New<String>("pixelHeightProperty").ToLocalChecked(), PixelHeightPropertyGetter);
        Nan::SetAccessor(constructor, Nan::New<String>("pixelWidthProperty").ToLocalChecked(), PixelWidthPropertyGetter);


        Nan::Set(exports, Nan::New<String>("WriteableBitmap").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      WriteableBitmap(::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 2
        && info[0]->IsInt32()
        && info[1]->IsInt32())
      {
        try {
          int arg0 = static_cast<int>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          int arg1 = static_cast<int>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          
          winRtInstance = ref new ::Windows::UI::Xaml::Media::Imaging::WriteableBitmap(arg0,arg1);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      WriteableBitmap *wrapperInstance = new WriteableBitmap(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapWriteableBitmap(winRtInstance));
    }


    static void Invalidate(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^>(info.This())) {
        return;
      }

      WriteableBitmap *wrapper = WriteableBitmap::Unwrap<WriteableBitmap>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->Invalidate();
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }



    static void PixelBufferGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^>(info.This())) {
        return;
      }

      WriteableBitmap *wrapper = WriteableBitmap::Unwrap<WriteableBitmap>(info.This());

      try  {
        ::Windows::Storage::Streams::IBuffer^ result = wrapper->_instance->PixelBuffer;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.Storage.Streams", "IBuffer", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void PixelHeightGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info.This())) {
        return;
      }

      BitmapSource *wrapper = BitmapSource::Unwrap<BitmapSource>(info.This());

      try  {
        int result = wrapper->_instance->PixelHeight;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void PixelWidthGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::BitmapSource^>(info.This())) {
        return;
      }

      BitmapSource *wrapper = BitmapSource::Unwrap<BitmapSource>(info.This());

      try  {
        int result = wrapper->_instance->PixelWidth;
        info.GetReturnValue().Set(Nan::New<Integer>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void DispatcherGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::DependencyObject^>(info.This())) {
        return;
      }

      DependencyObject *wrapper = DependencyObject::Unwrap<DependencyObject>(info.This());

      try  {
        ::Windows::UI::Core::CoreDispatcher^ result = wrapper->_instance->Dispatcher;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Core", "CoreDispatcher", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    static void PixelHeightPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapSource::PixelHeightProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    static void PixelWidthPropertyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      try
      {
        ::Windows::UI::Xaml::DependencyProperty^ result = ::Windows::UI::Xaml::Media::Imaging::BitmapSource::PixelWidthProperty;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.UI.Xaml", "DependencyProperty", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      

    private:
      ::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapWriteableBitmap(::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ UnwrapWriteableBitmap(Local<Value> value);
  };

  Persistent<FunctionTemplate> WriteableBitmap::s_constructorTemplate;

  v8::Local<v8::Value> WrapWriteableBitmap(::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(WriteableBitmap::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ UnwrapWriteableBitmap(Local<Value> value) {
     return WriteableBitmap::Unwrap<WriteableBitmap>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitWriteableBitmap(Local<Object> exports) {
    WriteableBitmap::Init(exports);
  }

  class XamlRenderingBackgroundTask : public WrapperBase {
    public:
      
      static void Init(const Local<Object> exports) {
        HandleScope scope;

        Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
        s_constructorTemplate.Reset(localRef);
        localRef->SetClassName(Nan::New<String>("XamlRenderingBackgroundTask").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);


          
            Nan::SetPrototypeMethod(localRef, "onRun", OnRun);
          




        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("XamlRenderingBackgroundTask").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      XamlRenderingBackgroundTask(::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ instance) {
        _instance = instance;
      }

      
    static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This())) {
        if (info.Length() > 0) {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++) {
            argsPtr[i] = info[i];
          }

          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        } else {
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);

          if (res.IsEmpty()) {
            return;
          }

          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }

      ::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^>(info[0])) {
        try {
          winRtInstance = (::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      XamlRenderingBackgroundTask *wrapperInstance = new XamlRenderingBackgroundTask(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


      
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;
      if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^>(info[0])) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
        return;
      }

      ::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ winRtInstance;
      try {
        winRtInstance = (::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapXamlRenderingBackgroundTask(winRtInstance));
    }


    static void OnRun(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^>(info.This())) {
        return;
      }

      XamlRenderingBackgroundTask *wrapper = XamlRenderingBackgroundTask::Unwrap<XamlRenderingBackgroundTask>(info.This());

      if (info.Length() == 1
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Background::IBackgroundTaskInstance^>(info[0]))
      {
        try
        {
          ::Windows::ApplicationModel::Background::IBackgroundTaskInstance^ arg0 = dynamic_cast<::Windows::ApplicationModel::Background::IBackgroundTaskInstance^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          wrapper->_instance->OnRun(arg0);
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
 else {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }





    private:
      ::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapXamlRenderingBackgroundTask(::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ wintRtInstance);
      friend ::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ UnwrapXamlRenderingBackgroundTask(Local<Value> value);
  };

  Persistent<FunctionTemplate> XamlRenderingBackgroundTask::s_constructorTemplate;

  v8::Local<v8::Value> WrapXamlRenderingBackgroundTask(::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ winRtInstance) {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr) {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(XamlRenderingBackgroundTask::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::UI::Xaml::Media::Imaging::XamlRenderingBackgroundTask^ UnwrapXamlRenderingBackgroundTask(Local<Value> value) {
     return XamlRenderingBackgroundTask::Unwrap<XamlRenderingBackgroundTask>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitXamlRenderingBackgroundTask(Local<Object> exports) {
    XamlRenderingBackgroundTask::Init(exports);
  }


} } } } } } 

NAN_MODULE_INIT(init) {
  // We ignore failures for now since it probably means that
  // the initialization already happened for STA, and that's cool

  CoInitializeEx(nullptr, COINIT_MULTITHREADED);

  /*
  if (FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED))) {
    Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"error in CoInitializeEx()")));
    return;
  }
  */

      NodeRT::Windows::UI::Xaml::Media::Imaging::InitBitmapCreateOptionsEnum(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitDecodePixelTypeEnum(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitSvgImageSourceLoadStatusEnum(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitBitmapSource(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitBitmapImage(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitDownloadProgressEventArgs(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitRenderTargetBitmap(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitSoftwareBitmapSource(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitSurfaceImageSource(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitSvgImageSource(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitSvgImageSourceFailedEventArgs(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitSvgImageSourceOpenedEventArgs(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitVirtualSurfaceImageSource(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitWriteableBitmap(target);
      NodeRT::Windows::UI::Xaml::Media::Imaging::InitXamlRenderingBackgroundTask(target);


  NodeRT::Utils::RegisterNameSpace("Windows.UI.Xaml.Media.Imaging", target);
}



NODE_MODULE(binding, init)
