【问题标题】:Populating Tile From LINQ Query (Web Service)从 LINQ 查询填充磁贴(Web 服务)
【发布时间】:2015-11-19 11:18:37
【问题描述】:

我目前正在从硬编码的数据中填充我的 Windows 磁贴(我想确保我可以先让它工作......)。我所拥有的...它适用于硬编码信息。

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

我正在使用网络服务来联系我的 SQL 数据库,并将提供该服务以及我所拥有的以下信息:

网络服务

[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;
    }

【问题讨论】:

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


    【解决方案1】:

    试试这个

    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; }
        }
    
    }
    ​

    【讨论】:

    • 我想我们可能有误解...我正在尝试将我的 LINQ 查询的结果分配给变量。
    • 我尝试回答:我的问题是我不确定如何使用我的 LINQ 查询结果将它们分配给 PrimaryTile 中的变量。我添加更多代码,看看我是否可以回答问题。
    • 我没有在 PrimaryTile 中看到三个属性:标题、描述和作者。所以我不知道你想要什么。我使用“new PrimaryTile”语句创建 PrimaryTile() 并将属性设置为值。
    • 我将更新添加到一个新问题,因为它发生了很大变化。这是链接,如果您可以查看并告诉我您的想法吗? stackoverflow.com/questions/33815652/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2013-08-12
    • 2011-02-08
    • 2016-07-06
    • 1970-01-01
    • 2011-07-16
    相关资源
    最近更新 更多