【问题标题】:Testing Roaming App Data in Emulator C#在模拟器 C# 中测试漫游应用程序数据
【发布时间】:2017-06-03 14:08:12
【问题描述】:

我正在使用 Visual Studio 2017 并尝试为 Windows 手机编写 C# UWP 应用程序。当我尝试在主机和模拟器(移动模拟器 10.0.15063.0)之间测试漫游应用程序数据时,数据不同步。即使等了几个小时。模拟器似乎可以跨设备同步我的电子邮件和日历。当我在主机上搜索文件时,它似乎已正确创建:

C:\Users\Owner\AppData\Local\Packages\23840StartPoint.StartPointRunning_x6wjmepvaasdr\RoamingState

我已经检查了包清单并且包名称和版本匹配。我尝试了几种不同的选择,但没有任何效果。我是不是错过了什么。

来自 GitHub 的应用程序数据示例是我用来自学编写/读取文件的模型。它也不适用于我的设置。

var applicationData = ApplicationData.Current;

ulong storage = applicationData.RoamingStorageQuota;

//Create file client.txt => store data => Roaming folder
ApplicationDataContainer roamingSettings =   ApplicationData.Current.RoamingSettings;

StorageFolder roamingFolder1 = ApplicationData.Current.RoamingFolder;
StorageFile file1 = await roamingFolder1.CreateFileAsync(Filename1, CreationCollisionOption.ReplaceExisting);

SetUpFlag = "true";
String string6 = Views.Colors101.m_BackGroundColor + ";" +  Views.Colors101.m_CalendarColor + ";" + Views.Colors101.m_HeaderColor;

String toWrite = SetUpFlag + ";" + string6;

StorageFile xfile1 = await roamingFolder1.GetFileAsync(Filename1);

await FileIO.WriteTextAsync(xfile1, toWrite);

ApplicationData.Current.SignalDataChanged();

【问题讨论】:

  • 如何在pc和uwp之间共享数据??
  • 我正在通过漫游文件夹共享一个小文本文件(大约 150 字节)。我是新手,不确定如何在我的评论中添加代码。你能推荐一个方法吗。我尝试添加反引号但没有用。
  • 您可以编辑您的原始问题以添加相关代码 sn-p 和其他更新。
  • 您能否在您的设备上测试official sample?如果您有条件,能否请您在电脑和其他手机设备上测试您的项目?
  • 太好了,我一定会试试这个。来自 GitHub 的应用程序数据示例是我用来自学编写/读取文件的模型。它在我的设置中也不起作用。请参考添加到我的原始问题中的代码。

标签: c# uwp


【解决方案1】:

也许试试我的版本,看看效果是否更好。

const string _listofStuffFile = "allMyStuff.txt";
public static async Task<bool> SaveMyListData(List<string> saveData)
    {
        try
        {
            var savedStuffFile = await ApplicationData.Current.RoamingFolder.CreateFileAsync(_listofStuffFile, CreationCollisionOption.ReplaceExisting);
            using (Stream ws = await savedStuffFile.OpenStreamForWriteAsync())
            {
                var ss = new DataContractSerializer(typeof(List<string>));
                ss.WriteObject(ws, saveData);
            }
            return true;
        }
        catch { }
        return false;
    }

public static async Task<List<string>> GetMyListData()
    {
        try
        {
            var rs = await ApplicationData.Current.RoamingFolder.OpenStreamForReadAsync(_listofStuffFile);
            if (rs == null)
                return new List<string>();
            var ss = new DataContractSerializer(typeof(List<string>));
            var sr = (List<string>)ss.ReadObject(rs);
            return sr;
        }
        catch { }
        return new List<string>();
    }

【讨论】:

  • 感谢您@ArchAngel 的努力。不幸的是,您的解决方案也不起作用。我读到,在调试模式下,应用程序不会挂起。是否有可能我需要做一些事情来强制暂停,我是否需要做一些特殊的事情来在暂停时释放文件句柄。
猜你喜欢
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多