-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update persistent storage example for WPF Framework (#1856)
* Update example * cull some needless comments
- Loading branch information
Showing
22 changed files
with
912 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...app-development/snippets/persist-and-restore-application-scope-properties/csharp/App.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Application x:Class="SDKSamples.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
StartupUri="MainWindow.xaml" | ||
Startup="App_Startup" | ||
Exit="App_Exit"> | ||
</Application> |
66 changes: 66 additions & 0 deletions
66
...-development/snippets/persist-and-restore-application-scope-properties/csharp/App.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.IO.IsolatedStorage; | ||
using System.IO; | ||
using System.Windows; | ||
|
||
namespace SDKSamples | ||
{ | ||
public partial class App : Application | ||
{ | ||
string _filename = "App.data"; | ||
|
||
public App() | ||
{ | ||
// Initialize application-scope property | ||
Properties["NumberOfAppSessions"] = "0"; | ||
} | ||
|
||
private void App_Startup(object sender, StartupEventArgs e) | ||
{ | ||
// Restore application-scope property from isolated storage | ||
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain(); | ||
try | ||
{ | ||
if (storage.FileExists(_filename)) | ||
{ | ||
using (IsolatedStorageFileStream stream = storage.OpenFile(_filename, FileMode.Open, FileAccess.Read)) | ||
using (StreamReader reader = new StreamReader(stream)) | ||
{ | ||
// Restore each application-scope property individually | ||
while (!reader.EndOfStream) | ||
{ | ||
string[] keyValue = reader.ReadLine().Split(new char[] { ',' }); | ||
Properties[keyValue[0]] = keyValue[1]; | ||
} | ||
} | ||
} | ||
} | ||
catch (DirectoryNotFoundException ex) | ||
{ | ||
// Path the file didn't exist | ||
} | ||
catch (IsolatedStorageException ex) | ||
{ | ||
// Storage was removed or doesn't exist | ||
// -or- | ||
// If using .NET 6+ the inner exception contains the real cause | ||
} | ||
} | ||
|
||
private void App_Exit(object sender, ExitEventArgs e) | ||
{ | ||
// Increase the amount of times the app was opened | ||
Properties["NumberOfAppSessions"] = int.Parse((string)Properties["NumberOfAppSessions"]) + 1; | ||
|
||
// Persist application-scope property to isolated storage | ||
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain(); | ||
using (IsolatedStorageFileStream stream = storage.OpenFile(_filename, FileMode.Create, FileAccess.Write)) | ||
using (StreamWriter writer = new StreamWriter(stream)) | ||
{ | ||
// Persist each application-scope property individually | ||
foreach (string key in Properties.Keys) | ||
writer.WriteLine("{0},{1}", key, Properties[key]); | ||
} | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...elopment/snippets/persist-and-restore-application-scope-properties/csharp/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Windows; | ||
|
||
[assembly:ThemeInfo( | ||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located | ||
//(used if a resource is not found in the page, | ||
// or application resource dictionaries) | ||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located | ||
//(used if a resource is not found in the page, | ||
// app, or any theme specific resource dictionaries) | ||
)] |
14 changes: 14 additions & 0 deletions
14
...elopment/snippets/persist-and-restore-application-scope-properties/csharp/MainWindow.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Window x:Class="SDKSamples.MainWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:SDKSamples" | ||
mc:Ignorable="d" | ||
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded"> | ||
<Grid> | ||
<Label Content="Times app was opened: " HorizontalAlignment="Left" Margin="31,80,0,0" VerticalAlignment="Top"/> | ||
<Label Content="X" HorizontalAlignment="Left" Margin="168,80,0,0" VerticalAlignment="Top" x:Name="lblTimes"/> | ||
|
||
</Grid> | ||
</Window> |
32 changes: 32 additions & 0 deletions
32
...pment/snippets/persist-and-restore-application-scope-properties/csharp/MainWindow.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
using System.Windows.Documents; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Imaging; | ||
using System.Windows.Shapes; | ||
|
||
namespace SDKSamples | ||
{ | ||
/// <summary> | ||
/// Interaction logic for MainWindow.xaml | ||
/// </summary> | ||
public partial class MainWindow : Window | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
private void Window_Loaded(object sender, RoutedEventArgs e) | ||
{ | ||
lblTimes.Content = App.Current.Properties["NumberOfAppSessions"]; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...lopment/snippets/persist-and-restore-application-scope-properties/csharp/ProjectCS.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net481</TargetFramework> | ||
<UseWPF>true</UseWPF> | ||
<RootNamespace>SDKSamples</RootNamespace> | ||
<ApplicationManifest>app.manifest</ApplicationManifest> | ||
</PropertyGroup> | ||
|
||
</Project> |
77 changes: 77 additions & 0 deletions
77
...development/snippets/persist-and-restore-application-scope-properties/csharp/app.manifest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/> | ||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> | ||
<security> | ||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<!-- UAC Manifest Options | ||
If you want to change the Windows User Account Control level replace the | ||
requestedExecutionLevel node with one of the following. | ||
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> | ||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> | ||
<requestedExecutionLevel level="highestAvailable" uiAccess="false" /> | ||
Specifying requestedExecutionLevel element will disable file and registry virtualization. | ||
Remove this element if your application requires this virtualization for backwards | ||
compatibility. | ||
--> | ||
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> | ||
</requestedPrivileges> | ||
</security> | ||
</trustInfo> | ||
|
||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> | ||
<application> | ||
<!-- A list of the Windows versions that this application has been tested on | ||
and is designed to work with. Uncomment the appropriate elements | ||
and Windows will automatically select the most compatible environment. --> | ||
|
||
<!-- Windows Vista --> | ||
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />--> | ||
|
||
<!-- Windows 7 --> | ||
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />--> | ||
|
||
<!-- Windows 8 --> | ||
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />--> | ||
|
||
<!-- Windows 8.1 --> | ||
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />--> | ||
|
||
<!-- Windows 10 --> | ||
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />--> | ||
|
||
</application> | ||
</compatibility> | ||
|
||
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher | ||
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need | ||
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should | ||
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. | ||
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation --> | ||
<!-- | ||
<application xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<windowsSettings> | ||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware> | ||
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware> | ||
</windowsSettings> | ||
</application> | ||
--> | ||
|
||
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) --> | ||
<dependency> | ||
<dependentAssembly> | ||
<assemblyIdentity | ||
type="win32" | ||
name="Microsoft.Windows.Common-Controls" | ||
version="6.0.0.0" | ||
processorArchitecture="*" | ||
publicKeyToken="6595b64144ccf1df" | ||
language="*" | ||
/> | ||
</dependentAssembly> | ||
</dependency> | ||
|
||
</assembly> |
6 changes: 6 additions & 0 deletions
6
...f/app-development/snippets/persist-and-restore-application-scope-properties/vb/App.config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" /> | ||
</startup> | ||
</configuration> |
7 changes: 7 additions & 0 deletions
7
...development/snippets/persist-and-restore-application-scope-properties/vb/Application.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Application x:Class="Application" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
StartupUri="MainWindow.xaml" | ||
Startup="App_Startup" | ||
Exit="App_Exit"> | ||
</Application> |
61 changes: 61 additions & 0 deletions
61
...elopment/snippets/persist-and-restore-application-scope-properties/vb/Application.xaml.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
Imports System.IO | ||
Imports System.IO.IsolatedStorage | ||
|
||
Class Application | ||
|
||
Private _filename As String = "App.data" | ||
|
||
Public Sub New() | ||
' Initialize application-scope property | ||
Properties("NumberOfAppSessions") = "0" | ||
End Sub | ||
|
||
Private Sub App_Startup(ByVal sender As Object, ByVal e As StartupEventArgs) | ||
' Restore application-scope property from isolated storage | ||
Dim storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain() | ||
Try | ||
If storage.FileExists(_filename) Then | ||
|
||
Using stream As IsolatedStorageFileStream = storage.OpenFile(_filename, FileMode.Open, FileAccess.Read) | ||
Using reader As New StreamReader(stream) | ||
|
||
' Restore each application-scope property individually | ||
Do While Not reader.EndOfStream | ||
Dim keyValue() As String = reader.ReadLine().Split(New Char() {","c}) | ||
Properties(keyValue(0)) = keyValue(1) | ||
Loop | ||
|
||
End Using | ||
End Using | ||
|
||
End If | ||
|
||
Catch ex As DirectoryNotFoundException | ||
' Path the file didn't exist | ||
Catch ex As IsolatedStorageException | ||
' Storage was removed or doesn't exist | ||
' -or- | ||
' If using .NET 6+ the inner exception contains the real cause | ||
End Try | ||
End Sub | ||
|
||
Private Sub App_Exit(ByVal sender As Object, ByVal e As ExitEventArgs) | ||
'Increase the amount of times the app was opened | ||
Properties("NumberOfAppSessions") = Integer.Parse(DirectCast(Properties("NumberOfAppSessions"), String)) + 1 | ||
|
||
' Persist application-scope property to isolated storage | ||
Dim storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain() | ||
|
||
Using stream As IsolatedStorageFileStream = storage.OpenFile(_filename, FileMode.Create, FileAccess.Write) | ||
Using writer As New StreamWriter(stream) | ||
|
||
' Persist each application-scope property individually | ||
For Each key As String In Properties.Keys | ||
writer.WriteLine("{0},{1}", key, Properties(key)) | ||
Next key | ||
|
||
End Using | ||
End Using | ||
End Sub | ||
|
||
End Class |
11 changes: 11 additions & 0 deletions
11
...-development/snippets/persist-and-restore-application-scope-properties/vb/AssemblyInfo.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Imports System.Windows | ||
|
||
'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found. | ||
'1st parameter: where theme specific resource dictionaries are located | ||
'(used if a resource is not found in the page, | ||
' or application resource dictionaries) | ||
|
||
'2nd parameter: where the generic resource dictionary is located | ||
'(used if a resource is not found in the page, | ||
'app, and any theme specific resource dictionaries) | ||
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)> |
14 changes: 14 additions & 0 deletions
14
...-development/snippets/persist-and-restore-application-scope-properties/vb/MainWindow.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Window x:Class="MainWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:SDKSamples" | ||
mc:Ignorable="d" | ||
Title="MainWindow" Height="450" Width="800" | ||
Loaded="Window_Loaded"> | ||
<Grid> | ||
<Label Content="Times app was opened: " HorizontalAlignment="Left" Margin="15,376,0,0" VerticalAlignment="Top"/> | ||
<Label Content="X" HorizontalAlignment="Left" Margin="152,376,0,0" VerticalAlignment="Top" x:Name="lblTimes"/> | ||
</Grid> | ||
</Window> |
5 changes: 5 additions & 0 deletions
5
...velopment/snippets/persist-and-restore-application-scope-properties/vb/MainWindow.xaml.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Public Class MainWindow | ||
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) | ||
lblTimes.Content = Application.Current.Properties("NumberOfAppSessions") | ||
End Sub | ||
End Class |
Oops, something went wrong.