将任务<string>类型保存为String,导致灾难性故障

时间:2015-06-10 08:48:40

标签: c# windows-store-apps base64 async-await windows-8.1

我正在尝试将我在Canvas上写的Ink保存在一个类中,然后使用在link找到的这两种方法重新加载它。

    private async Task<string> WriteInk() {
        using (var stream = new InMemoryRandomAccessStream()) {
            await inkManager.SaveAsync(stream);
            await stream.FlushAsync();
            stream.Seek(0);
            byte[] bytes = new byte[stream.Size];
            IBuffer buffer = bytes.AsBuffer();
            await stream.ReadAsync(buffer, (uint)stream.Size, InputStreamOptions.None);
            return Convert.ToBase64String(bytes);
        }
    }

    private async void ReadInk(string base64) {
        if (string.IsNullOrEmpty(base64)) return;
        var bytes = Convert.FromBase64String(base64);

        using (var stream = new InMemoryRandomAccessStream()) {
            await stream.WriteAsync(bytes.AsBuffer());
            await stream.FlushAsync();
            stream.Seek(0);

            await inkManager.LoadAsync(stream);

            if (inkManager.GetStrokes().Count > 0) {
                RenderInk();
            }
        }
    }

我有一个班级,我保存从WriteInk()返回的base64:

    public class MyCanvas
    {
        public string canvasIndex { get; set; }
        public String canvasStrokes { get; set; }
    }

问题在于调用WriteInk()

MyCanvas mc = new MyCanvas();
mc.canvasStrokes = await m_CanvasManager1.WriteInk();

我得到一个例外:灾难性的失败。 +我无法生成canvasStrokes类型的string并将Task<string>保存到其中 +方法ReadInk()将base64视为string

我应该将Task<string>保存为哪种类型,以便能够在第二种方法中将其用作string

当我调用WriteInk时,它会到达await inkCanvas.SaveAsync(stream); 它返回到mc.canvasStrokes = await m_CanvasManager0.WriteInk();处调用的位置并给出此异常:

An exception of type 'System.Exception' occurred in mscorlib.dll but was not handled in user code. Additional information: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

0 个答案:

没有答案