Xamarin 无法访问隐藏代码的控件
Xamarin Can't Access Controls on Code Behind
对于我的 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>
但是,在我的代码隐藏中,根本找不到任何按控件名称引用的控件。以下代码有误(有注释):
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";
看来设计器的一些问题已解决。
我遇到了类似的问题,在我的情况下,这是我自己造成的,在 xaml 文件中,我将内容属性 x:Class 设置为错误的值。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="**{namespace.classname}**">
指定 class 名称正确地解决了我的问题。
我遇到了同样的问题,在我重命名了一些文件并手动更正了 Class 引用之后。不知何故 VS 无法从 xaml 页面找到引用。
当我创建带有隐藏代码的新 xaml 文件时,相同的代码起作用了。
你可以试试这个
var asdf = this.FindByName<TimePicker>("tagName");
这里有条建议:
https://forums.xamarin.com/discussion/25409/problem-with-xaml-x-name-and-access-from-code-behind
来自 link
的直接答案
在您的页面属性中检查是否设置了以下属性:
BuildAction = 嵌入式资源
自定义工具 = MSBuild:UpdateDesignTimeXaml
如果没问题,删除 obj 和 bin 文件夹重建 PCL。
例如,如果您将 XAML 页面导入 PCL,属性会发生变化,无法编译。
在 XAML 中寻找这样的一行。
x="http://schemas.microsoft.com/winfx/2009/xaml"
而不是这个
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
可能是 XAML 中的某些东西弄得一团糟。
对于我的 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>
但是,在我的代码隐藏中,根本找不到任何按控件名称引用的控件。以下代码有误(有注释):
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";
看来设计器的一些问题已解决。
我遇到了类似的问题,在我的情况下,这是我自己造成的,在 xaml 文件中,我将内容属性 x:Class 设置为错误的值。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="**{namespace.classname}**">
指定 class 名称正确地解决了我的问题。
我遇到了同样的问题,在我重命名了一些文件并手动更正了 Class 引用之后。不知何故 VS 无法从 xaml 页面找到引用。 当我创建带有隐藏代码的新 xaml 文件时,相同的代码起作用了。
你可以试试这个
var asdf = this.FindByName<TimePicker>("tagName");
这里有条建议:
https://forums.xamarin.com/discussion/25409/problem-with-xaml-x-name-and-access-from-code-behind
来自 link
的直接答案在您的页面属性中检查是否设置了以下属性:
BuildAction = 嵌入式资源 自定义工具 = MSBuild:UpdateDesignTimeXaml
如果没问题,删除 obj 和 bin 文件夹重建 PCL。
例如,如果您将 XAML 页面导入 PCL,属性会发生变化,无法编译。
在 XAML 中寻找这样的一行。
x="http://schemas.microsoft.com/winfx/2009/xaml"
而不是这个
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
可能是 XAML 中的某些东西弄得一团糟。