如何解决Xamarin CarouselView,在另一个页面中显示当前项目
我正在开发歌词应用。它有两个选项卡。歌词标签和详细信息标签。
歌词标签是一个轮播视图,仅显示 id
、title
和 lyrics
的歌曲。
当“歌词”选项卡中的页面被滑动时,我想在“详细信息”选项卡中显示当前显示歌曲的所有其他信息,例如 artist
、album
、year
等。
歌词选项卡完美显示所有歌曲的id
、title
和lyrics
。但详细信息选项卡始终为空白。
在歌曲模型中:
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};
Album: {1}
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 举报,一经查实,本站将立刻删除。