微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin CarouselView,在另一个页面中显示当前项目

如何解决Xamarin CarouselView,在另一个页面中显示当前项目

我正在开发歌词应用。它有两个选项卡。歌词标签和详细信息标签

歌词标签一个轮播视图,仅显示 idtitlelyrics 的歌曲。

当“歌词”选项卡中的页面被滑动时,我想在“详细信息”选项卡中显示当前显示歌曲的所有其他信息,例如 artistalbumyear 等。

歌词选项卡完美显示所有歌曲的idtitlelyrics。但详细信息选项卡始终为空白。

在歌曲模型中:

public class Song
{
  public string id {get; set;}
  public string title { get; set; }
  public string lyrics { get; set; }
  public string artist { get; set; }
  public string album{ get; set; }
  public string genre{ get; set; }
}

在 Songsviewmodel 中:

class Songsviewmodel : Baseviewmodel
{
  public Song currentsong { get; set; }
  public List<Song> songlist { get; private set; } 
}

歌曲是从 song.json 文件中解析出来的,并绑定到 ItemsSource 中的 CarouselView 中的 Lyrics.xaml 并且运行良好。

Lyrics.xaml.cs中:

public partial class Lyrics : ContentPage
{
  public Lyrics()
  {
    InitializeComponent();
    BindingContext = new Songsviewmodel();
  }
    
  // Detecting CarouselView Current Item Change!
  public void OnCurrentItemChanged(object sender,CurrentItemChangedEventArgs e)
  {
    var item = e.CurrentItem as Song;
    ((Songsviewmodel)this.BindingContext).currentsong = item;
  }
}

Details.xaml页中,

<ListView ItemsSource="{Binding currentsong}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <Label FontSize="17" HorizontalOptions="Start" TextColor="Black" LineBreakMode="WordWrap">
        <Label.Text>
          <MultiBinding StringFormat="{}Artist : {0};&#x0a;Album: {1}&#x0a;Genre : {2}">
            <Binding Path="artist"/>
            <Binding Path="album"/>
            <Binding Path="genre"/>
          </MultiBinding>
        </Label.Text>
      </Label>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

Details.xaml.cs页中,

public partial class Details : ContentPage
{
  public Details()
  {
    InitializeComponent();
    BindingContext = new Songsviewmodel();
  }
}

请告诉我这里有什么问题。

谢谢。

解决方法

根据您的代码和描述,我猜您想将数据从一个 ContentPage 传递到另一个 ContentPage,对吗?

如果是,则可以使用 MessagingCenter 或 ContentPage 构造函数传递数据。

使用消息中心。

<StackLayout>
        <CarouselView CurrentItemChanged="CarouselView_CurrentItemChanged" ItemsSource="{Binding songlist}">
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Label Text="{Binding id}" />
                        <Label Text="{Binding title}" />
                        <Label Text="{Binding lyrics}" />
                    </StackLayout>
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>

        <Button
            x:Name="btn1"
            Clicked="btn1_Clicked"
            Text="go to another page" />

    </StackLayout>

 public partial class Page3 : ContentPage
{
    public Page3()
    {
        InitializeComponent();
        this.BindingContext = new SongsViewModel();      
    }
    
    private void CarouselView_CurrentItemChanged(object sender,CurrentItemChangedEventArgs e)
    {
        Song item= e.CurrentItem as Song;
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(item);
        MessagingCenter.Send<string,string>("11","Hi",jsonString);
    }

    private async void btn1_Clicked(object sender,EventArgs e)
    {
       await Navigation.PushAsync(new Page4());         
    }   
}
class SongsViewModel : ViewModelBase
{
  
    public static Song currentitem;
    public ObservableCollection<Song> songlist { get;  set;}
    
    public SongsViewModel()
    {
        songlist = new ObservableCollection<Song>()
        {
            new Song(){id="1",title="song 1",lyrics="song 1",artist="song 1",album="song 1",genre="song 1"},new Song(){id="2",title="song 2",lyrics="song 2",artist="song 2",album="song 2",genre="song 2"},new Song(){id="3",title="song 3",lyrics="song 3",artist="song 3",album="song 3",genre="song 3"},new Song(){id="4",title="song 4",lyrics="song 4",artist="song 4",album="song 4",genre="song 4"},new Song(){id="5",title="song 5",lyrics="song 5",artist="song 5",album="song 5",genre="song 5"},new Song(){id="6",title="song 6",lyrics="song 6",artist="song 6",album="song 6",genre="song 6"}
        };

        MessagingCenter.Subscribe<string,(sender,arg) =>
        {
            string jasonstring = arg;
            currentitem = JsonConvert.DeserializeObject<Song>(jasonstring);         
        });

    }
}

详细页面:

 <StackLayout>
        <Label
            FontSize="17"
            HorizontalOptions="Start"
            LineBreakMode="WordWrap"
            TextColor="Black">
            <Label.Text>
                <MultiBinding StringFormat="{}{0} {1} {2}">
                    <Binding Path="artist" />
                    <Binding Path="album" />
                    <Binding Path="genre" />
                </MultiBinding>
            </Label.Text>
        </Label>
        
    </StackLayout>

public partial class Page4 : ContentPage
{
  
    public Page4()
    {
        InitializeComponent();
        this.BindingContext = SongsViewModel.currentitem;       
    } 
}

使用 ContentPage 构造函数

  Song item;
    private void CarouselView_CurrentItemChanged(object sender,CurrentItemChangedEventArgs e)
    {
        item= e.CurrentItem as Song;
       
    }
    private async void btn1_Clicked(object sender,EventArgs e)
    {
       await Navigation.PushAsync(new Page4(item));         
    }   

 public partial class Page4 : ContentPage,INotifyPropertyChanged
{
    private Song _item;
    public Song item
    {
        get { return _item; }
        set
        {
            _item = value;
            RaisePropertyChanged("item");
        }
    }
    public Page4(Song item)
    {
        InitializeComponent();
        this.item = item;
        //this.BindingContext = SongsViewModel.currentitem;  
        this.BindingContext = item;

    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this,new PropertyChangedEventArgs(propertyName));
        }
    }
}

更新:

Page3 和 SongViewmodel 与上面的代码相同,但在 OnAppearing 上绑定 page4 BindingContext 以更新 BindingContext。

public partial class Page3 : ContentPage
{
    public Page3()
    {
        InitializeComponent();
        this.BindingContext = new SongsViewModel();      
    }
    Song item;
    private void CarouselView_CurrentItemChanged(object sender,CurrentItemChangedEventArgs e)
    {
        item= e.CurrentItem as Song;
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(item);
        MessagingCenter.Send<string,jsonString);
    }
   
}

  public class SongsViewModel : ViewModelBase
   {
 
    public static Song currentitem;
    public ObservableCollection<Song> songlist { get;  set;}
    
    public SongsViewModel()
    {
        songlist = new ObservableCollection<Song>()
        {
            new Song(){id="1",arg) =>
        {
            string jasonstring = arg;
            currentitem = JsonConvert.DeserializeObject<Song>(jasonstring);
        });

    }
}

 public partial class Page4 : ContentPage
{     
    protected override void OnAppearing()
    {
        base.OnAppearing();
       
        this.BindingContext = SongsViewModel.currentitem;
    }
    public Page4()
    {
        InitializeComponent();
    }    
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?