【UWP開發】uwp如何在win10和xbox one本地保存存檔

使用說明

uwp官方是建議通過xboxlive保存存檔的,如果需要使用本地存檔的接口,使用如下方法。


讀取存檔

*導入命名空間

using Windows.Foundation;
using Windows.Storage;
using Windows.System;

*讀取存檔接口

public void LoadData(string userId,System.Action<string> successHandle,System.Action<string> failHandle)
    {
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        IAsyncOperation<StorageFile> saveFileAsync = storageFolder.GetFileAsync(GetSaveDataFileName(userId));
        saveFileAsync.Completed = (IAsyncOperation<StorageFile> info, AsyncStatus status) =>
        {
            if (status == AsyncStatus.Completed)
            {
                StorageFile saveFile = info.GetResults();
                IAsyncOperation<string> readAsync = FileIO.ReadTextAsync(saveFile);
                    readAsync.Completed = (IAsyncOperation<string> readInfo, AsyncStatus readStatus) =>
                {
                    if (readStatus == AsyncStatus.Completed)
                    {
                        string data = readInfo.GetResults();
                        if (successHandle != null)
                        {
                            successHandle(data);
                        }
                    }
                };
            }
            else
            {
                if (failHandle != null)
                {
                    failHandle("");
                }
            }
        };
    }


保存存檔

public void SaveData(string userId, string data, System.Action successHandle, System.Action<string> failHandle)
    {
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        IAsyncOperation<StorageFile> saveFileAsync = storageFolder.CreateFileAsync(GetSaveDataFileName(userId),CreationCollisionOption.ReplaceExisting);
        saveFileAsync.Completed = (IAsyncOperation<StorageFile> info, AsyncStatus status) =>
        {
            Debug.Log("SaveStatus:" + status.ToString());
            if (status == AsyncStatus.Completed)
            {
                StorageFile saveFile = info.GetResults();
                IAsyncAction writeAsync = FileIO.WriteTextAsync(saveFile, data);
                writeAsync.Completed = (IAsyncAction writeInfo, AsyncStatus writeStatus) =>
                {
                    if (writeStatus == AsyncStatus.Completed)
                    {
                        if (successHandle != null)
                        {
                            successHandle();
                        }
                    }
                    else
                    {
                        if (failHandle != null) failHandle("");
                    }
                };
            }
            else
            {
                if (failHandle != null) failHandle("");
            }
        };
    }

    string GetSaveDataFileName(string userId)
    {
        return "LittleTriangle.sav";
        //奇怪的是不能使用後面的文件命名
        //if (string.IsNullOrEmpty(userId))
        //{
        //return "LittleTriangle.sav";
        //}
        //else {
        //    return userId + ".sav";
        //}
    }

遇到的問題:

●編譯之後觸發存檔,提示存檔成功,再次讀檔卻讀不到?

原來每次編譯會自動先把之前的應用卸載掉,之前的存檔也就沒了...


●存檔路徑位置?

C:\Users\用戶名\AppData\Local\Packages\應用包名xxx\LocalState\LittleTriangle.sav


●存檔名字規範?

這個是最讓我困惑的,我命名成LittleTriangle_【userId】.sav,或者【userId】.sav(【userId】爲獲取的玩家id),提示我存檔失敗,也就是應該是userId有一些不符合規範的符號,所以不能存檔。


●官方文檔的寫法提示報錯?

官方文檔的寫法是通過wait等待,發現在項目中這樣寫會報錯。猜測因爲官方是XAML而我是D3D。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章