Design DataContext无法按预期工作

问题描述 投票:0回答:2

在一个新的WP8.1项目中,我创建了一个ViewModel并将其用于设计数据。我的绑定没有显示我期望的值:

namespace App1
{
    public class ViewModel
    {
        public ViewModel()
        {
            this.Name = "hello world";
        }

        public string Name
        {
            get; set;

        }
    }
}

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DataContext="{d:DesignInstance local:ViewModel}">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="{Binding Name}" />
    </Grid>
</Page>

在Blend的设计图面或Visual Studio的设计器中,看到的是文本“名称”,而不是“ hello world”。

Page的设计时DataContext应该是我的ViewModel的实例,{Binding Name}应该是我继承的DataContext的Name属性的值。我在这里想念什么?

c# wpf xaml windows-phone-8 windows-phone-8.1
2个回答
0
投票

默认情况下,DesignInstance尝试模拟视图模型中的数据。如果要使用实际数据,则需要设置IsDesignTimeCreatable标志:

d:DataContext="{d:DesignInstance local:ViewModel, IsDesignTimeCreatable=True}"

0
投票

原来您需要构建非ARM才能正常工作。

问题是我正在使用ARM配置(当我想部署到Phone时切换到该配置),并且我已经更改了x86配置以构建ARM。因此,无论我选择了ARM还是x86配置,它都可以构建ARM,因此无法正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.