从自定义消息框中读取json文件 [英] Reading json file from custom message box

查看:131
本文介绍了从自定义消息框中读取json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




当我运行构建时出现此错误无法将类型'DevotionJson.Devotion'隐式转换为'字符串'



//这是我的班级

Hi
When i ran the build i got this error "Cannot implicitly convert type 'DevotionJson.Devotion' to 'string'"

//This is my class

public class Devotion
{
    public string Date { get; set; }
    public string Title { get; set; }
    public string Verse { get; set; }

    [JsonProperty("Read Chapter")]
    public string ReadChapter { get; set; }

    [JsonProperty("Read Text")]
    public string ReadText { get; set; }

    [JsonProperty("Bible In One Year")]
    public string BibleInOneYear { get; set; }

    public string Message { get; set; }
    public string Notes { get; set; }


    public override string ToString()
    {
        string[] props = new string[8];
        props[0] = "DATE - " + Date;
        props[1] = "TITLE - " + Title;
        props[2] = "VERSE - " + Verse;
        props[3] = "READ CHAPTER - " + ReadChapter;
        props[4] = "READ TEXT - " + ReadText;
        props[5] = "BIBLE IN ONE YEAR - " + BibleInOneYear;
        props[6] = "MESSAGE - " + Message;
        props[7] = "NOTES - " + Notes;
        return String.Join("\r\n\r\n", props);
    }                     
}



//这是我的xaml.cs


//This is my xaml.cs

CustomMessageBox cmb;

private void openMsgBox(object sender, RoutedEventArgs e)
{
    // create new Stack panel
    StackPanel sp = new StackPanel() { Orientation = System.Windows.Controls.Orientation.Vertical };

    //get policy text from file
    string devotion = getDevotion("MyDevotion/Devotion1.json");

    //Create new label
    TextBlock tb = new TextBlock()
    {
        Text = devotion,
        TextWrapping = TextWrapping.Wrap,
        FontSize = 25,
    };

    //Create new Scroll viewer
    ScrollViewer sv = new ScrollViewer()
    {
        VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
        Height = 700,
    };

    // add texblock in scroll viewer
    sv.Content = tb;

    //Add the controls to stack panel
    sp.Children.Add(sv);

    cmb = new CustomMessageBox()
    {
        // Set its content
        Content = sp,
        Opacity = 0.9,

        // Left button of message box Button
        LeftButtonContent = "Back",              
    };

    //Show the message box...
    cmb.Show();
}
        
public string getDevotion(string JsonfilePath)
{
    Devotion[] d = null;

    using (StreamReader r = new StreamReader(JsonfilePath))
    {
        string json = r.ReadToEnd();
        d = JsonConvert.DeserializeObject<devotion[]>(json);
    }
    return d[0];//**Error here**
}



亲切help


Kindly help

推荐答案

你的 getDevotion 方法期望你返回一个字符串但您正在尝试返回投入

Your getDevotion method is expecting you to return a string but you are trying to return a Devotion.


这篇关于从自定义消息框中读取json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆