在使用VS2012,WPF 4.5和设计时数据(特别是DesignInstance属性)时,我只花了几个小时处理问题.
目标:我希望在我的WPF项目(基于MVVM)中获得设计时数据支持,无论是在VS2012还是Blend中,我都无法在生命中使MVVMLight方法始终如一地工作.
因此,我尝试使用Blend提供的标记扩展,使用内置的设计时数据支持更改为"just".
问题:请考虑以下代码:
<Window x:Class="Nova.View.AlertsView"
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"
xmlns:local="clr-namespace:Nova.View"
xmlns:vm="clr-namespace:Nova.ViewModel"
mc:Ignorable="d"
DataContext="{Binding Alerts, Source={StaticResource Locator}}"
d:DataContext="{d:DesignInstance vm:DesignAlertsViewModel, IsDesignTimeCreatable=True}"
... />
Run Code Online (Sandbox Code Playgroud)
VS2012和Blend都报告"名称DesignAlertsViewModel在命名空间clr-namespace中不存在:Nova.ViewModel",即使intellisense解决它很好,并且你已经检查过一千次命名空间和类名都是正确的.
是否可以在uwp应用程序中使用x:bind和设计时间数据?
例如,如果我有一个文本框使用x:bind绑定到后面的代码中的viewmodel上的属性,该属性的值是否应该出现在设计器的文本框中?
有没有办法实现这个目标?
干杯
约翰尼
使用VS2013和Caliburn.Micro 2.0.2
鉴于此示例:
Name属性:
似乎在设计时,嵌套视图模型属性被忽略.有没有办法支持这个?
public class NestedViewModel : PropertyChangedBase
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyOfPropertyChange(() => Name);
}
}
public NestedViewModel()
{
Name = "Nested";
}
}
Run Code Online (Sandbox Code Playgroud)
<UserControl
x:Class="WpfApplication1.Views.NestedView"
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"
xmlns:cal="http://www.caliburnproject.org"
xmlns:viewModels="clr-namespace:WpfApplication1.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=viewModels:NestedViewModel, IsDesignTimeCreatable=True}"
cal:Bind.AtDesignTime="True">
<Grid>
<Label x:Name="Name" FontSize="16" Background="LightGreen"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
绿色标签显示Name设计器中嵌套视图模型的正确性:

public class ShellViewModel : PropertyChangedBase
{
private string _name;
private NestedViewModel _nestedViewModel; …Run Code Online (Sandbox Code Playgroud) wpf user-controls design-time-data caliburn.micro visual-studio-2013
Visual Studio 2010 可视化设计器是否允许在设计时通过外部 XML 文件加载数据?
看来我可以通过 d:DataContext 添加它,但是我有很多数据,通过 XML 加载它更容易。那么这可能吗?
在 WPF 中,我使用了以下的正常组合:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d:DataContext="{d:DesignData...}"
Run Code Online (Sandbox Code Playgroud)
允许设置设计时 DataContext。这意味着我在 XAML 中获得了合理的 Intellisense。我意识到上面的代码无法编译,但你明白了。
我刚刚开始使用 MAUI(没有 Blazor),并且希望实现同样的目标 - 这样我就可以告诉 Visual Studio 我的 ContentView 将绑定到特定的对象类型,即 d:BindingContext="" 以这样的方式运行时会被忽略吗?这将使设计 ItemTemplates 变得更加容易!
嗨,我尝试在wpf中使用我的第一个设计时数据.我使用以下教程:
我创建了简单的数据类,这里是:
public class Avatar:INotifyPropertyChanged
{
private string _name;
private string _surname;
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChanged("Name");
}
}
public string Surname
{
get { return _surname; }
set
{
_surname = value;
NotifyPropertyChanged("Surname");
}
}
public new event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了示例数据:
<TestForDesignTimeData:Avatar xmlns:TestForDesignTimeData="clr-namespace:TestForDesignTimeData" Name="John" Surname="Smith"/>
Run Code Online (Sandbox Code Playgroud)
并尝试在wpf窗口中使用设计时数据:
<Window x:Class="TestForDesignTimeData.MainWindow"
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" …Run Code Online (Sandbox Code Playgroud)