简体   繁体   中英

element defined in xaml "does not exist" in c# code behind

I´ve started to learn xamarin forms and now I'm already starting to apply animation to my Image. But I've got an error in my code.

Here's my code in xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace teste2
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        
        public Page1()
        {
            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            // The animation
            car.FadeTo(0, 2000);
        }
    }
}

And here's my error:

Error CS0103 Name 'car' does not exist in current context teste2

And here's my xaml code:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="teste2.TabsPage" BarBackgroundColor="#508ca7"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core" 
             android:TabbedPage.ToolbarPlacement="Bottom">
    <TabbedPage.Children >
        <ContentPage Title="Voiture" IconImageSource="lab_icon_voiture.png" BackgroundColor="#DCCECD">
            <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="Center">
                <!-- The image that i want to animate-->
                <Image Source="lab_voiture.png" x:Name="car"/>
            </StackLayout>
           
        </ContentPage>
        <ContentPage Title="Avion" IconImageSource="lab_icon_avion.png" BackgroundColor="#f4f4f4">
            <StackLayout Orientation="Vertical" HorizontalOptions="Center" VerticalOptions="CenterAndExpand">
                <Image Source="lab_avion.png" x:Name="plane"/>
            </StackLayout>
        </ContentPage>
    </TabbedPage.Children>
</TabbedPage>

This is a common issue with Xamarin on VS, especially on MAC, what happens is that your debug file does not generate the XAML changes onto the xaml.g.cs file. Which is basically the file that is autogenerated where our compiler combines the xaml and xaml.cs file.

All you need to do for this to update correctly is to comment the following line of code:

car.FadeTo(0, 2000);

Once you do this go for a clean build or a rebuild. once your app builds successfully go back to this line of code again and uncomment it and this should start working

Goodluck !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM