c# - 从 Azure 中提取显示在 MainPage 和 ItemPage 上的数据

标签 c# xaml azure mobile windows-phone-8

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

主页代码:

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; 时出现问题。 应用程序退出且没有任何错误代码。

有什么想法吗?

最佳答案

该问题是由于 StackPanel 的高度设置为“*”造成的。去掉之后,问题就解决了。

关于c# - 从 Azure 中提取显示在 MainPage 和 ItemPage 上的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16995375/

相关文章:

c# - Json.net 反序列化列表给出重复项

c# - ADO.NET 数据库保存,但不保存

使用 DataTemplate 的 ListBox 的 WPF 双向绑定(bind)

php - 使用 PHP 后端在通知中心注册

azure - 尝试使用 PySpark 从 ADLS Gen2 读取 csv 时,没有方案 "az"的文件系统错误

c# - 检测正则表达式后的单词

C# 只读与获取

xaml - 枢轴项目不垂直滚动

c# - ListView 绑定(bind)中的 SelectedItem

azure - 从 React 应用程序访问 Azure AD 的 ROPC 端点时出现 CORS 问题