2

I'm learning DirectX but I don't know C++ very well so I decided to use SharpDX instead. But I'm stuck on this part in C++ they use the address of a function in the TypedEventHandler, but I don't know how to write that in C#? Can anyone help?

ref class App sealed : public IFrameworkView
{
public:
virtual void Initialize(CoreApplicationView^ AppView)
{
    AppView->Activated += ref new TypedEventHandler
        <CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
}
virtual void SetWindow(CoreWindow^ Window) {}
virtual void Load(String^ EntryPoint) {}
virtual void Run() {}
virtual void Uninitialize() {}

void OnActivated(CoreApplicationView^ CoreAppView, IActivatedEventArgs^ Args)
{
    CoreWindow^ Window = CoreWindow::GetForCurrentThread();
    Window->Activate();
}
};

My conversion so far

internal class App : IFrameworkView
{
    public void Initialize(CoreApplicationView AppView)
    {
        // Call OnActivated() when the Activated event is triggered
        AppView.Activated += new TypedEventHandler<CoreApplicationView, IActivatedEventArgs>(this,this.OnActivated);

    }
    public void SetWindow(CoreWindow Window) {}
    public void Load(String EntryPoint) {}
    public void Run() {}
    public void Uninitialize() {}

    public void OnActivated(CoreApplicationView CoreAppView, IActivatedEventArgs Args)
    {
        CoreWindow Window = CoreWindow.GetForCurrentThread();
        Window.Activate();
    }

}
5
  • Where did you find ref new? That's not legal. The rest of your code is C++/CLI, not standard C++. C++/CLI should be very straightforward to translate into C#.
    – Ben Voigt
    Commented Jun 27, 2013 at 2:20
  • @KenKin: No, C++/CLI uses gcnew. Is this C++/CX?
    – Ben Voigt
    Commented Jun 27, 2013 at 2:25
  • Yeah it is. Fixing tags.
    – Ben Voigt
    Commented Jun 27, 2013 at 2:26
  • its C++/CX for Windows Store apps
    – erotavlas
    Commented Jun 27, 2013 at 2:46
  • @erotavlas: It would be better to state that in the question, but now it's retagged finely.
    – Ken Kin
    Commented Jun 27, 2013 at 2:48

1 Answer 1

6

You don't need the "this" argument for the C# event wireup:

public sealed class App : IFrameworkView
{
    public virtual void Initialize(CoreApplicationView AppView)
    {
        AppView.Activated += new TypedEventHandler <CoreApplicationView, IActivatedEventArgs>(OnActivated);
    }

    public virtual void SetWindow(CoreWindow Window)
    {
    }
    public virtual void Load(string EntryPoint)
    {
    }
    public virtual void Run()
    {
    }
    public virtual void Uninitialize()
    {
    }

    public void OnActivated(CoreApplicationView CoreAppView, IActivatedEventArgs Args)
    {
        CoreWindow Window = CoreWindow.GetForCurrentThread();
        Window.Activate();
    }
}
2
  • 3
    Should be able to shorten that to AppView.Activated += OnActivated;
    – Ben Voigt
    Commented Jun 27, 2013 at 2:21
  • Absolutely (although that won't work in older versions of C#). Commented Jun 27, 2013 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.