2017-03-06 23 views
1

我正在創建一個移動Windows應用程序。我在UWP的視圖,其xaml.cs文件用下面的代碼開始:在UWP中使用綁定和DataContext不起作用

using Windows.Storage; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using EnergyBattle.Helpers; 

namespace EnergyBattle.Views.Tutorial 
{ 
    public sealed partial class TutorialPage1 : Page 
    { 
     ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; 
     StorageFolder localFolder = ApplicationData.Current.LocalFolder; 
     UniversalData VM = UniversalData.Instance; 

     public TutorialPage1() 
     { 
      this.InitializeComponent(); 
      DataContext = VM; 
     } 

相應視圖具有以下標記代碼:如圖所示

<Page 
    x:Class="EnergyBattle.Views.Tutorial.TutorialPage1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:EnergyBattle.Views.Tutorial" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" > 
     <TextBlock>Select your sportclub</TextBlock> 

     <ListView ItemsSource="{Binding ListOfSportclubs}" IsItemClickEnabled="True" ItemClick="listItemClick"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto" /> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="Auto" /> 
         </Grid.ColumnDefinitions> 

         <TextBlock Grid.Row="0" Grid.Column="0"><Run Text="{Binding name}" /></TextBlock> 
        </Grid> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </StackPanel> 
</Page> 

我試圖使用結合(在「」)中顯示我保存在模型中的列表中的項目。 DataContext中包含正確的數據,正如您在此斷點處所看到的那樣:https://i.stack.imgur.com/8TFVi.png - 請注意,此時仿真器仍處於引導屏幕,因此視圖尚未加載。

當然,ListView應該用DataContext中的七個項目填充,對嗎?對我來說,除了測試文字「選擇你的運動俱樂部」之外,這個視圖並沒有顯示任何內容。

我在輸出窗口中收到以下錯誤信息:

Error: BindingExpression path error: 'ListOfSportclubs' property not found on 'EnergyBattle.Helpers.UniversalData'. BindingExpression: Path='ListOfSportclubs' DataItem='EnergyBattle.Helpers.UniversalData'; target element is 'Windows.UI.Xaml.Controls.ListView' (Name='null'); target property is 'ItemsSource' (type 'Object') 

我在做什麼錯?非常類似的代碼在我見過的其他程序中工作得很好。

編輯:人們想了解更多關於ListOfSportclubs。這裏定義:

class UniversalData 
    { 
     public static UniversalData Instance { get; } = new UniversalData(); 
     private List<Sportclub> ListOfSportclubs = new List<Sportclub>(); 
     private List<Stoplicht> stoplichtList = new List<Stoplicht>(); 

     public List<Sportclub> getSportclubList() { return ListOfSportclubs; } 
     public List<Stoplicht> getStoplichtList() { return stoplichtList; } 

     public void setSportclubList(List<Sportclub> sportclubList) 
     { 
      this.ListOfSportclubs = sportclubList; 
     } 

     public void setStoplichtList(List<Stoplicht> stoplichtList) 
     { 
      this.stoplichtList = stoplichtList; 
     } 
    } 
+0

是'ListOfSportclubs'屬性還是歸檔?顯示爲聲明它的類。 – Rafal

+0

我使用這些信息編輯了我的帖子。 – Maplestrip

+0

你的'ListOfSportclubs'必須是一個*公共屬性*和適當的getter。 – Romasz

回答

0

由於錯誤說在您的視圖模型中找不到具有此名稱的屬性。 Bindingpublic屬性一起使用,而不是字段。

在您的屏幕截圖中,您的視圖模型中有ListOfSportclubs成員,但是由於錯誤,它是字段。

您需要在視圖模型中創建一些public屬性並創建對此屬性的綁定。

public List<Sportclub> ListOfSportclubs { get; set; } = new List<Sportclub>(); 
+0

Urgh,只需添加{get;設置;}像這樣解決了我的整個問題。謝謝你幫助我! – Maplestrip

+0

剛剛做過 - 仍在試圖弄清楚整個網站的工作原理是怎樣的^ _ ^ – Maplestrip

0

更改「UniversalData」級以下的固定問題:

class UniversalData 
    { 
     public static UniversalData Instance { get; } = new UniversalData(); 
     public List<Sportclub> ListOfSportclubs {get; set;} = new List<Sportclub>(); 
     private List<Stoplicht> stoplichtList = new List<Stoplicht>(); 

     public List<Sportclub> getSportclubList() { return ListOfSportclubs; } 
     public List<Stoplicht> getStoplichtList() { return stoplichtList; } 

     public void setSportclubList(List<Sportclub> sportclubList) 
     { 
      this.ListOfSportclubs = sportclubList; 
     } 

     public void setStoplichtList(List<Stoplicht> stoplichtList) 
     { 
      this.stoplichtList = stoplichtList; 
     } 
    } 

是啊,你看,正確的:所有我需要做的是增加「{獲得;設置;}」到它。謝謝大家的迴應!