Xamarin has no control over code control
For my Xamarin forms project (latest version 1.3), when I create a view in Xamarin Studio (latest version, 5.5.4), I have defined the view like this:
<?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>
However, in my code, any reference to the control by name was not found at all. The following code is an error (noted by comments):
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";
}
}
}
Oddly enough, I was getting warnings with:
Warning: the private field `FormsDemo.Register.FirstName 'is assigned but its value is never used (FormsDemo.Solution)
I am getting one for each field. Why doesn't it work? How do I get intellisense to work?
source to share
I had a similar problem. In my case, it was my own business. In the xaml file, I have set the content x: Class attribute to the wrong value.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="**{namespace.classname}**">
Specifying the class name correctly resolved the problem for me.
source to share
you can try this
var asdf = this.FindByName<TimePicker>("tagName");
here SUGGESTIONS :
https://forums.xamarin.com/discussion/25409/problem-with-xaml-x-name-and-access-from-code-behind
DIRECT ANSWER from this link
In the page properties, check if the following properties are set:
BuildAction = built-in resource CustomTool = MSBuild: UpdateDesignTimeXaml
If everything is ok, delete the obj and bin folders, rebuild the PCL.
If you import a XAML page into PCL, for example, the attributes will change and will not compile.
source to share