我使用C#和Xaml为日期选择器和Wndows8 App创建了一个自定义控件。当我使用鼠标扩展comboBox的日期或月份或年份时,当我使用触摸屏并单击控件时工作正常,然后它一直崩溃,让我“检测到布局周期。布局无法完成。”错误。我该怎么做才能消除这个错误? 自定义控制代码:
XAML代码:
<UserControl
x:Class="Win8.MainApp.User_Control.DatePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Win8.MainApp.User_Control"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<DataTemplate x:Key="DateTemplate">
<TextBlock Text="{Binding}" FontWeight="Light" FontFamily="Segoe UI" FontSize="24" HorizontalAlignment="Left" Margin="9,2,0,0" VerticalAlignment="Center"/>
<!--<ComboBoxItem Content="{Binding}" FontWeight="Light" FontSize="24" HorizontalAlignment="Left" VerticalAlignment="Center"/>-->
</DataTemplate>
</UserControl.Resources>
<Grid Width="370">
<StackPanel Orientation="Horizontal">
<ComboBox ScrollViewer.VerticalScrollBarVisibility="Hidden" ItemTemplate="{StaticResource DateTemplate}" ItemsSource="{Binding Date.Day}" Width="100" Height="30" MaxDropDownHeight="1" SelectedItem="{Binding Day, Mode=TwoWay}"/>
<ComboBox ScrollViewer.VerticalScrollBarVisibility="Hidden" Margin="20,0,0,0" ItemTemplate="{StaticResource DateTemplate}" ItemsSource="{Binding Date.Month}" Width="100" Height="30" MaxDropDownHeight="1" SelectedItem="{Binding Month, Mode=TwoWay}" />
<ComboBox ScrollViewer.VerticalScrollBarVisibility="Hidden" Margin="20,0,0,0" ItemTemplate="{StaticResource DateTemplate}" ItemsSource="{Binding Date.Year}" Width="120" Height="30" MaxDropDownHeight="1" SelectedItem="{Binding Year, Mode=TwoWay}"/>
</StackPanel>
</Grid>
</UserControl>
.Cs Code
public sealed partial class DatePicker : UserControl, INotifyPropertyChanged
{
private int _day;
private int _month;
private int _year;
private Date _date = new Date();
public int Day
{
get { return _day; }
set
{
if (_day == value) return;
_day = value;
NotifyPropertyChanged("Day");
}
}
public int Month
{
get { return _month; }
set
{
if (_month == value) return;
_month = value;
_date.Day =
new ObservableCollection<int>((Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, _month))));
NotifyPropertyChanged("Month");
}
}
public int Year
{
get { return _year; }
set
{
if (_year == value) return;
_year = value;
_date.Day = new ObservableCollection<int>((Enumerable.Range(1, DateTime.DaysInMonth(_year, _month))));
NotifyPropertyChanged("Year");
}
}
public Date Date
{
get { return _date; }
}
private int GetDaysInMonth(int year, int month)
{
return DateTime.DaysInMonth(year, month);
}
public DatePicker()
{
this.InitializeComponent();
SetTodaysDate();
SetDataSource();
}
private void SetTodaysDate()
{
Day = DateTime.Now.Day;
Month = DateTime.Now.Month;
Year = DateTime.Now.Year;
}
private void SetDataSource()
{
int year = DateTime.Now.Year - 10;
_date.Day = new ObservableCollection<int>(Enumerable.Range(1, GetDaysInMonth(Year, Month)));
_date.Month = new ObservableCollection<int>(Enumerable.Range(1, 12));
_date.Year = new ObservableCollection<int>(Enumerable.Range(year, 20));
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
public class Date : INotifyPropertyChanged
{
private ObservableCollection<int> _day;
private ObservableCollection<int> _month;
private ObservableCollection<int> _year;
public ObservableCollection<int> Day
{
get { return _day; }
set
{
if (_day == value) return;
_day = value;
NotifyPropertyChanged("Day");
}
}
public ObservableCollection<int> Month
{
get { return _month; }
set
{
if (_month == value) return;
_month = value;
NotifyPropertyChanged("Month");
}
}
public ObservableCollection<int> Year
{
get { return _year; }
set
{
if (_year == value) return;
_year = value;
NotifyPropertyChanged("Year");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
答案 0 :(得分:0)
找到!
我在WinRT上遇到了完全相同的问题并找到了解决方法:需要删除“MaxDropDownHeight”属性。一旦分配了此属性,无论该值如何,都会在触摸设备上引起此问题。
尝试在代码中删除它: - )。