求助,为什么wpf(c#)不能步进调试部分类中的代码,vb可以
如题,我从网上下了个vb的wpf代码,改写成c#版本,但是调试中发现有一个usercontrol无易做图常实例化,原因是有一个grid组件没有被初始化,一直是null,进而在.clear()操作中报错。在vb中调试可以看到grid是在g.vb中的loadcomponent之后被赋值的,而c#中在g.cs中下的断点无效,无法知道到底是哪里出了问题!请问应该怎么改配置,还是c#本身就无法调试g.cs部分代码。另外grid==null可能是有哪些原因?谢谢! --------------------编程问答-------------------- monthview.xaml.cs
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Collections.Generic;
using Microsoft.VisualBasic;
namespace QuickWPFMonthCalendar
{
public partial class MonthView : UserControl
{
internal System.DateTime _DisplayStartDate = System.DateTime.Now.AddDays(-1 * (System.DateTime.Now.Day - 1));
/*
private int _DisplayMonth = _DisplayStartDate.Month;
private int _DisplayYear = _DisplayStartDate.Year;
private CultureInfo _cultureInfo = new CultureInfo(CultureInfo.CurrentUICulture.LCID);
private Calendar sysCal = _cultureInfo.Calendar();
private List<Appointment> _monthAppointments;
*/
private int _DisplayMonth;
private int _DisplayYear;
private CultureInfo _cultureInfo;
private Calendar sysCal;
private List<Appointment> _monthAppointments;
public event DisplayMonthChangedEventHandler DisplayMonthChanged;
public delegate void DisplayMonthChangedEventHandler(MonthChangedEventArgs e);
public event DayBoxDoubleClickedEventHandler DayBoxDoubleClicked;
public delegate void DayBoxDoubleClickedEventHandler(NewAppointmentEventArgs e);
public event AppointmentDblClickedEventHandler AppointmentDblClicked;
public delegate void AppointmentDblClickedEventHandler(int Appointment_Id);
public System.DateTime DisplayStartDate
{
get { return _DisplayStartDate; }
set
{
_DisplayStartDate = value;
_DisplayMonth = _DisplayStartDate.Month;
_DisplayYear = _DisplayStartDate.Year;
}
}
internal List<Appointment> MonthAppointments
{
get { return _monthAppointments; }
set
{
_monthAppointments = value;
BuildCalendarUI();
}
}
public MonthView()
{
//InitializeComponent();
_DisplayMonth = _DisplayStartDate.Month;
_DisplayYear = _DisplayStartDate.Year;
//_cultureInfo = new CultureInfo(CultureInfo.CurrentUICulture.LCID);
sysCal = new CultureInfo(CultureInfo.CurrentUICulture.LCID).Calendar;
if (_monthAppointments == null) BuildCalendarUI();
}
private void BuildCalendarUI()
{
int iDaysInMonth = sysCal.GetDaysInMonth(_DisplayStartDate.Year, _DisplayStartDate.Month);
int iOffsetDays = (int)System.Enum.ToObject(typeof(System.DayOfWeek), _DisplayStartDate.DayOfWeek);
int iWeekCount = 0;
WeekOfDaysControls weekRowCtrl = new WeekOfDaysControls();
MonthViewGrid.Children.Clear();
AddRowsToMonthGrid(iDaysInMonth, iOffsetDays);
//MonthYearLabel.Content = Microsoft.VisualBasic.MonthName(_DisplayMonth) + " " + _DisplayYear;
MonthYearLabel.Content = "June" + " " + _DisplayYear;
for (int i = 1; i <= iDaysInMonth; i++)
{
if ((i != 1) && System.Math.IEEERemainder((i + iOffsetDays - 1), 7) == 0)
{
//-- add existing weekrowcontrol to the monthgrid
Grid.SetRow(weekRowCtrl, iWeekCount);
MonthViewGrid.Children.Add(weekRowCtrl);
//-- make a new weekrowcontrol
weekRowCtrl = new WeekOfDaysControls();
iWeekCount += 1;
}
//-- load each weekrow with a DayBoxControl whose label is set to day number
DayBoxControl dayBox = new DayBoxControl();
dayBox.DayNumberLabel.Content = i.ToString();
dayBox.Tag = i;
dayBox.MouseDoubleClick += DayBox_DoubleClick;
//-- customize daybox for today:
if ((new System.DateTime(_DisplayYear, _DisplayMonth, i)) == System.DateTime.Today)
{
dayBox.DayLabelRowBorder.Background = (Brush)dayBox.TryFindResource("OrangeGradientBrush");
dayBox.DayAppointmentsStack.Background = Brushes.Wheat;
}
//-- for design mode, add appointments to random days for show...
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
if ((new System.Random()).Next(1) < 0.25)
{
DayBoxAppointmentControl apt = new DayBoxAppointmentControl();
apt.DisplayText.Text = "Apt on " + i + "th";
dayBox.DayAppointmentsStack.Children.Add(apt);
}
}
else if (_monthAppointments != null)
{
//-- Compiler warning about unpredictable results if using i (the iterator) in lambda, the
// "hint" suggests declaring another var and set equal to iterator var
int iday = i;
List<Appointment> aptInDay = _monthAppointments.FindAll(new System.Predicate<Appointment>((Appointment apt) => ((System.DateTime)apt.StartTime).Day == iday));
foreach (Appointment a in aptInDay)
{
DayBoxAppointmentControl apt = new DayBoxAppointmentControl();
apt.DisplayText.Text = a.Subject;
apt.Tag = a.AppointmentID;
apt.MouseDoubleClick += Appointment_DoubleClick;
dayBox.DayAppointmentsStack.Children.Add(apt);
}
}
Grid.SetColumn(dayBox, (i - (iWeekCount * 7)) + iOffsetDays);
weekRowCtrl.WeekRowGrid.Children.Add(dayBox);
}
Grid.SetRow(weekRowCtrl, iWeekCount);
MonthViewGrid.Children.Add(weekRowCtrl);
}
private void AddRowsToMonthGrid(int DaysInMonth, int OffSetDays)
{
MonthViewGrid.RowDefinitions.Clear();
System.Windows.GridLength rowHeight = new System.Windows.GridLength(60, System.Windows.GridUnitType.Star);
int EndOffSetDays = 7 - ((int)System.Enum.ToObject(typeof(System.DayOfWeek), _DisplayStartDate.AddDays(DaysInMonth - 1).DayOfWeek) + 1);
for (int i = 1; i <= (int)(DaysInMonth + OffSetDays + EndOffSetDays) / 7; i++)
{
var rowDef = new RowDefinition();
rowDef.Height = rowHeight;
MonthViewGrid.RowDefinitions.Add(rowDef);
}
}
private void UpdateMonth(int MonthsToAdd)
{
MonthChangedEventArgs ev = new MonthChangedEventArgs();
ev.OldDisplayStartDate = _DisplayStartDate;
this.DisplayStartDate = _DisplayStartDate.AddMonths(MonthsToAdd);
ev.NewDisplayStartDate = _DisplayStartDate;
if (DisplayMonthChanged != null)
{
DisplayMonthChanged(ev);
}
}
#region " UI Event Handlers "
private void MonthGoPrev_MouseLeftButtonUp(System.Object sender, MouseButtonEventArgs e)
{
UpdateMonth(-1);
}
private void MonthGoNext_MouseLeftButtonUp(System.Object sender, MouseButtonEventArgs e)
{
UpdateMonth(1);
}
private void Appointment_DoubleClick(object sender, MouseButtonEventArgs e)
{
if (object.ReferenceEquals(e.Source.GetType(), typeof(DayBoxAppointmentControl)))
{
if (((DayBoxAppointmentControl)e.Source).Tag != null)
{
//-- You could put your own call to your appointment-displaying code or whatever here..
if (AppointmentDblClicked != null)
{
AppointmentDblClicked((int)((DayBoxAppointmentControl)e.Source).Tag);
}
}
e.Handled = true;
}
}
private void DayBox_DoubleClick(object sender, MouseButtonEventArgs e)
{
//-- call to FindVisualAncestor to make sure they didn't click on existing appointment (in which case,
// that appointment window is already opened by handler Appointment_DoubleClick)
if (object.ReferenceEquals(e.Source.GetType(), typeof(DayBoxControl)))
//if (object.ReferenceEquals(e.Source.GetType(), typeof(DayBoxControl)) && Utilities.FindVisualAncestor(typeof(DayBoxAppointmentControl), e.OriginalSource) == null)
{
NewAppointmentEventArgs ev = new NewAppointmentEventArgs();
if (((DayBoxControl)e.Source).Tag != null)
{
ev.StartDate = new System.DateTime(_DisplayYear, _DisplayMonth, (int)((DayBoxControl)e.Source).Tag, 10, 0, 0);
ev.EndDate = ((System.DateTime)ev.StartDate).AddHours(2);
}
if (DayBoxDoubleClicked != null)
{
DayBoxDoubleClicked(ev);
}
e.Handled = true;
}
}
#endregion
}
public struct MonthChangedEventArgs
{
public System.DateTime OldDisplayStartDate;
public System.DateTime NewDisplayStartDate;
}
public struct NewAppointmentEventArgs
{
public System.DateTime? StartDate;
public System.DateTime? EndDate;
public int? CandidateId;
public int? RequirementId;
}
} --------------------编程问答-------------------- MonthView.xaml
<UserControl x:Class="QuickWPFMonthCalendar.MonthView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Grid VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Background="AliceBlue">
<Image x:Name="MonthGoPrev" Source="/Images/ForwardGreen.png" Height="24" RenderTransformOrigin="0.5,0.5" Margin="20,0,6,0"
MouseLeftButtonUp="MonthGoPrev_MouseLeftButtonUp">
<Image.RenderTransform>
<RotateTransform Angle="180"/>
</Image.RenderTransform>
</Image>
<Image Name="MonthGoNext" Source="/Images/ForwardGreen.png" Height="24" Margin="6,0,6,0"
MouseLeftButtonUp="MonthGoNext_MouseLeftButtonUp"/>
<Label x:Name="MonthYearLabel" Content="March 2009" FontSize="14" FontFamily="Arial" VerticalAlignment="Center"/>
</StackPanel>
<Grid Grid.Row="1" Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="1" Content="Sunday" FontSize="9" Margin="2,0,0,2" Padding="0,1,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,1,0"/>
<Label Grid.Column="2" Content="Monday" FontSize="9" Margin="2,0,0,2" Padding="0,1,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,1,0"/>
<Label Grid.Column="3" Content="Tuesday" FontSize="9" Margin="2,0,0,2" Padding="0,1,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,1,0"/>
<Label Grid.Column="4" Content="Wednesday" FontSize="9" Margin="2,0,0,2" Padding="0,1,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,1,0"/>
<Label Grid.Column="5" Content="Thursday" FontSize="9" Margin="2,0,0,2" Padding="0,1,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,1,0"/>
<Label Grid.Column="6" Content="Friday" FontSize="9" Margin="2,0,0,2" Padding="0,1,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,1,0"/>
<Label Grid.Column="7" Content="Saturday" FontSize="9" Margin="2,0,0,2" Padding="0,1,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<!--<StackPanel x:Name="MonthViewStack" Grid.Row="2">
</StackPanel>-->
<Grid x:Name="MonthViewGrid" Grid.Row="2">
<Grid.RowDefinitions>
</Grid.RowDefinitions>
</Grid>
</Grid>
</UserControl>
--------------------编程问答-------------------- 好长的代码,慢慢看一下。 --------------------编程问答-------------------- Appointment是什么类 --------------------编程问答-------------------- 代码有点长,得好好看看! --------------------编程问答-------------------- 这样贴代码看着真费劲 --------------------编程问答-------------------- 有点糊涂,帮顶· --------------------编程问答-------------------- 我也在改这个代码,楼主的目的,与我的一样。
但我没有遇到楼主这样的问题,我现在已经完成了一部分,还有“约会”还没完成。
补充:.NET技术 , C#