8

I have a List<class> of data. And I want to save it and retrieve it every time my app starts and exits respectively. What is the equivalent of IsolatedStorage (WP7) in Windows 8. How can I save these settings?


How to remove leading comment whitespace in Perl::Tidy?

I'm just configuring Perl::Tidy to match my preference. I have only one issue left which I can't find a fix.

Sample script:

#!/usr/bin/perl
#   |   |   |   |   |   < "|" indicates first five "tabs" (1 tab 4 spaces).
use strict;     # Enable strict programming mode.
use warnings;   # Enable Perl warnings.
use utf8;       # This is an UTF-8 encoded script.
1;

perltidyrc:

# Perl Best Practices (plus errata) .perltidyrc file

-l=76   # Max line width is 76 cols
-i=4    # Indent level is 4 cols
-ci=4   # Continuation indent is 4 cols
-et=4   # 1 tab represent 4 cols
-st     # Output to STDOUT
-se     # Errors to STDERR
-vt=2   # Maximal vertical tightness
-cti=0  # No extra indentation for closing brackets
-pt=0   # Medium parenthesis tightness
-bt=1   # Medium brace tightness
-sbt=1  # Medium square bracket tightness
-bbt=1  # Medium block brace tightness
-nsfs   # No space before semicolons
-nolq   # Don't outdent long quoted strings
-wbb="% + - * / x != == >= <= =~ < > | & **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="
        # Break before all operators

# extras/overrides/deviations from PBP

#--maximum-line-length=100               # be slightly more generous
--warning-output                        # Show warnings
--maximum-consecutive-blank-lines=2     # default is 1
--nohanging-side-comments               # troublesome for commented out code

-isbc   # block comments may only be indented if they have some space characters before the #

# for the up-tight folk :)
-pt=2   # High parenthesis tightness
-bt=2   # High brace tightness
-sbt=2  # High square bracket tightness

Result:

#!/usr/bin/perl
#   |   |   |   |   |   < "|" indicates first five "tabs" (1 tab 4 spaces).
use strict;      # Enable strict programming mode.
use warnings;    # Enable Perl warnings.
use utf8;        # This is an UTF-8 encoded script.
1;

As you can see there is a leading space which causes that the "#" doesn't match the forth tab.

How to remove this leading space?

4

2 に答える 2

8

Windows 8 では、次を使用してLocalFolderアクセスできるアプリに を使用する必要があります。

StorageFolder folder = ApplicationData.Current.LocalFolder;

次に、以下を使用してそこに保存されたファイルを参照します。

var fileToGet = await folder.GetFileAsync("nameOfFile.fileType");

私は現在、私が取り組んでいるプロジェクトで同様の状況にあり、カスタム オブジェクトのリストを Apps LocalFolder に保存し、後でリロードしたいと考えています。

私の解決策は、リストを XML 文字列にシリアル化し、これを App フォルダーに保存することでした。あなたは私の方法を適応させることができるはずです:

static public string SerializeListToXml(List<CustomObject> List)
    {
        try
        {
            XmlSerializer xmlIzer = new XmlSerializer(typeof(List<CustomObject>));
            var writer = new StringWriter();
            xmlIzer.Serialize(writer, List);
            System.Diagnostics.Debug.WriteLine(writer.ToString());
            return writer.ToString();
        }

        catch (Exception exc)
        {
            System.Diagnostics.Debug.WriteLine(exc);
            return String.Empty;
        }

文字列を取得したので、それをテキスト ファイルに保存して、LocalStorage に配置できます。

//assuming you already have a list with data called myList
await Windows.Storage.FileIO.WriteTextAsync("xmlFile.txt", SerializeListToXml(myList));

アプリを再度読み込むと、上記の読み込みメソッドを使用して LocalStorage から xmlFile を取得し、それを逆シリアル化して List を取得できます。

string listAsXml = await Windows.Storage.FileIO.ReadTextAsync(xmlFile.txt);
List<CustomObject> deserializedList = DeserializeXmlToList(listAsXml);

繰り返しますが、これをニーズに合わせて調整します。

public static List<CustomObject> DeserializeXmlToList(string listAsXml)
    {
        try
        {
            XmlSerializer xmlIzer = new XmlSerializer(typeof(List<CustomObject>));
            XmlReader xmlRead = XmlReader.Create(listAsXml);
            List<CustomObject> myList = new List<CustomObject>();
            myList = (xmlIzer.Deserialize(xmlRead)) as List<CustomObject>;
            return myList;
        }

        catch (Exception exc)
        {
            System.Diagnostics.Debug.WriteLine(exc);
            List<CustomObject> emptyList = new List<CustomObject>();
            return emptyList;
        }
    }
于 2012-10-07T13:26:10.783 に答える
5

このクラスを使用して、設定を保存およびロードできます。

public static class ApplicationSettings
{
    public static void SetSetting<T>(string key, T value, bool roaming = true)
    {
        var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings;
        settings.Values[key] = value;
    }

    public static T GetSetting<T>(string key, bool roaming = true)
    {
        return GetSetting(key, default(T), roaming);
    }

    public static T GetSetting<T>(string key, T defaultValue, bool roaming = true)
    {
        var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings;
        return settings.Values.ContainsKey(key) &&
               settings.Values[key] is T ?
               (T)settings.Values[key] : defaultValue;
    }

    public static bool HasSetting<T>(string key, bool roaming = true)
    {
        var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings;
        return settings.Values.ContainsKey(key) && settings.Values[key] is T;
    }

    public static bool RemoveSetting(string key, bool roaming = true)
    {
        var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings;
        if (settings.Values.ContainsKey(key))
            return settings.Values.Remove(key);
        return false;
    }
}

ただし、プリミティブ型 (bool、int、string など) のみを保存およびロードできます。これが、リストを XML または文字列に格納できる別の形式にシリアル化する必要がある理由です。オブジェクトを XML との間でシリアライズおよびデシリアライズするには、次のメソッドを使用できます。

public static string Serialize(object obj)
{
    using (var sw = new StringWriter()) 
    {
        var serializer = new XmlSerializer(obj.GetType());
        serializer.Serialize(sw, obj);
        return sw.ToString();
    }
}

public static T Deserialize<T>(string xml)
{
    using (var sw = new StringReader(xml))
    {
        var serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(sw);
    }
}

また、Windows ストア アプリの ApplicationSettings に独自のクラスのインスタンスを格納する方法はありますか?も参照してください。

于 2012-10-07T22:21:40.187 に答える