c# - 显示意外值的 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,这个限制仍然有效吗?
解决方案
我什么时候可以期望设计师反映对 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");
// }
//}
}
推荐阅读
- reactjs - Cypress React 单元测试 - 如何模拟/存根组件
- node.js - 每个签名的 this 类型都是与 Typescript 不兼容的 mongoose 方法
- python - 在必须为 pypi 打包的 python 代码中使用 os 模块
- java - 如何限制 Firebase 数据库中的推送量
- android - 如何解决 java.lang.IllegalArgumentException: Invalid target position 错误?
- reactjs - 故事书动作未记录
- ruby - Ruby 错误:`calcular_valor_final':未定义的方法`*'
- entity-framework - 如何在实体框架代码第一个迁移文件中生成默认sql值
- c++ - dart 和 c++ 之间的套接字加密和解密
- jmeter - 通过命令行值触发条件