Xamarin 表单无法通过“x:Name”访问控件

问题描述 投票:0回答:3

今天我遇到了一些问题。请帮助我。

我在 IOS 和 Android 上创建了一个跨平台应用程序。所以我尝试访问我的 MainPage 控件表单 .cs 文件,但我不能。这里提供了代码。

主页.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Movies"
             x:Class="Movies.MainPage">


    <TableView x:Name="mainTable">
        <TableSection>
            <ViewCell>
                <ViewCell.View>
                    <Label Text="Hello"></Label>
                </ViewCell.View>
            </ViewCell>
        </TableSection>
    </TableView>

</ContentPage>

MainPage.xaml.cs

using Xamarin.Forms;
namespace Movies
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            mainTable.
        }
    }
}

https://i.stack.imgur.com/2XZS1.png

项目运行成功。

https://i.stack.imgur.com/HGMwW.png

我尝试在 VS 安装程序中重建甚至恢复 VS17。 this.mainTable 也不起作用。

xamarin xamarin.forms
3个回答
2
投票

单击 .XAML 文件,选择属性。

查看名为“自定义工具”的属性。将其值从 MSBuild:Compile 更改为 MSBuild:UpdateDesignTimeXaml,或者如果它为空,则将其设置为 MSBuild:UpdateDesignTimeXaml。

这样就可以解决问题了。


2
投票

构建一次项目,然后就可以在.cs文件中访问该名称。


0
投票

您可能需要先构建解决方案,然后删除项目文件夹根目录中隐藏的 .vs 文件夹,然后重新打开。

© www.soinside.com 2019 - 2024. All rights reserved.