if write both better for every appliction to maintaing state temprarly
-----------------------------------------------------------------------
1)to store data in temparary
----------------------------
PhoneApplicationService phappserv = PhoneApplicationService.Current;
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
phappserv.State["myvalue"] = textBox1.Text;
base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
object somevalue ="";
if (phappserv.State.ContainsKey("myvalue"))
{
if (phappserv.State.TryGetValue("myvalue", out somevalue))
{
textBox1.Text = somevalue.ToString();
}
}
base.OnNavigatedTo(e);
}
2)tostore data for longterm
--------------------------
in app.xaml
-------------
helper methods
---------------
private void savestate()
{
PhoneApplicationService phoneappservice = PhoneApplicationService.Current;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["myvalue"] = phoneappservice.State["myvalue"];
}
private void loadstate()
{
PhoneApplicationService phoneappservice = PhoneApplicationService .Current;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
string myvalue = "";
if(settings.TryGetValue<string>("myvalue",out myvalue))
{
phoneappservice.State["myvalue"] = myvalue;
}
}
call these methods under
------------------------
private void Application_Launching(object sender, LaunchingEventArgs e)
{
loadstate();
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
loadstate();
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
savestate();
}
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
savestate();
}
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
synario: for example few files are there in isolated storage
prob: how to get filenames,which are stored in isolated storage
sol:
isolatedstoragefile(variablename) = IsolatedStorageFile.GetuserStoreforappication();
listbox.itemssource = isolatedstoragefile.GetFilenames();
//we need bind the property in the souce code also
i.e,(Content="{Binding})
<ListBox Height="200" HorizontalAlignment="Left" Margin="12,25,0,0" Name="listBox1"
VerticalAlignment="Top" Width="460" Background="Red" Visibility="Collapsed">
<ListBox.ItemTemplate>
<DataTemplate>
<HyperlinkButton Content="{Binding}" Click="HyperlinkButton_Click" >
</HyperlinkButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
in listbox i have links visible like this
file1.txt
---------
file2.txt
---------
file3.txt
---------
when user clicks any file he need to navigate another page with relavent filename with relavent content for that
the below code
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
// i need to write code here............
HyperlinkButton h = (HyperlinkButton)sender;
// i am contructing the uri
string uri = string.Format("/WindowsraviPhoneApplication2;component/Page1.xaml?id={0}", h.Content);
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
}
in the destination load event the code as follows
-------------------------------------------------
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isostrofile = IsolatedStorageFile.GetUserStoreForApplication();
string filename = "";
// string filename = NavigationContext.QueryString["id"];
if(NavigationContext.QueryString.TryGetValue("id",out filename))
{
using (var file = isostrofile.OpenFile(filename, System.IO.FileMode.Open, FileAccess.Read))
{
StreamReader sr = new StreamReader(file);
textBlock1.Text = sr.ReadToEnd();
}
}
}
http://blogs.msdn.com/b/mingfeis_code_block/archive/2010/10/03/windows-phone-7-how-to-store-data-and-pass-data-
between-pages.aspx
http://innovativesingapore.com/2010/09/code-how-to-store-data-and-pass-data-in-windows-phone/(data sotre permentely)
-----------------------------------------------------------------------
1)to store data in temparary
----------------------------
PhoneApplicationService phappserv = PhoneApplicationService.Current;
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
phappserv.State["myvalue"] = textBox1.Text;
base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
object somevalue ="";
if (phappserv.State.ContainsKey("myvalue"))
{
if (phappserv.State.TryGetValue("myvalue", out somevalue))
{
textBox1.Text = somevalue.ToString();
}
}
base.OnNavigatedTo(e);
}
2)tostore data for longterm
--------------------------
in app.xaml
-------------
helper methods
---------------
private void savestate()
{
PhoneApplicationService phoneappservice = PhoneApplicationService.Current;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["myvalue"] = phoneappservice.State["myvalue"];
}
private void loadstate()
{
PhoneApplicationService phoneappservice = PhoneApplicationService .Current;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
string myvalue = "";
if(settings.TryGetValue<string>("myvalue",out myvalue))
{
phoneappservice.State["myvalue"] = myvalue;
}
}
call these methods under
------------------------
private void Application_Launching(object sender, LaunchingEventArgs e)
{
loadstate();
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
loadstate();
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
savestate();
}
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
savestate();
}
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
synario: for example few files are there in isolated storage
prob: how to get filenames,which are stored in isolated storage
sol:
isolatedstoragefile(variablename) = IsolatedStorageFile.GetuserStoreforappication();
listbox.itemssource = isolatedstoragefile.GetFilenames();
//we need bind the property in the souce code also
i.e,(Content="{Binding})
<ListBox Height="200" HorizontalAlignment="Left" Margin="12,25,0,0" Name="listBox1"
VerticalAlignment="Top" Width="460" Background="Red" Visibility="Collapsed">
<ListBox.ItemTemplate>
<DataTemplate>
<HyperlinkButton Content="{Binding}" Click="HyperlinkButton_Click" >
</HyperlinkButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
in listbox i have links visible like this
file1.txt
---------
file2.txt
---------
file3.txt
---------
when user clicks any file he need to navigate another page with relavent filename with relavent content for that
the below code
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
// i need to write code here............
HyperlinkButton h = (HyperlinkButton)sender;
// i am contructing the uri
string uri = string.Format("/WindowsraviPhoneApplication2;component/Page1.xaml?id={0}", h.Content);
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
}
in the destination load event the code as follows
-------------------------------------------------
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isostrofile = IsolatedStorageFile.GetUserStoreForApplication();
string filename = "";
// string filename = NavigationContext.QueryString["id"];
if(NavigationContext.QueryString.TryGetValue("id",out filename))
{
using (var file = isostrofile.OpenFile(filename, System.IO.FileMode.Open, FileAccess.Read))
{
StreamReader sr = new StreamReader(file);
textBlock1.Text = sr.ReadToEnd();
}
}
}
http://blogs.msdn.com/b/mingfeis_code_block/archive/2010/10/03/windows-phone-7-how-to-store-data-and-pass-data-
between-pages.aspx
http://innovativesingapore.com/2010/09/code-how-to-store-data-and-pass-data-in-windows-phone/(data sotre permentely)
No comments:
Post a Comment