gpt4 book ai didi

xaml - 如何使用 Xamarin.Forms xaml 编辑器预览 ListView

转载 作者:行者123 更新时间:2023-12-05 01:29:15 24 4
gpt4 key购买 nike

我正在使用自定义单元格在 Xamarin.Forms 中实现一个 ListView,但我看不到我在做什么,因为预览显示一个空列表并且每次重新构建都非常简单,太耗时了。我什至看不到一个简单的 TextCell..

如何在编辑器中查看单元格的预览?这是我的 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:local="clr-namespace:MyNamespace"
x:Class="MyNamespace.MyAppPage">
<ContentPage.Content>

<ListView x:Name="myList">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="Preview?" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</ContentPage.Content>

注意:我使用的是 Visual Studio 2017 for Mac。


第二部分

好的,所以我尝试使用 BindingContext,但我不太了解此属性的用法。这是我更新的 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:local="clr-namespace:MyNamespace;assembly:MyNamespace"
BindingContext="{x:Static local:MusicSeed.Model}"
x:Class="MyNamespace.MyAppPage">

<ContentPage.Content>

<ListView ItemsSource="{Binding Tracks}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</ContentPage.Content>

</ContentPage>

这就是我拥有数据的地方,MusicSeed.cs:

namespace MyNamespace
{
public static class MusicSeed
{
public static readonly MusicSeedModel Model = new MusicSeedModel();
}

public class MusicSeedModel
{
public readonly MusicTrack[] Tracks =
{
new MusicTrack("Track 01"),
new MusicTrack("Track 02"),
new MusicTrack("Track 03"),
new MusicTrack("Track 04"),
};
}
}

结果是 XAML 编辑器抛出这个错误:

No static member found for local:MusicSeed.Model

我尝试将 Tracks 更改为静态但没有任何改变。也重建解决方案,错误消失但预览仍然是空的。我在这里缺少什么?

最佳答案

您必须为要在设计时呈现的行提供某种类型的 BindingContext。有一个 awesome article由 James Montemagno 撰写,解释了如何做到这一点。

另一篇推荐的文章是 this one on Xamarin Help

enter image description here

编辑 1:

您的 XAML 是正确的,但 View 模型是错误的。您只能绑定(bind)到属性。尝试将您的声明更改为以下内容:

namespace MyNamespace
{
public class MusicSeed
{
public static MusicSeedModel Model { get; } = new MusicSeedModel();
}

public class MusicSeedModel
{
public MusicTrack[] Tracks { get; } =
{
new MusicTrack("Track 01"),
new MusicTrack("Track 02"),
new MusicTrack("Track 03"),
new MusicTrack("Track 04"),
};
}

public class MusicTrack
{
public string Name { get; }

public MusicTrack(string v)
{
this.Name = v;
}
}
}

关于xaml - 如何使用 Xamarin.Forms xaml 编辑器预览 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47197739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com