开发者

Implementing a bookmarks/favouriotes type function in my application

I am looking into concepts of how to implement a bookmarks or favouities type function into my application. Basically, this enables the users to save favourite pages from my app, which are then displayed on a list of favourites. The favouties of course need to be deleted as well. I would proably just need two or three pieces of information like 'page name' and 'address of page' within my application.

My currrent trail of thought is with isolated storage and creating an XML file but this is proving difficul开发者_如何学运维t.

EDIT,: I am not asking for code or anything, but more after ideas on how to approach this and which methods to use.

Many thanks.


I agree 100% with Matt.

In Overflow7 I use the actual XAML Navigation URL for each of the bookmarks - and then persist the list of bookmarks in a single using a single JSON file (using JSON.Net)

So, for example, a bookmark to a search is actually stored in a big object like:

"windows-phone-7" : {
   "Bookmark" : "/Views/SearchPage.xaml?Site=StackOverflow&Tag=windows-phone-7"
},
"Stuart" : {
   "Bookmark" : "/Views/UserPage.xaml?Site=StackOverflow&User=12341"
},

This makes the bookmark navigation mechanism really easy - but does mean that it would be hard for me to later change my page names. For future proofing, it might be better to split the page name out from the persisted bookmark

For actually making/clearing bookmarks I use an icon on the ApplicationBar - the only problem with this though is that you can't use colour to show the on/off state - because of the way the ApplicationBar works in light theme mode you have to use some distinct icon shape to show whether the bookmark is on/off.

Load and save of bookmarks is using:

    private const string BookmarkFileName = "Bookmarks.json";

    private void LoadBookmarks()
    {
        BookmarkViewModel = new AppBookmarkViewModel();
        try
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (false == store.FileExists(BookmarkFileName))
                {
                    return;
                }

                using (var fs = store.OpenFile(BookmarkFileName, System.IO.FileMode.Open))
                {
                    using (var sr = new StreamReader(fs))
                    {
                        var text = sr.ReadToEnd();
                        BookmarkViewModel = JsonConvert.DeserializeObject<AppBookmarkViewModel>(text);
                    }
                }
            }
        }
        catch (Exception exc)
        {
            BookmarkViewModel = new AppBookmarkViewModel();
            MessageBox.Show("Sorry - there was a problem reading bookmarks - " + exc.Message);
        }
    }

    public void SaveBookmarks()
    {
        try
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(BookmarkFileName))
                {
                    store.DeleteFile(BookmarkFileName);
                }

                using (var fs = store.OpenFile(BookmarkFileName, System.IO.FileMode.CreateNew))
                {
                    using (var sw = new StreamWriter(fs))
                    {
                        var text = JsonConvert.SerializeObject(BookmarkViewModel);
                        sw.Write(text);
                    }
                }
            }
        }
        catch (Exception exc)
        {
            // don't show message box during shutdown
            //MessageBox.Show("Sorry - there was a problem reading bookmarks - " + exc.Message);
        }
    }


This will depend on what the user is actually favouriting but here's a quick generic overview of how I'd approach this. (THis is quite like how I've done similar things in real projects.)

If you had a dictionary/list (or similar collection/enumerable) of your pages, I'd just have a separate collection which stored the indexes of the favourited pages.

To persist the favourites I'd just serialize the collection (I'd use json rather than xml for speed) and save it to a file in IsolatedStorage.

Adding to and deleteing from the favourites woudl just be a case of updating the collection.

To display the favourites I'd create a class which wraps the list of keys/indexes and does a look up on the full list of pages to see what their names are and where to navigate to if selected.


you can use some king of database on top of isolated storage, like Sterling DB for example. Then you can query / delete favorites using LINQ.

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜