C# 触摸自定义控件日期选择器时,Windows8出现错误“检测到布局周期。布局无法完成”
我已经使用C和Xaml为日期选择器Wndows8应用程序创建了一个自定义控件。当我使用鼠标展开日期、月份或年份的组合框时,当我使用触摸屏并单击控件时,它工作正常,然后它一直崩溃,从而检测到布局周期。布局无法完成。错误我应该如何删除此错误? 自定义控件的代码: XAML代码:C# 触摸自定义控件日期选择器时,Windows8出现错误“检测到布局周期。布局无法完成”,c#,xaml,windows-8,windows-runtime,C#,Xaml,Windows 8,Windows Runtime,我已经使用C和Xaml为日期选择器Wndows8应用程序创建了一个自定义控件。当我使用鼠标展开日期、月份或年份的组合框时,当我使用触摸屏并单击控件时,它工作正常,然后它一直崩溃,从而检测到布局周期。布局无法完成。错误我应该如何删除此错误? 自定义控件的代码: XAML代码: <UserControl x:Class="Win8.MainApp.User_Control.DatePicker" xmlns="http://schemas.microsoft.com/winf
<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代码
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));
}
}
}
找到了
我在WinRT上遇到了完全相同的问题,并找到了解决方法:需要删除MaxDropDownHeight属性。一旦分配此属性,无论其值如何,都会在触摸设备上导致此问题
也尝试在代码中删除它:-.找到了
我在WinRT上遇到了完全相同的问题,并找到了解决方法:需要删除MaxDropDownHeight属性。一旦分配此属性,无论其值如何,都会在触摸设备上导致此问题
也请尝试在代码中删除它:-