首页 > 解决方案 > uwp c# pass additional parameter to an navigationview event handler while binding the event at runtime

问题描述

I have a navigationview and I want to pass extra argument to selection_changed event

MUXC.NavigationView navigationview = new MUXC.NavigationView();
navigationview.SelectionChanged += new EventHandler((s, e) => Navigationview_SelectionChanged(s, e, param));

getting error for above code

Cannot implicitly convert type 'System.Eventhandler' to 'Windows.Foundation.TypedEventHandler'

标签: c#uwpevent-handlingarguments

解决方案


When attaching events, there is usually no need to new an EventHandler, because the type of NavigationView.SelectionChanged is TypedEventHandler, direct assignment will cause the type to mismatch.

If you create a SelectionChanged event handle method, you can attach it like this:

var navigationview = new muxc.NavigationView();
navigationview.SelectionChanged += Navigationview_SelectionChanged;

private void Navigationview_SelectionChanged(muxc.NavigationView sender, muxc.NavigationViewSelectionChangedEventArgs args)
{
    // Do something...
}

From your code, you seem to have created a method with three parameters. If you need to keep the param, then you need to perform some conversion.

navigationview.SelectionChanged += (_s, _e) =>
{
    Navigationview_SelectionChanged(_s, _e, param);
};

推荐阅读