2013-07-08 5 views
0

Это класс, где я пытаюсь загрузить данныеMainviewModel окна телефон 8

PAGE2 XAML

<xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    shell:SystemTray.IsVisible="True"> 

    <!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <!--TitlePanel contains the name of the application and page title--> 
     <StackPanel Grid.Row="0" Margin="12,17,0,28"> 
      <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> 
      <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
     </StackPanel> 

     <!--ContentPanel - place additional content here--> 
     <Grid x:Name="ContentPanel" Margin="0,10,0,0" Grid.Row="1"> 

      <phone:LongListSelector x:Name="MainLongListSelector" Margin="10,2,0,0" ItemsSource="{Binding Items2}" SelectionChanged="MainLongListSelector_SelectionChanged"> 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Margin="0,0,0,17" Height="154"> 
          <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="20" Margin="0,0,0,538" Height="150"/> 
         </StackPanel> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 


      </phone:LongListSelector> 


     </Grid> 
    </Grid> 

</phone:PhoneApplicationPage> 

MainViewModel

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using Package_Tracker_P.Resources; 
using System.Collections.Generic; 
using Windows.Storage; 
using System.IO; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Text.RegularExpressions; 

namespace Package_Tracker_P.ViewModels 
{ 
    public class MainViewModel : INotifyPropertyChanged 
    { 
     public static List<Car> carlist = new List<Car>(); 

     public MainViewModel() 
     { 
      this.Items = new ObservableCollection<ItemViewModel>(); 
      this.Items2 = new ObservableCollection<ItemViewModel>(); 
     } 

     /// <summary> 
     /// A collection for ItemViewModel objects. 
     /// </summary> 


     public ObservableCollection<ItemViewModel> Items { get; private set; } 
     public ObservableCollection<ItemViewModel> Items2 { get; private set; } 


     private string _sampleProperty = "Sample Runtime Property Value"; 
     /// <summary> 
     /// Sample ViewModel property; this property is used in the view to display its value using a Binding 
     /// </summary> 
     /// <returns></returns> 
     public string SampleProperty 
     { 
      get 
      { 
       return _sampleProperty; 
      } 
      set 
      { 
       if (value != _sampleProperty) 
       { 
        _sampleProperty = value; 
        NotifyPropertyChanged("SampleProperty"); 
       } 
      } 
     } 

     /// <summary> 
     /// Sample property that returns a localized string 
     /// </summary> 
     public string LocalizedSampleProperty 
     { 
      get 
      { 
       return AppResources.SampleProperty; 
      } 
     } 

     public bool IsDataLoaded 
     { 
      get; 
      private set; 
     } 

     /// <summary> 
     /// Creates and adds a few ItemViewModel objects into the Items collection. 
     /// </summary> 
     public async void LoadData(Car toyota) 
     { 
      // Sample data; replace with real data 
      carlist.Add(toyota); 
      this.Items.Add(new ItemViewModel() { ID = (Items.Count.ToString()), LineOne = toyota.status, LineTwo = "", LineThree = "" }); 
      this.IsDataLoaded = true; 
      //await openfile(); 
      await IO.WriteToFile(); 
     } 

public async void LoadData() 
     { 
      try 
      { 
       stuff(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error Loading Data"); 
      } 
     } 
     public void stuff() 
     { 
      this.Items2.Add(new ItemViewModel() { ID = (Items2.Count.ToString()), LineOne = "Auto Detect", LineTwo = "", LineThree = "" }); 
      this.IsDataLoaded = true; 
     } 


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

Проблема не загрузки данных для СТРАНИЦА 2. Он загружает его для главной страницы с помощью элементов1. Пока приложение загружается, он вызывает LoadData() и этот элемент вызова(), который добавляет данные в items2. Правильно привязан в файле xaml для PAGE TWO в текстовом блоке. Я что-то упускаю?

ответ

0

я думаю, что главная проблема с этим кодом является то, что вы не установили DataContext страницы 2 как если вы используете MVVM свет, то

DataContext = {Binding MainViewModel,Source="{StaticResource Locator}}" 
Смежные вопросы