1

我目前将应用程序的数据存储在 Azure 移动服务 SQL 数据库中。我正在从数据库中提取项目并在列表视图中显示它们。当用户单击列表视图中的项目时,他们将导航到一个新页面,该页面显示有关数据库中特定记录的更多详细信息。

主页代码:

public class OSVersions
{
    [JsonProperty(PropertyName = "id")]
    public int id { get; set; }

    [JsonProperty(PropertyName = "Version")]
    public string Version { get; set; }

    [JsonProperty(PropertyName = "Codename")]
    public string Codename { get; set; }

    [JsonProperty(PropertyName = "Publish")]
    public bool Publish { get; set; }

    [JsonProperty(PropertyName = "ReleaseDate")]
    public DateTime ReleaseDate { get; set; }

    [JsonProperty(PropertyName = "Changes")]
    public string Changes { get; set; }

    [JsonProperty(PropertyName = "Notes")]
    public string Notes { get; set; }
}

public partial class OSMainVIew : PhoneApplicationPage
{
    private MobileServiceCollection<OSVersions, OSVersions> items;
    private IMobileServiceTable<OSVersions> osTable =
        App.MobileService.GetTable<OSVersions>();

    public OSMainVIew()
    {
        InitializeComponent();
    }

    private async void RefreshOSItems()
    {
        progressBar1.IsEnabled = true;
        progressBar1.IsIndeterminate = true;

        items = await osTable
            .Where(OSItem => OSItem.Publish == true)
            .ToCollectionAsync();

        MainListBox.ItemsSource = items;

        progressBar1.IsEnabled = false;
        progressBar1.Visibility = System.Windows.Visibility.Collapsed;
        progressBar1.IsIndeterminate = false;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        RefreshOSItems();
    } 

    private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (MainListBox.SelectedIndex == -1)
            return;

        NavigationService.Navigate(new Uri("/ViewModels/OS/OSItemView.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

        MainListBox.SelectedIndex = -1;
    }
}

项目页面代码:

public partial class OSItemView : PhoneApplicationPage
{
    private MobileServiceCollection<OSVersions, OSVersions> items;
    private IMobileServiceTable<OSVersions> osTable =
        App.MobileService.GetTable<OSVersions>();

    public OSItemView()
    {
        InitializeComponent();

        if ((Application.Current as App).IsTrial)
        {
            //textBlock1.Text = "Change Log available in full version only!";
            //textBlock2.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        string selectedIndex = "";
        int buildID;
        int idValue;

        if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
        {
            //Start progressBar
            progressBar1.IsEnabled = true;
            progressBar1.IsIndeterminate = true;

            //Convert selectedIndex -> buildID
            idValue = Convert.ToInt32(selectedIndex);
            buildID = idValue + 1;

            /* buildID = idValue + 1 becuase on OSMainView
             * Items stored in the ListBox are each even an index number
             * The first number is '0'
             * This is a problem because the first IDNumber in the Database is '1'
             * This isn't the best way to handle this, becuase even though the id field is an auto-increamental field,
             * sometimes values are skipped and rows are deleted.
             */ 

            //Query database
            items = await osTable
                .Where(OSItem => OSItem.id == buildID)
                .ToCollectionAsync();

            MainListBox.ItemsSource = items;

            //End progressBar
            progressBar1.IsEnabled = false;
            progressBar1.Visibility = System.Windows.Visibility.Collapsed;
            progressBar1.IsIndeterminate = false;
        }
    }
}

项目页面 XAML 代码:

<Grid x:Name="ContentPanel" Margin="10,97,12,0" Grid.RowSpan="2">
        <ListBox x:Name="MainListBox" Margin="10,-35,-12,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,0" Width="432" Height="*">
                        <TextBlock TextWrapping="Wrap" Text="{Binding Version}" Style="    {StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="Notes" FontFamily="Segoe WP Bold" Foreground="{StaticResource PhoneAccentBrush}" Height="30" HorizontalAlignment="Left" Margin="0,1,0,0" Name="textBlock3" Padding="0" VerticalAlignment="Top" Width="444" />
                        <TextBlock x:Name="notesText" Text="{Binding Notes}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Height="180" VerticalAlignment="Top" HorizontalAlignment="Stretch" FontFamily="Segoe WP SemiLight" Margin="0" Width="455"/>
                        <TextBlock Text="Change Log" Height="30" HorizontalAlignment="Left" Margin="0,222,0,0" Name="textBlock1"  VerticalAlignment="Top" FontFamily="Segoe WP Bold" Foreground="{StaticResource PhoneAccentBrush}" Width="444" Padding="0" />
                        <TextBlock Name="textBlock2" Text="{Binding Changes}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" FontFamily="Segoe WP SemiLight" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

我的问题发生在MainListBox.ItemsSource = items;执行时。应用程序退出,没有任何错误代码。

有任何想法吗?

4

1 回答 1

1

问题必须归因于 StackPanel 的高度设置为“*”。删除之后,问题就解决了。

于 2013-06-11T01:26:45.020 回答