[UWP]如何在同一页面上显示多个集合 [英] [UWP]How to dispaly multiple collections on the same page

查看:69
本文介绍了[UWP]如何在同一页面上显示多个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我只是在使用MVVM模型学习UWP,并且在同一页面上尝试显示多个观察收集时遇到了问题。 我正在使用VS2017。  这可能是XAML代码:

Hi everyone I am just learning UWP using the MVVM model and have run into a problem trying to display more than one observerablecollection on the same page.  I am using VS2017.   Here is may XAML code:

<Page
    x:Name="pageRoot"
    x:Class="Invaders.View.InvadersPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="using:Invaders.View"
    xmlns:viewmodel="using:Invaders.ViewModel"
    xmlns:common="using:Invaders.Common"
    SizeChanged="pageRoot_SizeChanged"
    ManipulationMode="TranslateX"
    ManipulationCompleted="pageRoot_ManipulationCompleted"
    ManipulationDelta="pageRoot_ManipulationDelta"
    Tapped= "pageRoot_Tapped"
    mc:Ignorable="d">
    <Page.Resources>
        <viewmodel:InvadersViewModel x:Name="viewModel"/>
    </Page.Resources>
    <Grid Background="#FF232222" DataContext="{StaticResource viewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="6*"/>
        </Grid.RowDefinitions>

        <StackPanel  HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal">
            <TextBlock Margin="10,10,5,5" Foreground="#FFCBBFBF">Score:</TextBlock>
            <TextBlock  Margin="5,10,10,5" Text="{Binding Score }" Foreground="#FFCBBFBF"/>
            <GridView  ItemsSource="{Binding Lives}">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Image Source="ms-appx:///Assets/player.png" Stretch="Fill" Height="15" Width="20" />
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </StackPanel>

        <Border Grid.Row="1" x:Name="playArea" BorderBrush="Blue" BorderThickness="2" CornerRadius="10" Background="Black" Margin="5" Loaded="playArea_Loaded">
            <ItemsControl ItemsSource="{Binding Path=Sprites}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Border>
    </Grid>
</Page>


以上代码生成错误消息" ArgumentException:Value不在预期范围内。" 
这对我来说并不多。 &NBSP;&NBSP;

现在,如果我删除了GRIDVIEW代码块或ITEMSCONTROL代码块,则错误消失并且程序正确运行(减去已删除的集合)。 &NBSP;&NBSP;此外,如果我将Bindings更改为代码
的任何一个块到一个虚假属性,则错误消息消失并且程序正确运行(减去输入伪造属性名称的集合数据)。 换句话说,只要我没有尝试在同一
时间绑定到两个集合,代码就可以正常工作。  

The above code produces the error message "ArgumentException: Value does not fall within the expected range."  which doesn't tell me much.   

Now, if I remove either the GRIDVIEW block of code or the ITEMSCONTROL block of code, the error goes away and the program runs correctly (minus the removed collection).    Furthermore, if I change the Bindings to either of theses blocks of code to a bogus property the error message goes away and the program runs correctly (minus the collection data where the bogus property name was entered).  In other words, the code works fine as long as I am not trying to bind to two collections at the same time.  

我无法弄清楚如何将两个不同的集合绑定到同一页面上的两个不同控件。 帮助!

I cant figure out how to bind two different collections to two different controls on the same page.  HELP!

推荐答案

嗨  JustLearningWPF,

Hi JustLearningWPF,

欢迎
到开发通用Windows应用论坛
!请使用  标记 
发布到此论坛时,谢谢!

我在我身边测试你的XMAL代码使用临时ViewModel,它工作正常。您能否发布更多有关InvadersViewModel和您的模型的代码,以帮助我们找出您的错误?

I test your XMAL code in my side with a temporary ViewModel, it works fine. Could you please post more code about your InvadersViewModel and your model to help us find out your error?

祝您好运,

Breeze


这篇关于[UWP]如何在同一页面上显示多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆