1
Vote

NullReferenceException using EventHandlers.Attach

description

First time I've seen something like this been using StyleMVVM a while in quite a few projects and have always used view:EventHandlers.Attach.

I'm working on porting over one of our windows phone applications to use StyleMVVM as well, once I add the above to an element on a page, during LoadComponent() call in InitalizeComponent() I get a NullReferenceException,

Source: StyleMVVM.Phone
StackTrace: at StyleMVVM.View.EventHandlerInstance.<>c__DisplayClass5`1.<GenericConnectHandler>b__1(T a)

Not sure what I'm doing differently/wrong here, page is just a BasePhoneApplicationPage. Pages without EventHandlers seem to work and even hook up to VM properly, but once I add it I see this issue.

Any insight you have would be much appreciated

comments

ipjohnson123 wrote May 14, 2014 at 8:46 PM

Hi KJee85,

Just so I can get up to speed can you provide a little more information that will help me track this down

1) Which version of windows phone (8 or 8.1)?
2) Which version of StyleMVVM
3) Can you send the exact syntax of the line you are using to attach (i.e. EventHandlers.Attach="Event => Method()"

Also if it's at all possible to recreate this problem in an example app it would be super helpful.

This evening I'll create a sample app and see if I have the same problem.

Sorry for the inconvenience

-Ian

KJee85 wrote May 14, 2014 at 10:45 PM

Sure thing.
  1. Windows Phone 8
  2. StyleMVVM 3.1.5 (Added via nuget)
  3. Here is what I used to test that it was this. Our first page is just a startup page, that is a simple grid with a progress bar, no event handlers. I was able to step into the VM OnNavigatedTo from here after loading some data I navigate to our main page, which has event handlers, and I saw the VM for the main page be created but the exception was thrown when creating the actual page.
So to make sure it was nothing weird in the main page I added a button to our startup page
<Button Margin="0,300,0,0" Content="Test" view:EventHandlers.Attach="Click => Test();"/>

and in VM:

public void Test() {}

After adding this, I then saw the same exception on app launch as it created the startup page. The main page and the rest of our pages uses this same syntax on buttons and some other elements using mostly Click/Tap/SelectionChanged events.

One thing that may or may not help, this is actually the only way I can attach an event, the longhand way for whatever reason is not working. I can add:

<view:EventHandlers.List>
            <view:EventHandlerList>

            </view:EventHandlerList>
</view:EventHandlers.List>

but when I go to put in the EventHandlerInstance line, intellisense only shows EventHandlerList as an option and if I just put the line (<view:EventHandlerInstance Attach="Click => Test();"/>) in I get: "Cannot add content to type EventHandlerList"

So not sure if that points to maybe a larger reference issue going on here, I've added StyleMVVM via nuget to other projects the same way and have never had an issue.

Thanks for the quick response and let me know if there is anything more I can do to help resolve this.

ipjohnson123 wrote May 15, 2014 at 12:53 AM

Interesting, well if I remember correctly the event connecting code for Windows Phone 8 was different than it was for Windows 8 & .Net 4.5. To be honest with you it's my least favorite of all the XAML platforms because it seems to have more idiosyncrasies compared to all the other platforms (And that's saying something).

I'll give it a whirl here in a minute and see what I glean, as you said everything you are doing is straightforward and should just work.

For 4.0 I'm going to be targeting .Net 4.5 & Windows 8.1 Portable (WinRT & Windows Phone) because I really don't want to have to support Windows Phone 8.

ipjohnson123 wrote May 15, 2014 at 1:19 AM

Ok so I created a test app, added a button and setup the EventHandler.Attach and it worked so I'm thinking its a little more complicated than it not working out right.

I'd be willing to look at your app for you but I'm a little at a loss for why this might be happening because I can't recreate the problem.

If you can recreate the problem in a little test app that would help significantly

KJee85 wrote May 15, 2014 at 1:55 AM

So, I had another dev take these changes and on his machine it works, not sure why mine would suddenly stop working, to my knowledge the other apps I have that use StyleMVVM are still working fine. Maybe I need to blow away my workspace or re-add the nuget. Thanks for the quick response.

ipjohnson123 wrote May 15, 2014 at 2:58 AM

Very odd but I've seen projects go sideways and the only way to get them to work again is blowing away the solution and grabbing latest.

I'm glad to hear some people are using StyleMVVM, you write code for yourself but you hope its useful for more than just you :)

KJee85 wrote May 15, 2014 at 6:48 PM

Can confirm a workspace wipe fixed this, after getting latest it worked.