Xamarin中的列表绑定问题

时间:2018-08-15 14:26:56

标签: c# xaml xamarin xamarin.forms xamarin.ios

首先,我想透露我是一名新开发人员,刚开始使用Xamarin和C Sharp进行编码。

我在列表中的位置上设置了一个列表。我有两个视图,一个列表视图页面和一个列表视图的详细页面。此列表中的所有Binding元素都可以访问,并且可以在列表视图中正常工作,然后单击它们时,它们也将显示在详细视图中。问题是我还在列表中以及模型中也添加了一个clicked属性和其他属性。此click属性旨在处理详细信息页面中的按钮单击,而该按钮又将播放音频文件。在我运行代码后,它们没有错误,音频根本无法播放。

请参见以下代码,以更好地了解所面临的问题:

Tourpage.xaml.cs

public partial class ToursPage : ContentPage
{
    async public void Handle_ItemSelected(object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)

    {
        if (e.SelectedItem == null)
            return;

        var location = e.SelectedItem as Location;
        await Navigation.PushAsync(new ToursDetail(location));
        listView.SelectedItem = null;

    }

    public ToursPage()
    {
        InitializeComponent();

        listView.ItemsSource = new List<Location>{

        new Location
        {
            Name = "Kilkenny Castle",
            Description = "This is the description Kilkenny Castle is simply dummy text of the printing and type setting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic ",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/a/a4/Kilkenny_castle.jpg",
            Clicked = "Handle_Clicked1"
        },
        new Location
        {
            Name = "Butler House",
            Description = "This is the description Kilkenny Castle is simply dummy text of the printing and type setting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic ",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/a/a4/Kilkenny_castle.jpg",
            Clicked = "Handle_Clicked2"
        }
        };

ToursDetail.xaml

    <StackLayout>
    <Image Source = "{Binding ImageUrl}"/>
    <Label Text = "{Binding Description}" LineBreakMode="WordWrap"/>
    <Button Text = "Play" Command="{Binding Clicked}"/>
    </StackLayout>

ToursDetail.xaml.cs :这是处理按钮单击的地方

 public partial class ToursDetail : ContentPage
{

    public ToursDetail(Location location)
    {
        if (location == null)
            throw new ArgumentNullException();
        BindingContext = location;

        InitializeComponent();
    }


    private void Handle_Clicked1(object sender, System.EventArgs e)
    {
        DependencyService.Get<IAudio>().Play_Pause("http://www.hochmuth.com/mp3/Haydn_Cello_Concerto_D-1.mp3");
    }

模型

namespace CustomRenderer.Models
{
public class Location
{
    public string Name { get; set; }  
    public string Description { get; set; }
    public string ImageUrl { get; set; }
    public string Clicked { get; set; }


}
}

现在还要注意的是,如果我将 ToursDetail.xaml 文件中的代码更改为以下内容:

<StackLayout>
    <Image Source = "{Binding ImageUrl}"/>
    <Label Text = "{Binding Description}" LineBreakMode="WordWrap"/>
    <Button Text = "Play" Clicked= "Handle_Clicked1"/>
    </StackLayout>

然后音频播放正常。因此,这种类型的设置对我不起作用,因为我需要列表选择来基于该选择绑定位置音频,而不是像我在此处设置的那样绑定。对于如何实现此解决方法的任何帮助,将不胜感激。我一直在兜圈子,试图解决这个问题并找到解决方案。

谢谢 迈克

1 个答案:

答案 0 :(得分:1)

我也是新手,但是我可能已经在视图模型中添加了“ Clicked”作为公共Command成员:

outerPublicKey

然后在ViewModel构造函数中,将命令与特定位置附近可能需要的任何其他逻辑一起附加-即将参数中的声音文件路径传递给函数:

public class ToursDetailViewModel : BaseViewModel
{
    public Location location { get; set; }
    public Command ClickedCommand { get; set; }
    ...

现在就这样:

public ToursDetailViewModel(Location loc = null)
{
    location = loc;
    ClickedCommand = new Command(async () => await Handle_Clicked1(loc.soundpath));

最后:

ToursDetailViewModel viewModel;
public ToursDetail(Location location)
{
    if (location == null)
        throw new ArgumentNullException();

    BindingContext = this.viewModel = new ToursDetailViewModel(location);