XAML中定义的元素不存在。在C#代码后面

发布于 2025-02-11 16:08:03 字数 2047 浏览 2 评论 0原文

我开始学习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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2025-02-18 16:08:03

这是Xamarin在VS上的常见问题,尤其是在Mac上,发生的事情是您的调试文件不会在XAML.G.CS文件上生成XAML更改。基本上,这是在编译器结合XAML和XAML.CS文件的情况下自动化的文件。

要正确更新所需要做的就是要评论以下代码行:

car.FadeTo(0, 2000);

一旦您执行此操作,就可以进行干净的构建或重建。一旦您的应用程序构建成功地返回此代码线并取消注销,这应该开始工作

好运!

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文