Florian Wolferstetter
Florian Wolferstetter

Reputation: 23

How to access controls in xamarin (C#, Visual Studio 2015) created by Designer (XAML)

In Visual Studio 2015 i created an Android app (Xamarin) and placed a ListView into my activity. The XAML created by the designer is as follows:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:p1="http://schemas.android.com/apk/res/android"
      p1:minWidth="25px"
      p1:minHeight="25px"
      p1:layout_width="match_parent"
      p1:layout_height="match_parent"
      p1:id="@+id/ListView1" />

Now i wanted to access the ListView-Object in my C#-Code like this:

ListView1.SomeProperty = "bla";

But that's not possible because ListView1 isn't known by Visual Studio's IntelliSense and it's not possible to compile. How can i access the control here?

Also there's no way to reach the properties in the Xamarin designer in Visual Studio. It's only possible to see the events but the properties-window is greyed out.

Thanks in Advance for your help!

Upvotes: 1

Views: 727

Answers (1)

SushiHangover
SushiHangover

Reputation: 74174

When using AXML you can access the controls that get "inflated" into objects by using the FindViewById method to retrieve a typed version of the object/widget:

Using your AXML example, you would need to access the resource id of "ListView1":

  var listView = FindViewById<ListView>(Resource.Id.ListView1);
  listView. ~(IntelliSense will work now)~

Upvotes: 2

Related Questions