简体   繁体   English

如何访问由Designer(XAML)创建的xamarin(C#,Visual Studio 2015)中的控件

[英]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. 在Visual Studio 2015中,我创建了一个Android应用(Xamarin),并将ListView放入我的活动中。 The XAML created by the designer is as follows: 设计器创建的XAML如下:

<?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: 现在,我想像这样在C#代码中访问ListView-Object:

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. 但这是不可能的,因为Visual Studio的IntelliSense无法识别ListView1,并且无法进行编译。 How can i access the control here? 我如何在这里访问控件?

Also there's no way to reach the properties in the Xamarin designer in Visual Studio. 此外,也无法在Visual Studio中访问Xamarin设计器中的属性。 It's only possible to see the events but the properties-window is greyed out. 只能看到事件,但属性窗口显示为灰色。

Thanks in Advance for your help! 在此先感谢您的帮助!

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: 使用AXML您可以通过使用FindViewById方法检索对象/小部件的键入版本来访问将这些对象“夸大”的控件:

Using your AXML example, you would need to access the resource id of "ListView1": 使用您的AXML示例,您将需要访问“ ListView1”的资源ID:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM