我怀疑错误“无法创建MainViewModel的实例”是罪魁祸首,但说实话,我不确定。这是Visual Studio给我的唯一错误,代码编译并运行尽管它;我遇到的问题是我的列表框没有显示我的表中的数据。
MainPage.xaml的相关示例:
<phone:PhoneApplicationPage
x:Class="CarpetQuote.MainPage"
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:DesignWidth="480" d:DesignHeight="768"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}" />
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Carpets}" SelectionChanged="MainListBox_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding carpetName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</phone:PhoneApplicationPage>
下面的MainViewModelSampleData.xaml;这是负责“无法创建MainViewModel实例”的页面。
<local:MainViewModel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CarpetQuote"
SampleProperty="Sample Value">
<local:MainViewModel.Items>
<local:ItemViewModel LineOne="design one" LineTwo="Maecenas praesent accumsan bibendum" LineThree="Maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur"/>
<local:ItemViewModel LineOne="design two" LineTwo="Dictumst eleifend facilisi faucibus" LineThree="Pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent"/>
<local:ItemViewModel LineOne="design three" LineTwo="Habitant inceptos interdum lobortis" LineThree="Accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat"/>
<local:ItemViewModel LineOne="design four" LineTwo="Nascetur pharetra placerat pulvinar" LineThree="Pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum"/>
<local:ItemViewModel LineOne="design five" LineTwo="Sagittis senectus sociosqu suscipit" LineThree="Dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis"/>
<local:ItemViewModel LineOne="design six" LineTwo="Torquent ultrices vehicula volutpat" LineThree="Senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend"/>
</local:MainViewModel.Items>
</local:MainViewModel>
我在其他地方读过一些关于错误的内容,但似乎没有什么帮助。一般的共识似乎是编译器不喜欢我的MainViewModel构造函数中的东西,但是我唯一的那一行db = new CarpetContext(App.ConnString);
是必需的,并且我在另一个项目中没有错误地运行功能相同的代码。
如果这个错误不应该归咎于失败的绑定,这里是App.xaml.cs的相关部分,其中创建了数据库。
public partial class App : Application
{
private static MainViewModel viewModel = null;
public static MainViewModel ViewModel
{
get
{
if (viewModel == null)
{
viewModel = new MainViewModel();
}
return viewModel;
}
}
private static string connString = null;
public static string ConnString { get { return connString; } }
public App()
{
// Autogenerated code and comments snipped
if (connString == null)
{
connString = Resources["IsoDBConn"] as string;
}
createDB();
}
private void createDB()
{
using (var db = new CarpetContext(ConnString))
{
if (!db.DatabaseExists())
{
db.CreateDatabase();
}
}
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
if (!App.ViewModel.isDataLoaded)
{
App.ViewModel.loadData();
}
}
}
最后(我觉得代码之墙真的很糟糕),来自MainViewModel的引用函数。
public bool IsDataLoaded
{
get;
private set;
}
public void loadData()
{
db.Carpets.InsertOnSubmit(new Carpet("StainMaster", 10));
db.Carpets.InsertOnSubmit(new Carpet("Windsor", 12));
db.Carpets.InsertOnSubmit(new Carpet("Homfray", 11.5m));
db.Carpets.InsertOnSubmit(new Carpet("Wall Street", 15));
db.Carpets.InsertOnSubmit(new Carpet("Axminster", 16.5m));
db.SubmitChanges();
Carpets = new ObservableCollection<Carpet>(db.Carpets.ToList());
this.isDataLoaded = true;
}
答案 0 :(得分:0)
好的,这是一个奇怪的,但我在另一台计算机上正在研究它......它运作良好。
原来问题在于我的Windows Phone SDK安装。列表框确实填充,但文本不可见。
这可能太局部化而无用,建议关闭。