c# - Xamarin 无法访问隐藏代码的控件

标签 c# .net xamarin code-behind xamarin.forms

对于我的 Xamarin Forms 项目(最新版本 1.3),当我在 Xamarin Studio(最新版本 5.5.4)中创建 View 时,我将 View 定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="FormsDemo.Register">
  <ContentPage.Content>

    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>

      <Label Grid.Row="0" Grid.Column="0" Text="First Name" />
      <Entry x:Name="FirstName" Grid.Row="0" Grid.Column="1" />

      <Label Grid.Row="1" Grid.Column="0" Text="Last Name" />
      <Entry x:Name="LastName" Grid.Row="1" Grid.Column="1" />

      <Label Grid.Row="2" Grid.Column="0" Text="Email" />
      <Entry x:Name="Email" Grid.Row="2" Grid.Column="1" />

      <Button x:Name="SaveButton" Grid.Row="3" Grid.Column="0" Text="Login" />
    </Grid>

  </ContentPage.Content>
</ContentPage>

但是,在我的代码隐藏中,根本找不到任何按名称引用控件的内容。以下代码是错误的(由注释指出):

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace FormsDemo
{ 
  public partial class Register : ContentPage
  { 
    public Register ()
    {
      InitializeComponent ();

            //Using this approach worked, but is not ideal
      this.FindByName<Button>("SaveButton").Clicked += Save_Clicked;
    }

    void Save_Clicked (object sender, EventArgs e)
    {
      this.FirstName.Text = "A";
      this.LastName.Text= "B";
    }
  }
}

奇怪的是,我收到警告:

Warning: The private field `FormsDemo.Register.FirstName' is assigned but its value is never used (FormsDemo.Solution)

我为每个领域得到一个。为什么这不起作用?如何让 IntelliSense 工作?

最佳答案

最新的更新似乎解决了这个问题;这些引用工作正常:

this.FirstName.Text = "A";
this.LastName.Text= "B";

看来设计者的一些问题已解决。

关于c# - Xamarin 无法访问隐藏代码的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27754000/

相关文章:

c# - ReplayKit 制作的视频在 Xamarin 中不断跳帧

c# - SetColorFilter 在 Xamarin.Android 中不起作用

c# - 如何将实体推送到 Rx Observable 上?

c# - asp.net 母版页中的 Javascript 代码依赖于当前打开的页面

.net - Microsoft Unity 中的转发类型

.net - Visual Studio 2010 可以测试 .net 3.5 SP1 项目吗?

c# - 如何在 C# 中使用 XMLDocument 删除 XML 文件的第一行?

c# - DSA 使用相同的数据生成不同的签名

c# - Uri.IsWellFormedUriString 用于相对 Hashbang url 兼容性

iOS 应用程序开发 Windows 上的 PhoneGap 或 Xamarin