首页 > 解决方案 > 显示意外值的 DesignInstance

问题描述

所以我一直在尝试进入 WPF 设计器的 DesignData。我有一个页面:

<src:BasePage xmlns:cal="Namespace&AssemblyInfo"
              xmlns:src="AnotherNamespace"
              x:Class="Actual_Class"
              x:TypeArguments="ActualClassArg"
              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:d="http://schemas.microsoft.com/expression/blend/2008"           
              mc:Ignorable="d" 
              d:DataContext="{d:DesignInstance d:Type=cal:ViewDesignMock,
                                               IsDesignTimeCreatable=True}">

...
                    <TextBlock x:Name="UserInstructionLabel"         
                               VerticalAlignment="Bottom"
                               HorizontalAlignment="Center"
                               Text="{Binding userInstruction}">
                    </TextBlock>
</src:BasePage>

该页面的代码如下:

    public partial class InstructionPage : BasePage<InstructionViewController>
    {
        public InstructionPage() { }

        public InstructionPage(IDetectorControl detector, IBaser baser) : this()
        {         
            ViewModel = new InstructionViewController(detector, baser); //ViewModel is of BasePage
            DataContext = ViewModel.M_View;
            InitializeComponent();          
        }
   }

ViewDesignMock 如下:

    public class ViewDesignMock
    {
        public ViewDesignMock(){ }

        public System.String userInstruction
        {
            get => "TESTING";
        }
   }

结果是设计视图显示绑定的“userInstruction”,而不是“TESTING”。由于我对此还很陌生,因此我确实对发生的事情有一些疑问。我什么时候可以期望设计师反映对 DesignInstance 模型所做的更改?在构建?为什么我看到绑定上显示的属性名称,我能做些什么来反映值而不是名称?我正在使用 Visual Studio 2019。

先感谢您!

编辑:

无意中看到这篇 2016 年在微软写的文章,标题为“Debug or disable project code in XAML Designer”

“对于面向 ARM 或 X64 处理器的项目,Visual Studio 无法在设计器中运行项目代码,因此在设计器中禁用了禁用项目代码按钮。”

我的项目确实针对 x64,这个限制仍然有效吗?

标签: c#wpfvisual-studio

解决方案


我什么时候可以期望设计师反映对 DesignInstance 模型所做的更改?在构建?

是的,在 WPF 设计视图中,您需要刷新(重建)DesignInstance,该视图将显示更新值。

没有相关代码。我在我身边创建了一个简单的。它可以在 WPF 设计视图中显示值而不是属性的名称。

  public class ViewDesignMock : INotifyPropertyChanged
{
    public ViewDesignMock() { }

    public System.String userInstruction
    {
        get => "TESTING22222222222222";
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyRaised(string propertyname)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
        }
    }


    //private string _userInstruction = "TESTING22666222";
    //public string userInstruction
    //{
    //    get
    //    {
    //        return _userInstruction;
    //    }
    //    set
    //    {
    //        _userInstruction = value;
    //        OnPropertyRaised("userInstruction");
    //    }
    //}


}

在此处输入图像描述


推荐阅读