Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[Win8app] Waarde die vaak wijzigd opslaan

Pagina: 1
Acties:

  • Douweegbertje
  • Registratie: Mei 2008
  • Laatst online: 30-10 12:53

Douweegbertje

Wat kinderachtig.. godverdomme

Topicstarter
En voila, mn tweede topic :+

Again; ik ben vrij nieuw met C# en de manier van programmeren dus ik hoop dat het niet al te ' noob' overkomt.

Ik heb een zogenaamde ' settings' aka storage class waar ik een waarde een set/get kan meegeven, maar waarbij in eerste instatie de waarde ook eventueel uit de local storage gehaald kan worden (en anders default value geeft)

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public Settings()
        {
            try
            {
                // Get the settings for this application.
                settings = IsolatedStorageSettings.ApplicationSettings;

            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }


        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }

            return valueChanged;
        }


       
        public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key))
            {
                value = (valueType)settings[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }


 public int CookieCountSetting
        {
            get
            {
                return GetValueOrDefault<int>(CookieCountSettingKeyName, CookieCountSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(CookieCountSettingKeyName, value))
                {
                    Save();
                }
            }
        }


Op een ' Clicked' event moet deze waarde met +1 omhoog gaan;

C#:
1
TextTotalClicked.Text = settings.CookieCountSetting++.ToString();


Nu vind ik dit wel best, echter ga ik dit aardig uitbreiden waarbij deze CountSetting op een gegeven moment om de 1 seconden -of meer- een update krijgt.

Is dit nu efficient? Ik weet dat het wel werkt, maar is er niet een mogelijkheid om als het ware deze value gewoon in een losse variable te zetten en bij een /exit pas op te slaan?

Windows 8 App heeft hier gewoon de mogelijkheden voor middels;

C#:
1
2
3
4
5
6
7
8
9
10
11
 // 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)
        {
        }

        // 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)
        {
        }


Alleen zou ik mijn god niet weten hoe ik dan een variabele die in een andere functie zit hier kan krijgen.

In elk geval, doe ik het nu " goed" of kan het 10x betere? :+

  • Caelorum
  • Registratie: April 2005
  • Laatst online: 14:18
Settings bij opstarten uitlezen, tussentijds cachen in Settings class en dan bij afsluiten allemaal wegschrijven? Zal je wel een DI moeten gebruiken of een singleton van Settings moeten maken ofzo.

[ Voor 26% gewijzigd door Caelorum op 24-10-2013 19:44 ]


  • Douweegbertje
  • Registratie: Mei 2008
  • Laatst online: 30-10 12:53

Douweegbertje

Wat kinderachtig.. godverdomme

Topicstarter
Ik begin nu dan met:

C#:
1
2
3
4
5
6
7
8
9
10
 private Settings settings = new Settings();

        public int CookieCount;

        public Game()
        {
            InitializeComponent();


            CookieCount = settings.CookieCountSetting;


Vervolgens doe ik dit, wat technisch gezien dan mijn data moet opslaan;

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
// 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)
        {
            settings.CookieCountSetting = CookieCount;
        }

        // 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)
        {
            settings.CookieCountSetting = CookieCount;
        }


Echter als ik de app sluit, en weer start.. is mijn value gewoon weer 0.

  • Caelorum
  • Registratie: April 2005
  • Laatst online: 14:18
Moet je voor Windows 8 niet ApplicationDataContainer gebruiken in plaats van IsolatedStorageSettings? Kijk anders even naar MSDN: Quickstart: Local application data (Windows Store apps using C#/VB/C++ and XAML) (Windows)

  • Douweegbertje
  • Registratie: Mei 2008
  • Laatst online: 30-10 12:53

Douweegbertje

Wat kinderachtig.. godverdomme

Topicstarter
Nope, ik gebruikte een Example en deze werkte wel goed op deze manier.
Mijn probleem is ook niet direct de storage zelf, want dit werkt als ik het gewoon ergens anders neer zet.

Juist de Application_Deactivated en Application_Closing worden simpelweg genegeerd

  • Douweegbertje
  • Registratie: Mei 2008
  • Laatst online: 30-10 12:53

Douweegbertje

Wat kinderachtig.. godverdomme

Topicstarter
Daar ook gelijk mijn antwoord :+

Ik gebruik op mijn pagina dit;

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    <phone:PhoneApplicationPage
    x:Class="CookieClicker.Game"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">
   
</phone:PhoneApplicationPage>


Terwijl in de algemene App.xaml dit staat.


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<Application
    x:Class="CookieClicker.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <!--Application Resources-->
    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:CookieClicker" x:Key="LocalizedStrings"/>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService
            Launching="Application_Launching" Closing="Application_Closing"
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>


Dus ik moet dan nog even bekijken of ik dan wel buiten de App.cs dit kan aanroepen :)
Pagina: 1