Windows phone 7 透视项目资源未更新

Windows phone 7 透视项目资源未更新,windows-phone-7,mvvm,wpf-controls,wpftoolkit,Windows Phone 7,Mvvm,Wpf Controls,Wpftoolkit,您好,我有一个Category类,我正在从API下载数据并显示在Pivot中,现在对于数据加载的Pivot索引0,我添加了一个Pivot selection changed事件,当用户切换到其他PivotItems并加载数据时。但问题是项目资源没有更新。如果我喜欢这个pivot.itemssource=“”;和pivot.itemssource=mylist;我必须将轴。selectedIndex也更改为当前索引,在这种情况下,翻转不平滑,它首先转到0索引,因为轴。itemssource=“”;

您好,我有一个Category类,我正在从API下载数据并显示在
Pivot
中,现在对于数据加载的
Pivot
索引0,我添加了一个
Pivot selection changed事件,当用户切换到其他
PivotItems
并加载数据时。但问题是
项目资源
没有更新。如果我喜欢这个
pivot.itemssource=“”
;和
pivot.itemssource=mylist
;我必须将
轴。selectedIndex
也更改为当前索引,在这种情况下,翻转不平滑,它首先转到0索引,因为
轴。itemssource=“”
;然后来到真实的地方。请帮我找到一个解决方案,这样它会自动绑定和更新。我的课程如下:

Category.xaml

C类
名称空间MyApp.Views
{
公共部分类类别:PhoneApplicationPage
{
List webclientsList=新列表();
私有int selectedIndex=0;
私有列表框子列表;
公共只读从属属性列表VerticalOffsetProperty;
公共类别()
{
初始化组件();
对于(int i=0;i对于(int i=0;i首先尝试设置

 pivot.itemssource=null;
然后

 pivot.itemssource=myList;
但这是错误的方式。

如果你想成为一名优秀的
windowsphone
开发者,你必须知道什么是
MVVM

首先,你必须阅读和理解

然后尝试了解如何使用现有的mvvm工具包,例如或

您应该很好地理解如何使用MVVM,然后您就可以自己解决问题了

希望能有所帮助

也检查一下

下面是我在项目中使用MVVM所遵循的步骤。
从silverlight安装MVVM
http://www.galasoft.ch/mvvm/installing/  不知道确切的链接,但我想它可以在这里。
现在重新启动VisualStudio。
转到右键单击项目并将新项目添加到项目中。
现在在ViewModel文件夹中添加模型视图定位器。(保存这些文件将是一个很好的做法。)
放入新文件夹(如ViewModel、View、Model)
现在,您已经创建了一个viewlocator,它可以处理viewmodel的所有清理过程。
现在,在同一个文件夹中为您的需求编辑创建一个modelview,如下所示,您也可以
可根据您的要求自由添加更多参数。
使用GalaSoft.MvvmLight;
使用制度;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Ink;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统
 pivot.itemssource=null;
 pivot.itemssource=myList;
Install MVVM from silverlight
http://www.galasoft.ch/mvvm/installing/  Dont know the exact link but I think its available here.

Now restart your visual studio.

Go to Right click on your project and add new Items to your project.
Now add Model View Locator inside ViewModel  folder. (It will be good practice to keep these file 

into a new folder like ViewModel, View, Model)

Now you have created a viewlocator which handles all the process like cleaning for a viewmodel.

Now in the same folder create a modelview for your requirement edit like below, and also you are 

free to add more parameters acc to your requirement.

using GalaSoft.MvvmLight;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using MyProject.Model;
namespace MyProject.ViewModel
{

    public class ProductViewModel : INotifyPropertyChanged
    {
         private ObservableCollection<ProductsClass> productData;

         public ObservableCollection<ProductsClass> ProductData
        {
            get
            {
                return productData;
            }

            set {
                productData = value;
                RaisePropertyChanged("ProductData");
            }
        }


         public ProductViewModel(ObservableCollection<ProductsClass> productData)
        {
            ProductData = productData;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;

        if (handler != null)

             {

                 handler(this, new PropertyChangedEventArgs(propertyName));

             }


    }





        /// <summary>
        /// Initializes a new instance of the ProductViewModel class.
        /// </summary>
        public ProductViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
    }
}



Now we have created our modelview, we have to mention something for this in our locator as 

follows(Make the necessary changes only):








namespace Myproject.ViewModel
{

    public class ViewModelLocator
    {





    private static MainViewModel _main;



        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        private static ProductViewModel _viewModelProduct;



        public ViewModelLocator()
        {


            CreateMain();

            CreateViewModelProduct();


        }



        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            _main.Cleanup();
            _main = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            ClearMain();

            ClearViewModelProduct();

        }



        //Product model declaration

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        public static ProductViewModel ProductViewModelStatic
        {
            get
            {
                if (_viewModelProduct == null)
                {
                    CreateViewModelProduct();
                }

                return _viewModelProduct;
            }
        }

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public ProductViewModel ViewModelProduct
        {
            get
            {
                return ProductViewModelStatic;
            }
        }



        /// <summary>
        /// Provides a deterministic way to create the ViewModelPropertyName property.
        /// </summary>
        public static void CreateViewModelProduct()
        {
            if (_viewModelProduct == null)
            {
                _viewModelProduct = new ProductViewModel();
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ViewModelPropertyName property.
        /// </summary>
        public static void ClearViewModelProduct()
        {
            //_main.Cleanup();
            _viewModelProduct = null;
        }


    }
}


Now inside your VIew create a variable like 

 private ProductViewModel pvm;

inside constructor add this:

 pvm = (ProductViewModel)Resources["pviewModel"];

Add this in your loaded method:

  Binding binding = new Binding("ProductData") { Source = pvm };


Like



 private void Product_Loaded(object sender, RoutedEventArgs e)
        {
            //Additional Code
            pvm.ProductData = Constants.productDet;
            //Additional Code          

            Binding binding = new Binding("ProductData") { Source = pvm };
        }


Now after adding some more code in your xaml file whenever you update anything in .cs it will 

automatically reflect in your view.


Make following in your xaml file:

DataContext="{Binding ViewModelProduct, Source={StaticResource Locator}}"
    Loaded="Product_Loaded"> at the top declaration



    <phone:PhoneApplicationPage.Resources>

        <viewModel:ProductViewModel x:Key="pviewModel" />

    </phone:PhoneApplicationPage.Resources>



Now the productsclass will be similar like this(its on u how u add parameters acc to ur requirement)





namespace Myproject.Model
{
    public class ProductsClass : INotifyPropertyChanged
    {
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    name = value;
                    // NotifyPropertyChanged("Name");
                }
            }
        }


        private ProductClass productDetailsOverview;
        public ProductClass ProductDetailsOverview
        {
            get { return productDetailsOverview; }
            set
            {
                if (productDetailsOverview != value)
                {
                    productDetailsOverview = value;
                    NotifyPropertyChanged("ProductDetailsOverview");
                }
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }


    public class ProductClass 
    {

        public long product_start_offset { get; set; }
        public ObservableCollection<FiltersData> filters_data { get; set; }
        public long product_end_offset { get; set; }
        public long product_count { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


and you can use ProductDetailsOverview in the itemssource to access any parameter like

ItemsSource="{Binding ProductDetailsOverview.product_list}