【发布时间】:2015-02-02 13:44:27
【问题描述】:
我有下面的 ViewModel 类,我在 XAML 中用作 DataContext..
public class ViewModel
{
public ObservableCollection<Model> Collection { get; set; }
public ViewModel()
{
Collection = new ObservableCollection<Model>();
GenerateDatas();
}
private void GenerateDatas()
{
this.Collection.Add(new Model(0, 1));
this.Collection.Add(new Model(1, 2));
this.Collection.Add(new Model(2, 3));
this.Collection.Add(new Model(3, 4));
}
}
public class Model
{
public double X { get; set; }
public double Y { get; set; }
public Model(double x, double y)
{
X = x;
Y = y;
}
}
我通过在 XAML 中创建应用程序的命名空间来链接它,如下所示:
xmlns:local="clr-namespace:MyApplication"
然后我按如下方式访问 ViewModel 以用作 DataContext:
<sparrow:SparrowChart.DataContext>
<local:ViewModel/>
</sparrow:SparrowChart.DataContext>
但是我得到一个错误,本地命名空间中不存在 ViewModel 这个名称。我该如何解决这个问题?
完整的 XAML 文件:
<phone:PhoneApplicationPage
x:Class="SparrowCharts.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clrnamespace: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"
xmlns:sparrow="clr-namespace:Sparrow.Chart;assembly=Sparrow.Chart.WP8.45"
xmlns:local="clr-namespace:MyApplication"
mc:Ignorable="d"
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 x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<sparrow:SparrowChart>
<sparrow:SparrowChart.DataContext>
<local:ViewModel/>
</sparrow:SparrowChart.DataContext>
<sparrow:SparrowChart.XAxis>
<sparrow:LinearXAxis/>
</sparrow:SparrowChart.XAxis>
<sparrow:SparrowChart.YAxis>
<sparrow:LinearYAxis/>
</sparrow:SparrowChart.YAxis>
<sparrow:LineSeries PointsSource="{Binding Collection}" XPath="X" YPath="Y"/>
</sparrow:SparrowChart>
</Grid>
</Grid>
C# 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MyApplication.Resources;
using System.Collections.ObjectModel;
namespace MyApplication
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
// Create a ViewModel
public class ViewModel
{
public ObservableCollection<Model> Collection { get; set; }
public ViewModel()
{
Collection = new ObservableCollection<Model>();
GenerateDatas();
}
private void GenerateDatas()
{
this.Collection.Add(new Model(0, 1));
this.Collection.Add(new Model(1, 2));
this.Collection.Add(new Model(2, 3));
this.Collection.Add(new Model(3, 4));
}
}
public class Model
{
public double X { get; set; }
public double Y { get; set; }
public Model(double x, double y)
{
X = x;
Y = y;
}
}
}
}
【问题讨论】:
-
您的类可能驻留在不同的命名空间中。如果您确定您的 ViewModel 类位于命名空间 ViewModel 中并且您的项目正在运行,那么您可以安全地忽略此错误。
-
@Vishal 项目无法运行并出现错误... ViewModel 类驻留在已定义为本地命名空间的应用程序命名空间中
-
你能给我看看你的完整 xaml 文件吗?
-
@EsotericScreenName 做到了 :-)
-
您确定您的
ViewModel类实际上在您的MyApplication命名空间中吗?您没有在代码示例中显示这一点。