2013-07-11 173 views
0

我正在嘗試在我的視圖的xaml內的ViewModel中引用一個類,並且出現錯誤Object reference not set to an instance of an object。試圖將ViewModel設置爲ListBox的資源時發生錯誤。此外,當試圖設置我的ListBox的ItemsSource屬性時,另一個錯誤結果指出The resource "effects" could not be resolved如何在視圖中引用ViewModel

MainPage.xaml中

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 

     <Grid.Resources> 
      //Error occurs here! 
      <vm:EffectItems x:Key="effects"/> 
     </Grid.Resources> 

     //The ItemsSource property thus contains an error as well 
     <ListBox Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{StaticResource effects}" SelectionChanged="ListBox_SelectionChanged"    
       toolkit:TiltEffect.IsTiltEnabled="True" 
        ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Auto"> 
      <ListBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <toolkit:WrapPanel Orientation="Horizontal" ItemWidth="152" /> 
       </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical" Margin="14,0,0,10" > 
         <Image Source="{Binding Thumbnail}" Width="128" Height="128" /> 
         <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

我也曾嘗試以下設置,導致同樣的錯誤在同一個項目

<ListBox Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{StaticResource effects}" SelectionChanged="ListBox_SelectionChanged"    
       toolkit:TiltEffect.IsTiltEnabled="True" 
        ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Auto"> 
      <ListBox.Resources> 
       <vm:EffectItems x:Key="effects"/> 
      </ListBox.Resources> 
      <ListBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <toolkit:WrapPanel Orientation="Horizontal" ItemWidth="152" /> 
       </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical" Margin="14,0,0,10" > 
         <Image Source="{Binding Thumbnail}" Width="128" Height="128" /> 
         <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

ViewModel類

public class EffectItems : ObservableCollection<EffectItem> 
{ 
    public EffectItems() 
    { 
     Add(new EffectItem(new BlackWhiteEffect(), "data/icons/BlackWhite.png")); 
     Add(new EffectItem(new SepiaEffect(), "data/icons/Sepia.png")); 
     Add(new EffectItem(new TiltShiftEffect { UpperFallOff = 0.2f, LowerFallOff = 1.0f }, "data/icons/TiltShift.png")); 
     Add(new EffectItem(new PolaroidEffect { Tinting = 0.8f }, "data/icons/PolaYellow.png", "Pola")); 
    } 
} 

在頂部我的頁面我有xmlns:vm="clr-namespace:AppName.ViewModels"其中不包含錯誤。

+0

您可以發佈包含堆棧跟蹤的完整錯誤消息?在Visual Studio中設計時或在運行時期間是否出現錯誤? – Jehof

+0

您的viewmodel類是否有無參數的構造函數?除非你使用某種方式的依賴注入,否則如果你的構造函數有參數你必須自己實例化它。 –

+0

我在上面添加了ViewModel類,這是View中引用的類。我試圖重新創建Codeplex示例PicFx http://picfx.codeplex.com/ – Matthew

回答

1

您可以通過設置視圖DataContext將您的ViewModel綁定到視圖。在直接的方式是將其設置在後面的代碼的構造函數:使用默認綁定

// Constructor 
public MainPage() 
{ 
    InitializeComponent(); 

    DataContext = new EffectItems(); 
} 

然後你可以設置你的名單到DataContext的的ItemsSource:

ItemsSource="{Binding}" 
相關問題