2012-03-19 81 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