navigation between page windows phone without reloading

强颜欢笑 提交于 2019-12-25 01:10:03

问题


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

回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/6554276/navigation-between-page-windows-phone-without-reloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!