嗨,我是 Windows Phone 的新手。我正在开发一个应用程序,其中有一个包含来自数据库(动态)的多个事件的日历。对于日历,我使用 wpcontrols:calendar 并更改我使用 ColorConvertor 的事件的背景颜色。我可以更改一天的背景颜色,但无法更改更多一天的颜色。要求是事件可能在任何时间点发生变化。下面是我的代码:
XAML 代码:
<phone:PhoneApplicationPage
x:Class="cal.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wpControls="clr-namespace:WPControls;assembly=WPControls"
xmlns:WpControlsExample="clr-namespace:cal"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
Loaded="PhoneApplicationPage_Loaded">
<phone:PhoneApplicationPage.Resources>
<WpControlsExample:ColorConverter x:Key="ColorConverter"/>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<wpControls:Calendar
x:Name="Cal"
ColorConverter="{StaticResource ColorConverter}"
MonthChanged="Cal_MonthChanged"
MonthChanging="Cal_MonthChanging"
SelectionChanged="Cal_SelectionChanged"
DateClicked="Cal_DateClicked"
EnableGestures="True"
/>
<!--<wpControls:Calendar
x:Name="Cal"/>-->
<Button Grid.Row="1" Content="{Binding ElementName=Cal, Path=SelectedDate}"/>
</Grid>
</Grid>
CS代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using cal.Resources;
using System.Windows.Media;
using WPControls;
using System.Windows.Media.Imaging;
namespace cal
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
this.Cal.SelectedDate = DateTime.Today;
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
}
private void Cal_MonthChanged(object sender, WPControls.MonthChangedEventArgs e)
{
MessageBox.Show("Cal_MonthChanged fired. New year is " + e.Year.ToString() + " new month is " + e.Month.ToString());
}
private void Cal_MonthChanging(object sender, WPControls.MonthChangedEventArgs e)
{
MessageBox.Show("Cal_MonthChanging fired. New year is " + e.Year.ToString() + " new month is " + e.Month.ToString());
}
private void Cal_SelectionChanged(object sender, WPControls.SelectionChangedEventArgs e)
{
MessageBox.Show("Cal_SelectionChanged fired. New date is " + e.SelectedDate.ToString());
}
private void Cal_DateClicked(object sender, WPControls.SelectionChangedEventArgs e)
{
MessageBox.Show("Cal_DateClicked fired. New date is " + e.SelectedDate.ToString());
}
}
}
ColorConverter.cs 的 CS 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using WPControls;
using System.Windows.Media.Imaging;
namespace cal
{
public class ColorConverter : IDateToBrushConverter
{
public Brush Convert(DateTime dateTime, bool isSelected, Brush defaultValue, BrushType brushType)
{
String MyString;
MyString = "2013-09-01 21:34 PM";
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",null);
if (brushType == BrushType.Background)
{
if (dateTime == new DateTime(MyDateTime.Year, MyDateTime.Month, MyDateTime.Day))
{
// Fill rectangle with an ImageBrush
//blueRectangle.Fill = imgBrush;
return new SolidColorBrush(Colors.Yellow);
}
else
{
return defaultValue;
}
}
else
{
if (dateTime == new DateTime(DateTime.Today.Year, DateTime.Today.Month, 6))
{
return new SolidColorBrush(Colors.Orange);
}
else
{
return defaultValue;
}
}
}
}
}
请帮帮我......提前谢谢