Xamarin.Forms, access to controls written in code markup

I am trying to add some elements to the list that I added using the Xamarin.Forms markup in the xaml file. The key can be obtained by attaching it using the click event. But since listview is empty, I need an ondraw event like in ondraw , so that I can connect to it when it's drawn.

enter image description here

In the XAML file, I have:

 <?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="ButtonXaml.ButtonXamlPage"> <StackLayout> <Button Text="Tap for click count!" BorderWidth="10" TextColor="Red" HorizontalOptions="Center" Clicked="OnButtonClicked" /> <ListView HorizontalOptions="Center" /> </StackLayout> </ContentPage> 

In the .cs file I have

 using System; using Xamarin.Forms; namespace ButtonXaml { public partial class ButtonXamlPage { int count = 0; public ButtonXamlPage() { InitializeComponent(); } public void OnButtonClicked(object sender, EventArgs args) { ((Button)sender).Text = "You clicked me"; } } } 

So I have to connect to events in Listview or I can do something like Resource.getElementbyID , as in android

+6
source share
3 answers

To access the Forms control in your code, you need to give it a name using the x:Name attribute

in XAML:

 <ListView HorizontalOptions="Center" x:Name="MyList" /> 

in code:

 MyList.ItemsSource = myData; 
+17
source

There is an error in Xamarin where VS does not see a specific x: Name http://forums.xamarin.com/discussion/25409/problem-with-xaml-x-name-and-access-from-code-behind

Suppose you defined an image in XAML:

 <Image x:Name="myImageXName" /> 

Then this should work in the code behind:

 this.FindByName<Image>("myImageXName"); 
+8
source

In my case, the problem was the absence of the XamlCompilation(XamlCompilationOptions.Compile)] line XamlCompilation(XamlCompilationOptions.Compile)] in the .xaml.cs file.

Example:

 [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); BindingContext = new MainPageViewModel(); } ... } 
+2
source

Source: https://habr.com/ru/post/976700/


All Articles