【发布时间】:2015-03-01 11:36:48
【问题描述】:
对于我的 Xamarin Forms 项目(最新版本 1.3),当我在 Xamarin Studio(最新版本,5.5.4)中创建视图时,我将视图定义如下:
<?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>
但是,在我的代码隐藏中,根本找不到任何通过其名称对控件的引用。以下代码出错(由 cmets 指出):
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";
}
}
}
奇怪的是,我收到了以下警告:
警告:分配了私有字段“FormsDemo.Register.FirstName”,但从未使用过它的值 (FormsDemo.Solution)
我为每个字段获得一个。为什么这不起作用?如何让 IntelliSense 工作?
【问题讨论】:
-
好像缺少设计器文件...
-
@rene 是的,你是对的,没有设计器文件。但为什么会这样,有没有一种简单的方法来创建?我通过 Xamarin Forms Studio 编辑器创建了它...
-
您在哪一行和哪个文件中收到该警告?
-
双击警告是什么行引起的。它似乎不是来自这段代码
-
确实如此,另一张海报是对的。没有设计师。
标签: c# .net xamarin code-behind xamarin.forms