从LINQ查询(Web服务)填充平铺

时间:2015-11-19 02:31:01

标签: c# xml linq win-universal-app

我目前正在从硬编码数据填充我的Windows磁贴(我想确保我可以先让它工作......)。我拥有的......它与硬编码信息相得益彰。

我想要尝试做的是从LINQ查询中提取信息并将它们放入几个变量中,然后将这些变量放入Tile中。

我正在使用Web服务联系我的SQL DB,并将提供以下内容:

网络服务

[OperationContract]
List<TBL_My_Info> FindInfo(string uid);

public List<TBL_My_Info> FindInfo(string uid)
{
    DataClasses1DataContext context = new DataClasses1DataContext();
    var res = from r in context.TBL_My_Info where r.User_Name == uid select r;
    return res.ToList();
}

我可以验证查询是否包含了我需要的3条信息,即TitleDescriptionAuthor。我遇到的问题是将这3个部分分配给他们各自的变量,以便我可以在我的瓷砖中显示它们。

我的问题是我不知道如何使用LINQ查询结果将它们分配给PrimaryTile中的变量..

这就是我所拥有的,我已经硬编码了瓷砖的值,以确保它正确显示:

private void UpdatePrimaryTile(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        var xmlDoc = TileService.CreateTiles(new PrimaryTile());
        var updater = TileUpdateManager.CreateTileUpdaterForApplication();
        TileNotification notification = new TileNotification(xmlDoc);
        updater.Update(notification);
    }


public class PrimaryTile
{
    public string time { get; set; } = "8:15 AM, Saturday";
    public string message { get; set; } = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.";
    public string message2 { get; set; } = " At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident.";
    public string branding { get; set; } = "name";
    public string appName { get; set; } = "HoL";
}


public static Windows.Data.Xml.Dom.XmlDocument CreateTiles(PrimaryTile primaryTile)
    {
        XDocument xDoc = new XDocument(
            new XElement("tile", new XAttribute("version", 3),
                new XElement("visual",
                    // Small Tile 
                    new XElement("binding", new XAttribute("branding",
                    primaryTile.branding), new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileSmall"),
                        new XElement("group",
                            new XElement("subgroup",
                                new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),
                                    new XElement("text", primaryTile.message, new
                                    XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true), new XAttribute("hint-maxLines", 3))
                                )
                            )
                        ),
                        // Medium Tile 
                        new XElement("binding", new XAttribute("branding", primaryTile.branding), 
                            new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileMedium"),
                                new XElement("group", new XElement("subgroup", 
                                    new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),
                                        new XElement("text", primaryTile.message, new XAttribute("hint-style", "captionsubtle"), 
                                            new XAttribute("hint-wrap", true), new XAttribute("hint-maxLines", 3))
                                   )
                              )
                         )
                    )
                )
            );
        Windows.Data.Xml.Dom.XmlDocument xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
        xmlDoc.LoadXml(xDoc.ToString());
        return xmlDoc;
    }

1 个答案:

答案 0 :(得分:0)

试试这个

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PrimaryTile pt = new PrimaryTile() {
                time = DateTime.Parse("11/21/2015 8:15 AM"),
                message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.",
                message2 = " At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident.",
                branding = "name",
                appName = "HoL"
            };
            XmlDocument doc = CreateTiles(pt);

        }
        public static XmlDocument CreateTiles(PrimaryTile primaryTile)
        {
            XDocument xDoc = new XDocument(
                new XElement("tile", new XAttribute("version", 3),
                    new XElement("visual",
                // Small Tile 
                        new XElement("binding", new XAttribute("branding",
                        primaryTile.branding), new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileSmall"),
                            new XElement("group",
                                new XElement("subgroup",
                                    new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),
                                        new XElement("text", primaryTile.message, new
                                        XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true), new XAttribute("hint-maxLines", 3))
                                    )
                                )
                            ),
                // Medium Tile 
                            new XElement("binding", new XAttribute("branding", primaryTile.branding),
                                new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileMedium"),
                                    new XElement("group", new XElement("subgroup",
                                        new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),
                                            new XElement("text", primaryTile.message, new XAttribute("hint-style", "captionsubtle"),
                                                new XAttribute("hint-wrap", true), new XAttribute("hint-maxLines", 3))
                                       )
                                  )
                             )
                        )
                    )
                );
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xDoc.ToString());
            return xmlDoc;
        }

    }
    public class PrimaryTile
    {
        public DateTime time { get; set; } 
        public string message { get; set; }
        public string message2 { get; set; }
        public string branding { get; set; }
        public string appName { get; set; }
    }

}
​
&#13;
&#13;
&#13;