简体   繁体   English

在xaml中定义的元素在c#代码后面“不存在”

[英]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.我已经开始学习 xamarin 表单,现在我已经开始将动画应用于我的图像。 But I've got an error in my code.但是我的代码中有一个错误。

Here's my code in xaml.cs:这是我在 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错误 CS0103 当前上下文 teste2 中不存在名称“汽车”

And here's my xaml code:这是我的 xaml 代码:

<?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.这是 VS 上的 Xamarin 的常见问题,尤其是在 MAC 上,发生的情况是您的调试文件不会对 xaml.g.cs 文件生成 XAML 更改。 Which is basically the file that is autogenerated where our compiler combines the xaml and xaml.cs file.这基本上是我们的编译器结合 xaml 和 xaml.cs 文件时自动生成的文件。

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 !祝你好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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