根据您的代码和描述,我猜您想将数据从一个 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();
}
}