【发布时间】:2020-10-10 13:29:06
【问题描述】:
我在使用 Xamarin 数据绑定时遇到了一点问题。绑定到 C# 类中的项目时,我收到标题中的错误。我一直在尝试从找到here 的表示例中绑定一个元素。我尝试将BindingContext = this; 添加到构造函数中,并将类声明上方的行更改为[DesignTimeVisible(false)],就像在创建项目期间找到的主从模板中的NewItemPage 一样,但是,它没有帮助。
代码如下:
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="NetworkScanner.Views.TestPage">
<TableView>
<TableRoot>
<TableSection Title="Getting Started" BindingContext="{x:Reference Table}">
<ViewCell>
<StackLayout Orientation="Horizontal">
<Image Source="bulb.png" />
<Label Text="left"
TextColor="#f35e20" />
<Label Text="right"
HorizontalOptions="EndAndExpand"
TextColor="#503026" />
</StackLayout>
</ViewCell>
</TableSection>
</TableRoot>
</TableView>
</ContentPage>
C#:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 Xamarin.Forms;
使用 Xamarin.Forms.Xaml;
命名空间 NetworkScanner.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
// [DesignTimeVisible(false)]
公共部分类 TestPage : ContentPage
{
公共 TableSection 表 { 获取;放; }
公共测试页()
{
初始化组件();
// BindingContext = this;
}
}
}
【问题讨论】:
-
你想完成什么?您正在设置 BindingContext,但没有任何绑定表达式,并且您从未初始化您的
Table属性。一般来说,如果你想要一个数据驱动的表,你会使用 ListView 或 CollectionVIew,而不是 TableView。
标签: c# xaml xamarin xamarin.forms