0

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

歌词选项卡是一个轮播视图,仅显示idtitle歌曲lyrics

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

歌词标签完美显示idtitle以及lyrics所有歌曲。但详细信息选项卡始终为空白。

在 SongModel 中:

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文件中解析出来的,并绑定到ItemsSourceinCarouselView并且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();
  }
}

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

谢谢。

4

1 回答 1

0

根据您的代码和描述,我猜您想将数据从一个 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, string>("11", "Hi", (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, string>("11", "Hi", 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",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, string>("11", "Hi", (sender, 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();
    }    
}
于 2021-03-08T03:36:11.403 回答