2012-03-19 76 views
1

我正在尝试创建一个收藏夹页面,用户可以将Web浏览器的当前URL添加到observablecollection,这可以在任何时候选择将用户发送到该收藏夹的url。 我已经尝试创建一个绑定到列表框的observablecollection,当用户选择将当前URL(在主页面上)添加到收藏夹页面的列表框时,它将被填充(在0索引处)。我到目前为止的内容如下,但没有填充我的Listbox,我不确定为什么?创建收藏夹页面

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

我创建了一个最喜欢的课被用于构建我收藏的ObservableCollection绑定到列表框

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

} 

所以这是我的基本实现,虽然我不确定它是否正确,或者我必须做些什么才能解决问题,以便它能正常工作。我有麻烦将绑定到Listbox的observablecollection,然后还与选择所选索引最喜欢的(和它的URL),然后导航回到MainPage.xaml与这个URL使用selectionchanged方法?任何帮助将不胜感激,因为我非常坚持和新的C#,我不知道该怎么做。请包括代码帮助,我将不胜感激!非常感谢提前。

回答

0

我改变了一点你的实现,以保持简单。

我有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); 
     } 
    } 

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

App.xaml。CS

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

我已经张贴在一个zip文件全部样品溶液,在以下地址

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

我希望这有助于

+0

感谢@Alex你的代码工作太棒了我解。然而,我有一个问题,关于将收藏夹列表保存到独立存储器,因此在关闭并重新启动应用程序时,收藏夹列表仍然存在。我正在使用一个Settings.cs类,它具有写入独立存储的键值对,但我无法弄清楚保存Url的ObservableCollection的最佳方式。我的格式如下(另一部分) '公共静态只读设置 DisableScreenLock =新设置(“DisableScreenLock”,true);' 任何想法? – Matthew 2012-03-23 23:30:44

+0

@Matt,我在我的博客上写了一篇与此相关的稿子。感谢您的问题,我发布了它。 http://www.ritzcovan.com/2012/03/read-and-write-observablecollectiont-to-isolatedstorage/ – Alex 2012-03-24 13:03:23