创建收藏页面

时间:2012-03-19 21:48:48

标签: c# windows-phone-7

我正在尝试创建一个“收藏夹”页面,用户可以将webbrowser的当前url添加到observablecollection,可以随时选择将该用户发送到该收藏夹的URL。 我已经尝试创建一个绑定到Listbox的observablecollection,当用户选择将当前url(在Main页面上)添加到Favorites页面的Listbox时,它将被填充(在0索引处)。到目前为止,我所拥有的内容如下,但是没有填充我的列表框,我不确定为什么?

MainPage.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>

MainPage.xaml.cs中

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

我创建了一个Favorite类,用于构建绑定到Listbox的我的收藏夹observablecollection

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());
    }

}

这是我的基本实现,虽然我不确定它是否正确或我必须做些什么来修复它以便它能正常工作。我遇到麻烦将observablecollection绑定到Listbox,然后使用selectionchanged方法选择所选索引的收藏夹(及其url),然后使用此url导航回MainPage.xaml?任何帮助将不胜感激,因为我非常困难和新的c#,我不太清楚该怎么做。请包括代码帮助我会非常感谢!!非常感谢提前。

1 个答案:

答案 0 :(得分:0)

我稍微改变了你的实现,以保持简单。

我有2个页面及其相关的ViewModel:

MainPage.xaml.cs - 显示列表框中的网址列表

 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。

<强> App.xaml.cs

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

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

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

我希望这会有所帮助