Xamarin Can't Access Controls on Code Behind
Asked Answered
D

5

6

For my Xamarin Forms project (latest version 1.3), when I create a view in Xamarin Studio (latest, 5.5.4), I define the view as the following:

<?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-behind, any references to the control by its name are not being found at all. The following code is erroring (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 get one for each field. Why is this not working? How do I get IntelliSense to work?

Deccan answered 3/1, 2015 at 10:47 Comment(5)
Looks like the designer file is missing ...Jesicajeske
@Jesicajeske Yes, you are correct, there is no designer file. But why would that be, and is there an easy way to create? I created this through the Xamarin Forms Studio editor...Deccan
On what line and in which file do you get that warning?Jesicajeske
Double click the warning what line is causing this. It doesnt seem to come from this codeTranslator
It does, the other poster was right. No designer.Deccan
D
2

The latest update seemed to fix the issue; these references works OK:

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

It appears some of the designer problems are resolved.

Deccan answered 10/2, 2015 at 16:16 Comment(0)
V
1

you can try this

var asdf = this.FindByName<TimePicker>("tagName");

there are SUGGESTIONS to do here:

https://forums.xamarin.com/discussion/25409/problem-with-xaml-x-name-and-access-from-code-behind

DIRECT ANSWER from that link

On your page properties check the following properties are set:

BuildAction = Embedded Resource CustomTool = MSBuild:UpdateDesignTimeXaml

If that's okay, delete the obj & bin folders are rebuild the PCL.

If you import a XAML page into a PCL for example the attributes get changed and it won't compile.

Viscid answered 29/3, 2018 at 18:18 Comment(0)
S
0

I had a similar issue, In my case it was my own doing, In the xaml file, I had set the content attribute x:Class with 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 solved the issue for me.

Seneschal answered 11/5, 2017 at 11:55 Comment(0)
P
0

I had the same problem, after I renamed some files and manually corrected the Class references. Somehow VS was not able to find the references from xaml page. When I created new xaml file with code behind, the same code worked.

Pegpega answered 31/5, 2017 at 15:4 Comment(0)
A
0

Look for a line in XAML that looks like this.

x="http://schemas.microsoft.com/winfx/2009/xaml"

instead of this

xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

probably something in XAML messy things up.

Allophone answered 4/5, 2022 at 20:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.