Xamarin.Forms accessing controls written in markup from Code(Xamarin.Forms 从代码访问用标记编写的控件)
问题描述
我正在尝试将一些项目添加到我在 xaml 文件中使用 Xamarin.Forms 标记添加的 Listview.可以通过与单击事件挂钩来访问该按钮.但是由于列表视图是空的,我需要像 winforms 中的 ondraw
之类的事件,以便在绘制时可以挂钩它.
Im trying to add some items to a Listview which i added using Xamarin.Forms markup in an xaml file.
The button can be accessed by hooking with the click event.But since the listview is empty i need the event like ondraw
like in winforms, so that i can hook to it when it is drawn.
在我的 XAML 文件中:
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>
在 .cs 文件中我有
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";
}
}
}
那么我应该挂钩 Listview 中的事件还是可以像我们在 android 中那样做类似 Resource.getElementbyID
的事情
So should i hook to events in Listview or can i do something like Resource.getElementbyID
like we do in android
推荐答案
要在代码隐藏中访问 Forms 控件,您需要为其分配一个名称,使用 x:Name
属性
To access a Forms control in the code-behind, you need to assign it a name, using the x:Name
attribute
在 XAML 中:
<ListView HorizontalOptions="Center" x:Name="MyList" />
在代码中:
MyList.ItemsSource = myData;
这篇关于Xamarin.Forms 从代码访问用标记编写的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Xamarin.Forms 从代码访问用标记编写的控件


- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01