添加到收藏按钮内的StackPanel
本文关键字:StackPanel 按钮 收藏 添加 | 更新日期: 2023-09-27 18:05:30
我有一个问题,我想不出如何解决它。
我需要在StackPanel中添加一个按钮到Favorites,问题是该按钮按在其他页面上,即StackPanel声明,我不能将按钮添加到它的孩子。所以,我有两个页面:主页。xaml和PlayerPage。xaml。
在MainPage我保持一个StackPanel与按钮,如:
<StackPanel x:Name="mainStack" >
<Button x:Name="but1" Click="Button2_Click" Tag="/videos/video1.mp4" Content="Play Video 1" />
<Button x:Name="but2" Click="Button2_Click" Tag="/videos/video2.mp4" Content="Play Video 2" />
<Button x:Name="but3" Click="Button2_Click" Tag="/videos/video3.mp4" Content="Play Video 3" />
</StackPanel>
<StackPanel x:Name="favoriteStack" >
<!-- Here need to be added favorite videos when user press Add to fav button! -->
</StackPanel>
cs
private void Button2_Click(object sender, EventArgs e)
{
NavigationService.Navigate(
new Uri("/PlayerPage.xaml?path=" +
HttpUtility.UrlEncode((sender as Button).Tag.ToString()),
UriKind.Relative));
}
在PlayerPage 。xaml:
<MediaElement x:Name="mediaElement1"
MediaOpened="mediaElement1_MediaOpened"
MediaFailed="mediaElement1_MediaFailed"
MediaEnded="mediaElement1_MediaEnded"
CurrentStateChanged="mediaElement1_CurrentStateChanged" />
<Button x:Name="AddToFav" Click="Button1_Click"
Content="Add this video to Favorites" />
cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.TryGetValue("path", out path))
{
if (!string.IsNullOrWhiteSpace(path))
{
mediaElement1.Source = new Uri( path );
}
}
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
// here must be somethink like:
Button butsender = new Button();
butsender = sender as Button;
stack2.Children.Add(butsender);
//better will be to save to Isolated Storage or somethink like this for future launching...
}
我有很多问题,因为我真的不知道如何执行…我试过使用全局应用程序栏并分配更多,但没有成功。任何帮助将不胜感激!谢谢!
执行此操作的最佳方法是遵循MVVM模式。您将定义一个包含两个列表的ViewModel类。
public class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<Video> Videos { get; set; }
public ObservableCollection<Video> FavoriteVideos { get; set; }
public MainViewModel()
{
Videos = new ObservableCollection<Video>();
FavoriteVideos = new ObservableCollection<Video>();
}
private Video selectedVideo;
public Video SelectedVideo
{
get { return selectedVideo; }
set { selectedVideo = value; NotifyPropertyChanged("SelectedVideo"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
在app . example .cs中你将定义ViewModel
private static MainViewModel viewModel = null;
public static MainViewModel ViewModel
{
get
{
// Delay creation of the view model until necessary
if (viewModel == null)
viewModel = new MainViewModel();
return viewModel;
}
}
然后你可以在构造函数中设置两个页面(MainPage和其他)的数据上下文,像这样:
DataContext = App.ViewModel;
现在你只需要绑定项目到两个列表(StackPanel不会在这里做。使用其他列表,如ListBox)
ItemsSource="{Binding Videos}"
ItemsSource="{Biding FavoriteVideos}"
对于导航,如果你使用ListBox,只需设置点击事件
ItemClick="ItemClick"
private void ItemClick(object sender, ItemClickEventArgs e)
{
Video item = e.ClickedItem as Video;
App.ViewModel.SelectedVideo = item;
NavigationService.Navigate(new Uri("/PlayerPage.xaml,UriKind.Relative));
}
在PlayerPage上绑定到SelectedVideo就可以了。对于收藏,你只需要将视频添加到FavoriteList
private void FavoriteButton_Click(object sender, RoutedEventArgs e)
{
if(!App.ViewModel.FavoriteVideos.Contains(App.ViewModel.SelectedVideo)) {
App.ViewModel.FavoriteVideos.Add(App.ViewModel.SelectedVideo);
}
}
现在你可以根据需要在应用中动态添加数据。StackPanel只会帮助你的静态视频,如果数据发生变化,它是不实用的。