【问题标题】:wpf ViewModel Binding Issuewpf ViewModel 绑定问题
【发布时间】:2014-07-12 11:00:32
【问题描述】:

我一直在尝试使用 caliburn 自学 WPF。大多数情况下一切正常,但我有一个我无法弄清楚的问题。我昨天得到它并试图修复它,我今天又回来了,经过很长时间我没有进一步的。如果有人知道我做错了什么,那就太棒了!

错误出现在我定义数据上下文的位置:

命名空间“clr-namespace:Translator.ViewModels”中不存在名称“TranslatorViewModel”。

我的看法:

 <Window x:Class="Translator.Views.TranslatorView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:model="clr-namespace:Translator.ViewModels"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d"
    d:DataContext="{x:Type model:TranslatorViewModel}"
    Title="Translator" Height="232.895" Width="1030.263">

我的视图模型:

 using Caliburn.Micro;

 namespace Translator.ViewModels
 {
      public class TranslatorViewModel : PropertyChangedBase
      {
      }
 }

我的引导程序:

using Translator.ViewModels;
using Caliburn.Micro;

namespace Translator
{
    public class AppBootstrapper : BootstrapperBase
    {
        public AppBootstrapper()
        {
             Initialize();
        }

        protected override void OnStartup(object sender,   System.Windows.StartupEventArgs e)
        {
             DisplayRootViewFor<TranslatorViewModel>();
        }
    }
} 

提前致谢!

【问题讨论】:

    标签: wpf binding viewmodel caliburn


    【解决方案1】:

    要设置设计时DataContext,您需要使用d:DesignInstance 标记扩展。看看这是否适合你:

    d:DataContext="{d:DesignInstance Type=model:TranslatorViewModel}"
    

    【讨论】:

    • 感谢您的回复,但我仍然收到相同的错误:-(
    • 这只是设计器错误还是编译时错误?通常,除非您对其进行编译,否则设计人员无法选择这些值。
    • 我认为这是编译时错误。如果我清理解决方案并重建它,错误又回来了
    • 啊哈!我找到了答案......当我重建它时,我的 App.Xaml 在 XAML 中出现错误,我没有正确引用我的解决方案命名空间。一旦我修复了这个错误,另一个错误(这是我昨天遇到的唯一错误)就消失了,我可以构建并运行我的解决方案!非常感谢您的帮助
    • 太棒了。它终于为你工作了。但即使您想进行设计时绑定,您也必须使用d:DesignInstance 而不是x:Type
    猜你喜欢
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2010-12-09
    相关资源
    最近更新 更多