创建收藏夹页面

本文关键字:收藏夹 创建 | 更新日期: 2023-09-27 18:28:30

我正试图创建一个收藏夹页面,用户可以通过该页面将网络浏览器的当前url添加到可观察集合中,可以随时选择该集合将用户发送到该收藏夹的url。我尝试创建一个绑定到列表框的observalecollection,当用户选择将当前url(在主页上)添加到收藏夹页面的列表框时,该列表框将被填充(在0索引处)。到目前为止,我有以下内容,但没有填充我的列表框,我不确定为什么?

主页.xaml

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar Opacity=".5" IsVisible="True" IsMenuEnabled="True">
        ...            
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="add to favorites" Click="AddToFavorites_Click"/>      
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

主页.xaml.cs

void AddToFavorites_Click(object sender, EventArgs e)
    {
        this.NavigationService.Navigate(new Uri("/FavoritesPage.xaml?curUrl=" + TheBrowser.currentUrl(), UriKind.Relative));
    }

我创建了一个Favorite类,用于构建绑定到Listbox 的Favorites observaleCollection

Favorite.cs

  public class Favorite : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    //A helper method used by the properties
    void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    DateTimeOffset modified;
    public DateTimeOffset Modified
    {
        get { return this.modified; }
        set { this.modified = value; OnPropertyChanged("Modified"); }
    }
    //Title for name of Favorite
    //Settings.currentFavorite holds the currentUrl to be used as the title
    string title = Settings.currentFavorite.Value;
    public string Title
    {
        get { return this.title;}
        set { this.title = value; OnPropertyChanged("Title"); }
    }

上面的这个Favorite.cs类在我的FavoritesPage中使用如下:

Favorite.xaml

<ListBox x:Name="FavoritesListBox" Grid.Row="1" ItemsSource="{Binding}"
             SelectionChanged="FavoritesListBox_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Border Margin="24,0" toolkit:TiltEffect.IsTiltEnabled="True">
                        <TextBlock Text="{Binding Title}" Margin="12"/>
                    </Border>
                    <TextBlock Foreground="{StaticResource PhoneSubtleBrush}"
                               Text="{Binding Modified, Converter={StaticResource DateConverter}}"
                               Margin="24,0,0,12"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Favorite.xaml.cs

public partial class FavoritesPage : PhoneApplicationPage
{
    //current url from querystring
    string favoriteUrl;
    //temporary state
    public static readonly Setting<int> CurrentFavoritesIndex = new Setting<int>("CurrentFavoritesIndex", -1);
    //the users data
    public static readonly Setting<ObservableCollection<Favorite>> FavoritesList = 
        new Setting<ObservableCollection<Favorite>>("FavoritesList", new ObservableCollection<Favorite>());
    public FavoritesPage()
    {
        InitializeComponent();
        //this.FavoritesListBox.DataContext = this;
        //bind the favorites list as the data source for the FavoritesListBox
        //this.DataContext = FavoritesList.Value;
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        //gets the current Url
        NavigationContext.QueryString.TryGetValue("curUrl", out favoriteUrl);
        Settings.currentFavorite.Value = favoriteUrl;
        //clear the selection so selecting the same item twice in a row will still raise the SelectionChanged event
        CurrentFavoritesIndex.Value = -1;
        this.FavoritesListBox.SelectedIndex = -1;
        //bind the favorites list as the data source for the FavoritesListBox
        this.DataContext = FavoritesList.Value;
    }
    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        //Favorite fav = FavoritesList.Value[CurrentFavoritesIndex.Value];
        //fav.Modified = DateTimeOffset.Now;
    }            
    void FavoritesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (FavoritesListBox.SelectedIndex >= 0)
        {
            //Navigate to the webbrowser page for the selected item
            CurrentFavoritesIndex.Value = FavoritesListBox.SelectedIndex;
            //??
            //this.NavigationService.Navigate(new Uri("/MainPage.xaml?curUrl=" + FavoritesListBox.), UriKind.Relative));
        }
    }
    //private void AddToFavorites_Click(object sender, EventArgs e)
    void AddToFavorites_Click(object sender, EventArgs e)
    {
        Favorite favorite = new Favorite();
        favorite.Modified = DateTimeOffset.Now;
        FavoritesList.Value.Insert(0, favorite);
        //FavoritesList.Value.Add("xxxx");
        //FavoritesList.Value.Insert(0, WebBrowser.SourceProperty.ToString());
    }
}

所以这是我的基本实现,尽管我不确定它是否正确,也不确定我必须做些什么来修复它才能正常工作。我遇到了问题,将observaleCollection绑定到Listbox,然后也使用selectionchanged方法,该方法在所选索引处选择收藏夹(及其url),然后使用该url导航回MainPage.xaml?任何帮助都将不胜感激,因为我对c#非常陌生,我不太确定该怎么办。请提供代码帮助,我将不胜感激!!提前非常感谢。

创建收藏夹页面

为了保持简单,我对您的实现做了一些更改。

我有2个页面与他们相关的ViewModels:

MainPage.xaml.cs-在列表框中显示URL列表

 public MainPage()
        {
            InitializeComponent();
            Loaded += (s, e) => DataContext = new MainPageViewModel();
        }
        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            NavigationService.Navigate(new Uri("/views/browser.xaml?url=" + e.AddedItems[0], UriKind.Relative));
        }

MainPageViewModel.cs

namespace FavUrl.viewmodels
{
    public class MainPageViewModel : ViewModelBase
    {
        public ObservableCollection<string> Urls {
            get { return (App.Current as App).Urls; }
        }
    }
}

browser.xaml.cs

public partial class browser : PhoneApplicationPage {
        private BrowserViewModel _vm;
        public browser()
        {
            InitializeComponent();
            Loaded += (s, e) => {
                            _vm = new BrowserViewModel();
                            DataContext = _vm;
                      };
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
            string targetUrl = null;
            NavigationContext.QueryString.TryGetValue("url", out targetUrl);
            if(targetUrl != null)
                webBrowser1.Navigate(new Uri(targetUrl, UriKind.Absolute));
            base.OnNavigatedTo(e);
        }
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            _vm.AddUrl(webBrowser1.Source.AbsoluteUri);
        }
        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e) {
            NavigationService.Navigate(new Uri("/views/MainPage.xaml", UriKind.Relative));
        }
        private void btnGo_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            webBrowser1.Navigate(new Uri(tbUrl.Text.Trim(), UriKind.Absolute));
        }
    }

BrowserViewModel.cs

  public class BrowserViewModel : ViewModelBase
    {
        public BrowserViewModel() {
        }
        public void AddUrl(string url)
        {
            (App.Current as App).Urls.Add(url);
        }
    }

我有一个ObservableCollection,它作为属性公开在App.xaml.cs文件中。我从这两个页面访问这个ObservableCollection。

应用程序.xaml.cs

public ObservableCollection<string> Urls { get; set;}

我已经在以下地址的zip文件中发布了整个示例解决方案

http://www.ritzcovan.com/wp-content/zips/FavUrl.zip

我希望这能帮助