值不在预期范围内- Windows 8.1应用程序

本文关键字:Windows 应用程序 范围内 | 更新日期: 2023-09-27 18:04:35

放置在我得到异常的代码中:

EventHandler(this, new ObservableDictionaryChangedEventArgs(change, key));

 namespace TestGridApp
 {
    public sealed partial class ItemDetailPage : Page
    {
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }
        public ItemDetailPage()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
        }
        private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            var mitem = await MovieDataSource.GetItemAsync((String)e.NavigationParameter);
            //this.DefaultViewModel["Item"] = mitem;
            ItemDetailGridView.ItemsSource = mitem;
        }
        #region NavigationHelper registration
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
        }
        #endregion
    }
}
Xaml:

<Page
    x:Name="pageRoot"
    x:Class="TestGridApp.ItemDetailPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestGridApp"
    xmlns:data="using:TestGridApp.Data"
    xmlns:common="using:TestGridApp.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!--
            TODO: Content should be placed within the following grid 
                  to show details for the current item
        -->
        <Grid Grid.Row="1" x:Name="contentRegion">
            <GridView FlowDirection="LeftToRight" ItemsSource="{Binding Item}" x:Name="ItemDetailGridView">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Grid Height="110" Width="480" Margin="10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0" Orientation="Vertical">
                                <TextBlock Text="{Binding OverView}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap"/>
                                <StackPanel>
                                    <TextBlock Text="{Binding CreatedBy}" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap"/>
                                    <TextBlock Text="{Binding EpisodeRunTime}" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap"/>
                                    <TextBlock Text="{Binding homepage}" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap"/>
                                </StackPanel>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>
                <GridView.Header>
                    <StackPanel Width="600" Margin="40,4,14,0">
                        <TextBlock Text="{Binding Title}" Margin="0,0,0,20" Style="{StaticResource SubheaderTextBlockStyle}" MaxHeight="60"/>
                        <Image Source="{Binding ImagePath}" Height="420" Margin="0,0,0,20" Stretch="UniformToFill"/>
                        <TextBlock Text="{Binding Airdate}" Margin="0,0,0,0" Style="{StaticResource BodyTextBlockStyle}"/>
                    </StackPanel>
                </GridView.Header>
            </GridView>
        </Grid>
        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                        Style="{StaticResource NavigationBackButtonNormalStyle}"
                        VerticalAlignment="Top"
                        AutomationProperties.Name="Back"
                        AutomationProperties.AutomationId="BackButton"
                        AutomationProperties.ItemType="Navigation Button"/>
            <TextBlock x:Name="pageTitle" Text="{Binding UniqueId}" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" 
                        IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40"/>
        </Grid>
    </Grid>
</Page>

我不知道为什么这不起作用,但我已经在谷歌上搜索了5个多小时。

值不在预期范围内- Windows 8.1应用程序

在您的示例中没有一个好的MVVM系统。在xaml中,您将GridView绑定到名为Item的属性:

<GridView FlowDirection="LeftToRight" ItemsSource="{Binding Item}" x:Name="ItemDetailGridView">

Item必须是您的ViewModel类属性之一或ItemDetailPage类属性之一,但我没有在您的代码中看到这一点!然后你试图用代码后面的集合填充ItemsSource


我建议你尝试为你的项目制作一个正确的MVVM系统。您可以根据以下步骤进行设置:

1)Item Entity创建一个Model类。例如:

public Class MyItemModel{
    public string OverView {get; set;}
    public string CreatedBy {get; set;}
    public string Homepage {get; set;}
}

2)创建继承INotifyPropertyChanged接口的ViewModel类。例如:

public class MyItemViewModel: INotifyPropertyChanged
{
    private ObservableCollection<MyItemModel> _myItems;
    private string _createdBy;
    private string _homepage;
    public ObservableCollection<MyItemModel> MyItems
    {
        get { return _myItems; }
        set
        {
            _myItems = value;
            OnPropertyChanged("MyItems");
        }
    }
    public string CreatedBy
    {
        get { return _createdBy; }
        set
        {
            _createdBy = value;
            OnPropertyChanged("CreatedBy");
        }
    }
    public string Homepage
    {
        get { return _homepage; }
        set
        {
            _homepage = value;
            OnPropertyChanged("Homepage");
        }
    }
    //-----------------------------------------------------------------------
    public MyItemViewModel()
    {
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += navigationHelper_LoadState;
    }
    private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        var mitem = await MovieDataSource.GetItemAsync((String)e.NavigationParameter);
        // You must have data inside mitem if you put a breakpint at the below line
        MyItems = mitem;
    }
    //-----------------------------------------------------------------------
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

3)现在将ViewModel类绑定到DataContext页,并将ViewModelMyItems属性绑定到ElementItemsSource。它可以在Xamlcode-behind中执行。我推荐Xaml。例如:

<Page.DataContext>
    <vm:MyItemViewModel/>
</Page.DataContext>

.

<GridView FlowDirection="LeftToRight" ItemsSource="{Binding MyItems}" x:Name="ItemDetailGridView">
<!--.....Your other Xaml codes.....-->

注意:你也可以在你的Model类中实现INotifyPropertyChanged