navigation between page windows phone without relo

2019-08-26 01:57发布

private void btn_friends_pressed(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Friends.xaml", UriKind.Relative));
        }

When I press the button I go to the Friends page, which loads many friends from isolated storage.Than I press "back" button and go to the Menu page, when I press again the button, I have "Operation not permitted on IsolatedStorageFileStream." message. How I can not reload page and keep it in RAM. Something like:

if (Friends.Page.IsRunning==true)
    NavigationService.Navigate("/Friends.xaml");
else
    NavigationService.Navigate(new Uri("/Friends.xaml", UriKind.Relative));

2条回答
神经病院院长
2楼-- · 2019-08-26 02:16

Whenever you navigate to a page, it is reloaded automatically. The pages themselves are not kept in memory once you've navigated away from them. If you want to store it memory, and not read it from Isolated Storage each time, then you can simply create a static class that contains a static List that stores your friends. Once you've loaded your friends, depending on their type, you can add it to the list. Whenever you need to access them, simply call it from the static List. For example, in your solution, create a new class:

using ... //your using directives

namespace MyApp //Your project Namespace
{
    public static class FriendsStorage //rename `FriendsStorage` to whatever you want 
    {
         public static List<Friends> ListOfFriends = new List<Friends>(); //Your list 
    }
}

To set it, you can load the information from IsolatedStorage and add it to the list:

foreach(Friend f in Friends)
   FriendsStorage.ListOfFriends.Add(f);

Whenever you need to query the Friends list you can call it like this:

var friendList = FriendsStorage.ListOfFriends;

Even if you use the above method, you should try and fix the error you're getting. Can you post your Isolated Storage code?

查看更多
劳资没心,怎么记你
3楼-- · 2019-08-26 02:29

If you want to get rid of the error message, you should use your stream in a using() block,

using (var stream = new IsolatedStorageFileStream(...))
{
   // load your data here
}

Regarding saving page, it's generally not a good idea because your memory can exponentialy grow and your application will be very unresponsive.

Although you can always use your App.xaml.cs as a global instance of your application to cache some of your data sources:

List<Friend> _Friends;
List<Friend> _Friends
{
    get
    {
        if(_Friends == null) _Friends = GetFriends();
        return _Friends;
    }
}

but if you did this be very careful not to store loads of data.

查看更多
登录 后发表回答