繁体   English   中英

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

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

我已经开始学习 xamarin 表单,现在我已经开始将动画应用于我的图像。 但是我的代码中有一个错误。

这是我在 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);
        }
    }
}

这是我的错误:

错误 CS0103 当前上下文 teste2 中不存在名称“汽车”

这是我的 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>

这是 VS 上的 Xamarin 的常见问题,尤其是在 MAC 上,发生的情况是您的调试文件不会对 xaml.g.cs 文件生成 XAML 更改。 这基本上是我们的编译器结合 xaml 和 xaml.cs 文件时自动生成的文件。

要正确更新,您需要做的就是注释以下代码行:

car.FadeTo(0, 2000);

完成此操作后,请进行干净的构建或重建。 一旦您的应用程序构建成功,请再次返回这行代码并取消注释它应该开始工作

祝你好运 !

暂无
暂无

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

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