繁体   English   中英

按 DateTime.now 显示 - Silverlight WP7

[英]Displaying by DateTime.now - Silverlight WP7

好的,所以我想显示一些属性,但前提是它们的开始时间是今天的日期。

有没有办法使用通配符来获得价值? 或在 Where 子句中列出多个值?

这是一个示例。我在想是否可以拉出当前时间然后转换为字符串( var time = DateTime.Now.ToString("yyyyMMddHHmmss");) 我可以使用该 var 作为属性的过滤器。 这里... tv.Attribute("start").Value == time

编辑* *

我已经更新了代码,以使其更清楚我所追求的。 正如您在下面看到的,我使用 Where 子句来显示与特定“开始”时间相关的属性。

现在这个例子对我不起作用,因为首先它需要是 24 小时内的所有列表,而不是下面的特定时间,还因为我想通过 DateTime.Now 动态显示列表。

所以,我真正需要的是一种显示当天的方法,而不是特定时间作为布尔值,并在我的 boolean 方程中使用它来显示当天的“开始”属性。

            WebClient c = new WebClient();
            c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
            c.DownloadStringAsync(new Uri("http://www.domain.com/source.xml"));
            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;


        } 


        void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;


            var r = XDocument.Parse(e.Result);


            listBox2.ItemsSource = from tv in r.Root.Descendants("programme")
                                   where tv.Attribute("start").Value == "20110724190000 +1200"
                                   let channelE1 = tv.Attribute("channel")
                                   let startE1 = tv.Attribute("start")
                                   let nameEl = tv.Element("title")
                                   orderby tv.Attribute("start").Value ascending
                                   let urlEl = tv.Element("desc")


                                   select new TV1guide



                                   {
                                       DisplayName = nameEl == null ? null : nameEl.Value,
                                       ChannelName = channelE1 == null ? null : channelE1.Value,
                                       ChannelURL = urlEl == null ? null : urlEl.Value,
                                       StartTime = startE1 == null ? (DateTime?)null : DateTime.ParseExact(startE1.Value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeLocal),



                                   };

            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;

public class TV1guide
        {
            public string DisplayName { get; set; }
            public string ChannelURL { get; set; }
            public string ImageSource { get; set; }
            public DateTime? StartTime { get; set; }
            public DateTime? EndTime { get; set; }
            public string ChannelName { get; set; }

        }

    }

}

除此之外,我还尝试了 HiTech Magic 的建议,但我很确定语法是错误的。

                WebClient c = new WebClient();
                c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
                c.DownloadStringAsync(new Uri("http://www.domain.com/source.xml"));
                progressBar1.IsIndeterminate = true;
                progressBar1.Visibility = Visibility.Visible;


            } 

bool MyDateCheckingMethod( DateTime otherDate )
{
    // Is this today (ignoring time)?
    return otherDate.Date == DateTime.Now.Date;
}

void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                if (e.Error != null)
                    return;


                var r = XDocument.Parse(e.Result);


                listBox2.ItemsSource = from tv in r.Root.Descendants("programme")
                                       where MyDateCheckingMethod(tv.Attribute("start").Value)
                                       let channelE1 = tv.Attribute("channel")
                                       let startE1 = tv.Attribute("start")
                                       let nameEl = tv.Element("title")
                                       orderby tv.Attribute("start").Value ascending
                                       let urlEl = tv.Element("desc")


                                       select new TV1guide



                                       {
                                           DisplayName = nameEl == null ? null : nameEl.Value,
                                           ChannelName = channelE1 == null ? null : channelE1.Value,
                                           ChannelURL = urlEl == null ? null : urlEl.Value,
                                           StartTime = startE1 == null ? (DateTime?)null : DateTime.ParseExact(startE1.Value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeLocal),



                                       };

                progressBar1.IsIndeterminate = false;
                progressBar1.Visibility = Visibility.Collapsed;

    public class TV1guide
            {
                public string DisplayName { get; set; }
                public string ChannelURL { get; set; }
                public string ImageSource { get; set; }
                public DateTime? StartTime { get; set; }
                public DateTime? EndTime { get; set; }
                public string ChannelName { get; set; }

            }

        }

    }

LINQ 与所有 C# 功能配合得很好,所以它可以很简单:

where MyListOfDates.Contains(tv.Attribute("start").Value)

并将所有必需的日期放入List<DateTime> MyListOfDates

如果您想使用其他比较而不仅仅是基本列表,请将逻辑放入 Method 并调用它,例如:

where MyDateCheckingMethod(tv.Attribute("start").Value)

您从 XML 解析中获得的“日期/时间”值实际上是“yyyyMMddHHmmss K”格式的字符串,因此您的方法可能如下所示:

bool MyDateCheckingMethod(string dateString)
{
    DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
    // Is this today (ignoring time)?
    return otherDate.Date == DateTime.Now.Date;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM