[英]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.