xaml - MVVM-Light 未显示设计模式数据

标签 xaml mvvm uwp mvvm-light uwp-xaml

我使用 TemplateStudio 和 MVVM-Light 创建了一个 UWP 应用,但无法在 Visual Studio(或 Blend)中显示设计数据。

查看模型定位器:

public class ViewModelLocator
{
    NavigationServiceEx _navigationService = new NavigationServiceEx();

    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if(ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<IFooProvider, DesignFooProvider>();
        }
        else
        {
            SimpleIoc.Default.Register<IFooProvider, FooProvider>();
        }

        SimpleIoc.Default.Register(() => _navigationService);
        SimpleIoc.Default.Register<ShellViewModel>();
        Register<MainViewModel, MainPage>();
        Register<FooViewModel, FooView>();

        _navigationService.SetMainViewModel(MainViewModel);
    }

    public MainViewModel MainViewModel => ServiceLocator.Current.GetInstance<MainViewModel>();

    public ShellViewModel ShellViewModel => ServiceLocator.Current.GetInstance<ShellViewModel>();

    public FooViewModel FooViewModel => ServiceLocator.Current.GetInstance<FooViewModel>();

    public void Register<VM, V>() where VM : class
    {
        SimpleIoc.Default.Register<VM>();
        _navigationService.Configure(typeof(VM).FullName, typeof(V));
    }
}

FooViewModel 有一个绑定(bind)到 FooView.xaml 中的 ObservableCollection

现在,当我运行实际代码时,一切都运行良好,并且我的 FooViewModel 正确地填充了来自 FooProvider 的数据。

尽管在 Visual Studio 或 Blend 中查看 FooView,FooView.xaml 的 ListBox 中没有显示任何数据。

有没有办法在设计时调试出现的问题?如何修复定位器以在设计时在 FooView 中正确显示数据?

(注意:我添加到上述类中的唯一代码与 FooView 相关项目相关,其他代码已预先填充)。

最佳答案

Is there a way to debug what's going wrong during design time?

是的,我们可以。步骤如下:

  1. 打开任务管理器,找到XDesProc.exe并从详细信息点击记录其PID
  2. 将断点设置为构造函数代码或您要检查的任何代码
  3. 从 Blend 打开您的应用,找到第二个 XDesProc.exe 并记录其 PID
  4. 关闭 FooView 页面。
  5. 转到 Visual Studio,选择“调试”->“附加到进程”,然后选择托管(v4.6、v4.5、v4.0)代码,然后选择 XDesProc.exe 的 Blend PID
  6. 返回 Blend 并打开您想要查看设计数据问题的页面
  7. Visual Studio 将在您想要的断点处中断。

顺便说一句,我没有您的代码,所以我不太确定是否重现了您的问题。您可以调试并在调试后分享给我们信息。

How do I fix my locator to correctly display data in FooView during design time?

此处的解决方法是使用以下方式手动添加设计时数据:

<Page
x:Class="MVVMLightStartUp.Views.FooViewPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:MVVMLightStartUp.ViewModels"
DataContext="{Binding FooViewViewModel, Source={StaticResource Locator}}" 
d:DataContext="{d:DesignInstance Type=vm:DesignTimeViewModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d">

并在后面的代码中定义一个DesignTimeViewModel.cs:

 public class DesignTimeViewModel
{
    public ObservableCollection<Foo> BindingData { get; set; }

    public DesignTimeViewModel(DesignTimeDataService dataservice)
    {
        var FooList = dataservice.GetFoo();
        BindingData = new ObservableCollection<Foo>(FooList);
    }
}

有关详细信息,您可以引用以下一些博客:

https://learn.microsoft.com/en-us/windows/uwp/data-binding/displaying-data-in-the-designer https://msdn.microsoft.com/en-us/magazine/dn169081.aspx

关于xaml - MVVM-Light 未显示设计模式数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44558827/

相关文章:

xaml - "The type or namespace name ' 应用程序 ' could not be found"Xamarin 表单

wpf - ListView XAML 中的替代背景颜色

c# - UWP 应用中的表

c# - Contextmenu 的 PlacementTarget 未设置

c# - 如何从 UWP TimePicker 中删除按钮

c# - 如何使用 MVVM Light ViewModelLocator 处理多个 ViewModel

wpf - ViewModel 应该如何与 Repository 交互?

c# - 使用 MEF 在 Prism 中查看初始化时出现问题

c# - Azure 注销异步

uwp - 绘制虚线边框