添加到收藏夹StackPanel内的按钮

时间:2014-05-27 19:59:39

标签: c# xaml windows-phone-8

我有一个问题,没有想到如何解决这个问题。

我需要在StackPanel中为收藏夹添加一个按钮,问题是在StackPanel声明的其他页面上按下了按钮,我无法向它的子项添加按钮。 所以,我有2个页面:MainPage.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...
}

我遇到了很多问题,因为我真的不知道如何执行...我尝试使用全局App Bar并分配更多但不成功的问题。任何帮助,将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法是遵循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.xaml.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只会帮助您处理静态视频,如果数据发生变化,它将无法使用。