samedi 29 juin 2013

WP7 binding problem: System.MethodAccessException: Attempt to access the method failed

This an easy and simple problem I faced recently but I waste a lot of time to understand it

For me this problem occured with VS2012 and WP7 project! 

So I explained my project was a simple soundboard.
This soundoard is just composed by a ListBox, and the datatemplate is composed by two buttons and one textbox.

When I build the project and I run it to the emulator

  1. It tooks a lot of time to load and run
  2. Buttons and text filed are empty
But in the Xaml and code, every fields are binded correctly.

Just by looking at the output windows, I had this message

"System.Windows.Data Error: Cannot get 'Description' value (type 'System.String') from 'BoardField' (type 'BoardField'). BindingExpression: Path='Description' DataItem='BoardField' (HashCode=84854501); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt to access the method failed: .BoardField.get_Description()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obA first chance exception of type 'Microsoft.Advertising.Shared.AdException' occurred in Microsoft.Advertising.Mobile.dll
A first chance exception of type 'Microsoft.Advertising.Shared.AdException' occurred in Microsoft.Advertising.Mobile.dll
A first chance exception of type 'Microsoft.Advertising.Shared.AdException' occurred in Microsoft.Advertising.Mobile.dll
A first chance exception of type 'Microsoft.Advertising.Shared.AdException' occurred in Microsoft.Advertising.Mobile.dll
A first chance exception of type 'Microsoft.Advertising.Shared.AdException' occurred in Microsoft.Advertising.Mobile.dll
The program '[247333058] TaskHost.exe: Managed' has exited with code 0 (0x0)."


So, it seems that there is an accesibility problem.. By reading the info coming from this exception. It looks like it came from the class that contains data. Here is the class that cause problem


Code:
class BoardField
    {
        public string Hero { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
        public string Description { get; set; }
        public string Language { get; set; }
    }

Just by adding the accessor "public" 
Code:
public class BoardField
    {
        public string Hero { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
        public string Description { get; set; }
        public string Language { get; set; }
    }


So it seems that the for a WP7 project on WS2012 has wrong settings at project start.

Good Work