Marcel
Marcel

Reputation: 927

Xamarin Forms Android MethodAccessException

Since the upgrade to 15.5.1, my Xamarin project doesn't seem to work anymore.

When loading a page, I get this error:

System.MethodAccessException: Method Xamarin.Forms.View:.ctor ()' is inaccessible from method MyApp.MyPage.InitializeComponent ()'

However, this page is built exactly like my other pages which are working and I have no idea where to start searching. Any ideas where this comes from? I use Xamarin.Forms plugin version 2.5.0.121934.

Edit: This is the content of the auto generated file where the exception comes up:

public partial class MyPage : global::Xamarin.Forms.ContentPage {
         [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent() {
            global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(MyPage));
        }
    }

Edit 2:

namespace MyApp
{
    public partial class MyPage : ContentPage
    {
        MyPageVM viewModel;
        public PNewWeighing(String information)
        {
            this.InitializeComponent();

            viewModel = new MyPageVM();
            viewModel.information = information;
            BindingContext = viewModel;
        }

        protected override bool OnBackButtonPressed()
        {
            viewModel.OnNavigateCancelCommand();
            return true;
        }
    }
}

And

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="MyApp.MyPage"
    x:Name="MyPage"
    NavigationPage.HasBackButton="False"
    NavigationPage.HasNavigationBar="False">
<ContentPage.Padding>
    <OnPlatform 
            x:TypeArguments="Thickness"
            iOS="20, 40, 20, 20"
            Android="20, 20, 20, 20"
            WinPhone="20, 20, 20, 20" />
</ContentPage.Padding>
<ContentPage.Content>
    <ScrollView>
    <Grid
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand"
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

        <Label
            Grid.Row="0"
            Grid.ColumnSpan="2"
            Text="{Binding Text}"
            FontAttributes="Bold" />

        <!-- Placeholder -->
        <View
            Grid.Row="1"
            Grid.ColumnSpan="2" />

        <!-- General controls -->

    </Grid>
    </ScrollView>
</ContentPage.Content>

Upvotes: 0

Views: 1402

Answers (2)

Mohamed Ali
Mohamed Ali

Reputation: 3995

I've faced the same exception but because of inclusion of a custom view inside the page, making the custom view constructor public solved it for me

Upvotes: 4

W0RT4
W0RT4

Reputation: 458

Im not sure but can u try use this.InitializeComponent() in page constructor?

Upvotes: 2

Related Questions