我正在嘗試創建一個收藏夾頁面,用戶可以將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#,我不知道該怎麼做。請包括代碼幫助,我將不勝感激!非常感謝提前。
感謝@Alex你的代碼工作太棒了我解。然而,我有一個問題,關於將收藏夾列表保存到獨立存儲器,因此在關閉並重新啓動應用程序時,收藏夾列表仍然存在。我正在使用一個Settings.cs類,它具有寫入獨立存儲的鍵值對,但我無法弄清楚保存Url的ObservableCollection的最佳方式。我的格式如下(另一部分) '公共靜態只讀設置 DisableScreenLock =新設置(「DisableScreenLock」,true);' 任何想法? –
Matthew
2012-03-23 23:30:44
@Matt,我在我的博客上寫了一篇與此相關的稿子。感謝您的問題,我發佈了它。 http://www.ritzcovan.com/2012/03/read-and-write-observablecollectiont-to-isolatedstorage/ – Alex 2012-03-24 13:03:23