当我尝试在 Windows phone 8 页面上更改方向时遇到以下异常,该页面包含一个 longlistselector 控件,列表中有多个项目(列表中只有一个项目可以正常工作)并且似乎无法找到可行的解决方法/解决方案(通过找到可行的解决方法,我不想关闭改变方向的能力)。在 OnOrientationChanged 事件触发之前引发异常,实际上 OnOrientationChanged 事件永远不会触发。
以下异常被抛出两次(我假设 longlistselector 中的每个项目都是一次,但是当我在 longlistselector 中有两个以上项目时,它继续只抛出异常两次):
System.ArgumentException: Value does not fall within the expected range.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_UpdateLayout(UIElement element)
at Microsoft.Phone.Controls.PhoneApplicationFrame.UpdateOrientationTransform()
at Microsoft.Phone.Controls.PhoneApplicationFrame.System.Windows.Controls.IFrame.InternalOnBeginLayoutChanged(Object sender, OrientationChangedEventArgs args)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at System.Windows.Controls.Frame.FireEventHandler[T](EventHandler`1 handler, Object sender, T args)
at Microsoft.Phone.Controls.PhoneApplicationPage.set_LayoutOrientation(PageOrientation value)
at Microsoft.Phone.Controls.PhoneApplicationPage.OnOrientationChanged(OrientationChangedEventArgs e)
at Microsoft.Phone.Controls.PhoneApplicationPage.UIOrientationChange(Orientations orientation)
at Microsoft.Phone.Controls.PhoneApplicationPage.Microsoft.Phone.Controls.IPhoneApplicationPage.InternalOnUIOrientationChange(Orientations orientation)
at System.Windows.Navigation.Journal.OrientationChanged(Orientations orientation)
at Microsoft.Phone.TaskModel.Interop.Task.FireOnOrientationChanged(Int32 orientation)}
我已经创建了一个简单的项目,我很乐意通过电子邮件将它发送给任何想要查看它的人,特别是我所看到的......它只是打开主页并将一些项目(复选框)加载到 longlistselector 控件中。要重复该问题,只需在 longlistselector 中运行具有 2 个或更多项目的应用程序并尝试更改方向。
以下是 MainPage.xaml 的副本:
<phone:PhoneApplicationPage
x:Class="PhoneApp2.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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--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>
<!-- LOCALIZATION NOTE:
To localize the displayed strings copy their values to appropriately named
keys in the app's neutral language resource file (AppResources.resx) then
replace the hard-coded text value between the attributes' quotation marks
with the binding clause whose path points to that string name.
For example:
Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
This binding points to the template's string resource named "ApplicationTitle".
Adding supported languages in the Project Properties tab will create a
new resx file per language that can carry the translated values of your
UI strings. The binding in these examples will cause the value of the
attributes to be drawn from the .resx file that matches the
CurrentUICulture of the app at run time.
-->
<!--TitlePanel contains the name of the application and page title-->
<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>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector x:Name="LongList"/>
</Grid>
<!--Uncomment to see an alignment grid to help ensure your controls are
aligned on common boundaries. The image has a top margin of -32px to
account for the System Tray. Set this to 0 (or remove the margin altogether)
if the System Tray is hidden.
Before shipping remove this XAML and the image itself.-->
<!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>
</phone:PhoneApplicationPage>
以下是后面的代码副本:
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 PhoneApp2.Resources;
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private void LoadPageWithData()
{
LongList.ItemsSource = null;
System.Collections.Generic.List<CheckBox> Items = new List<CheckBox>();
//fetch planes from the data base and load onto the wnd
for (int j = 0; j < 2; j++)
{
CheckBox _CB = new CheckBox();
_CB.Content = "Plane" + j;
_CB.Tag = j;
Items.Add(_CB);
}
LongList.ItemsSource = Items;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
LoadPageWithData();
}
}
}