【问题标题】:Open string as correct filetype in Xamarin.Forms在 Xamarin.Forms 中以正确的文件类型打开字符串
【发布时间】:2018-02-08 22:02:40
【问题描述】:

在我正在编写的应用程序中,我下载了一个文件作为字符串。我有文件名和扩展名,所以我知道它是什么类型的文件,但它被下载为一串wingdings等。如何将其转换为正确的类型并在 Android 上打开?

我正在使用 C# 在 Xamarin 表单中编写应用程序。在 iOS 中做这件事有很大不同吗?

    async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        Publication p = (Publication)e.SelectedItem;
        Debug.WriteLine(p);
        if (p.folderID.Equals("-1"))
        {
            string resp = await post(p.docNum);
            //Downloaded file is "resp"
            //Open file here, could be pdf, etc. Filename and extension are 
            //properties of Publication "p"

        } else
        {
            await Navigation.PushAsync(new PublicationsPage(p.folderID));
        }
    }


    private async Task<String> post(string id)
    {
        Dictionary<string, string> dir = new Dictionary<string, string>();
        dir.Add("LoginID", App.user.login_id);
        dir.Add("docID", id);
        var jsonReq = JsonConvert.SerializeObject(dir);
        Debug.WriteLine("req: " + (String)jsonReq);
        var content = new StringContent(jsonReq, Encoding.UTF8, "application/json");
        var response = await client.PostAsync("urlLink.com", content);
        var responseString = await response.Content.ReadAsStringAsync();
        return responseString;
    }

【问题讨论】:

  • 与其告诉我们您已经“将文件作为字符串下载”,不如向我们展示您为此编写的实际代码?
  • I have the file name and extension, so I know what type of file it is,。但你不会告诉我们吗?有什么原因吗?
  • 如果 post() 方法是实际下载的内容,那么共享该代码是有意义的,不是吗?

标签: c# android ios xamarin xamarin.forms


【解决方案1】:

Deserialize:

var myObject = new MyType();
var ms = new MemoryStream(Encoding.UTF8.GetBytes(resp));
var ser = new DataContractJsonSerializer(myObject.GetType());
myObject = ser.ReadObject(ms) as MyType;
ms.Dispose();

超出范围:定义 MyType 并“打开”myObject。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2019-02-14
    相关资源
    最近更新 更多