c# - DataContext 和 DesignData - 找不到文件

标签 c# visual-studio xaml windows-phone-8

在我当前的布局中,我希望某些数据在设计 View 中可见,因此我阅读了有关 DataContext 和 DesignData 的内容,但我未能正确使用它们。

我的 MainPage.xaml 以这种方式启动:

<phone:PhoneApplicationPage
    x:Class="AppStalker.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"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True"
    d:DataContext="{d:DesignData Source=./SampleData/SampleData.xml}">

我的应用程序编译时没有警告/错误,我可以在模拟器中看到数据。无论如何,在构建解决方案之后,VS 2012 提示找不到 C:\path\to\project\AppName\SampleData\SampleData.xml

我三重检查我没有拼写错误,我也将 xml 更改为 xaml 但没有任何效果。我还三次验证了解决方案 View 显示了目录和文件。

现在它变得有趣了:如果我将路径更改为 Source=./App.xaml,它不会提示。如果我将其更改为引用 MainPage.xaml,它会提示文件中存在一些错误。如果我将 SampleData.xml 移动到与 App.xaml 相同的根目录,它仍然拒绝找到它们...

我还将文件的构建操作更改为ResourceEmbedded ResourceDesignData,但没有任何改变。

基本上我是按照这个LongListSelector Walkthrough

有什么想法吗?

最佳答案

文件应该是Resource

如果要将文件放在项目的根目录下;除了App.xaml,只需写:

d:DataContext="{d:DesignData SampleData.xml}"

如果您想将它放在项目的文件夹中:

d:DataContext="{d:DesignData SampleData/SampleFoodData.xml}"

和它一样:

d:DataContext="{d:DesignData Source=SampleData/SampleFoodData.xml}"

关于c# - DataContext 和 DesignData - 找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16966311/

相关文章:

WPF : Custom Button

c# - 减少代码的时间复杂度

visual-studio - Visual Studio不断更改project.sln文件

visual-studio - VS 2012 .NET 4.5 中的 Entity Framework 构建问题

xaml - Windows 应用商店通用应用程序中的字体大小缩放 (W8.1 + WP8.1)

c# - 将 Telerik RadGRID 绑定(bind)到列表<string> 对象

c# - 当我在文本框中输入时发出烦人的哔哔声

c# - 如何编辑模型的 ChildCollection?

c# - 拆分时间跨度并在 csv 中创建新字段

visual-studio - 是否可以更改 VisualHg 调用 hg.exe 的速率?