欢迎来到我们的 Microsoft 问答平台!
您的代码中存在几个问题。
导航到页面时,您为 model 创建了一个新实例,因此 没有 的值。DetailsSongsViewModelcurrentsong
public Details()
{
InitializeComponent();
BindingContext = new SongsViewModel();
}
当您在页面中设置 listView 时,它的类型应该是列表而不是 。<ListView ItemsSource=“{绑定当前歌曲}”>ItemsSourceDetailscurrentsong 根据您的需要,当我们导航到页面时,我们可以将所选项目的数据传递到页面:DetailsDetails
In page Lyrics:
public void OnCurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
{
Song item = e.CurrentItem as Song;
// ((SongsViewModel)this.BindingContext).currentsong = item;
Navigation.PushAsync(new Details(item));
}
In page Details,we can get the item from the constructor :
public partial class Details : ContentPage
{
Song currentSong;
public Details(Song song)
{
InitializeComponent();
currentSong = song;
BindingContext = currentSong;
}
}
Details.xaml
<ContentPage.Content>
<StackLayout>
<Label FontSize="17" HorizontalOptions="Start" TextColor="Black" LineBreakMode="WordWrap">
<Label.Text>
<MultiBinding StringFormat="{}Lyricist : {0};
Album: {1}
Genre : {2}">
<Binding Path="lyricist"/>
<Binding Path="album"/>
<Binding Path="genre"/>
</MultiBinding>
</Label.Text>
</Label>
</StackLayout>
</ContentPage.Content>
Update
It has two tabs. Lyrics tab and Details tab.
If you have two tabs, you can achieve this function by the following steps:(I have tested on my side ,it works properly)
add static for the currentsong in model SongsViewModel, then page Lyrics and Details will share the same field currentsong:
public class SongsViewModel
{ // set static for currentsong
public static Song currentsong { get; set; }
public List<Song> songlist { get; private set; }
public SongsViewModel()
{
songlist = new List<Song>();
songlist.Add(new Song { title = "title1", lyricist = "Jack", album = "Album1", genre = "classical" });
songlist.Add(new Song { title = "title2", lyricist = "hello", album = "Album2", genre = "Country" });
songlist.Add(new Song { title = "title3", lyricist = "May", album = "Album3", genre = "pop" });
}
}
make model Song implement interface INotifyPropertyChanged, when the value of currentsong changes, the UI will refresh automatically.
public class Song: INotifyPropertyChanged
{
public string id { get; set; }
public string title { get; set; }
public string lyrics { get; set; }
// public string lyricist { get; set; }
string _lyricist;
public string lyricist
{
set { SetProperty(ref _lyricist, value); }
get { return _lyricist; }
}
// public string album { get; set; }
string _album;
public string album
{
set { SetProperty(ref _album, value); }
get { return _album; }
}
//public string genre { get; set; }
string _genre;
public string genre
{
set { SetProperty(ref _genre, value); }
get { return _genre; }
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Lyrics.xaml.cs
public partial class Lyrics : ContentPage
{
SongsViewModel viewModel;
public Lyrics()
{
InitializeComponent();
viewModel = new SongsViewModel();
BindingContext = viewModel;
}
public void OnCurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
{
Song item = e.CurrentItem as Song;
SongsViewModel.currentsong = item;
}
}
Lyrics.xaml
<ContentPage.BindingContext>
tabbedpagewithnavigationpage:SongsViewModel</tabbedpagewithnavigationpage:SongsViewModel>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<CarouselView ItemsSource="{Binding songlist}" CurrentItemChanged="OnCurrentItemChanged">
<CarouselView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" />
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame HasShadow="True"
BorderColor="DarkGray"
CornerRadius="5"
Margin="20"
HeightRequest="100"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand">
<StackLayout>
<Label Text="{Binding title }"
FontAttributes="Bold"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label
Text="{Binding lyricist }"
HeightRequest="150"
WidthRequest="150"
HorizontalOptions="Center" />
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</ContentPage.Content>
Details.xaml.cs
public partial class Details : ContentPage
{
Song song;
public Details()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
song = SongsViewModel.currentsong;
BindingContext = song;
}
}
Details.xaml <ContentPage.Content>
<StackLayout>
<Label FontSize="17" HorizontalOptions="Start" TextColor="Black" LineBreakMode="WordWrap">
<Label.Text>
<MultiBinding StringFormat="{}Lyricist : {0}; Album: {1} Genre : {2}">
<Binding Path="lyricist"/>
<Binding Path="album"/>
<Binding Path="genre"/>
</MultiBinding>
</Label.Text>
</Label>
</StackLayout>
</ContentPage.Content>
如果回复有帮助,请点击“接受答案”并点赞。 注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。