Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.8k stars 778 forks source link

Design time data issues #804

Open vb2ae opened 2 years ago

vb2ae commented 2 years ago

Could this be related to:

https://github.com/Caliburn-Micro/Caliburn.Micro/blob/7ef1a9a2820a45b3bffc41a5d6f5b04ef661988b/src/Caliburn.Micro.Platform/ViewModelBinder.cs#L250-L254

or how GetNamedElements is defined:

https://github.com/Caliburn-Micro/Caliburn.Micro/blob/7ef1a9a2820a45b3bffc41a5d6f5b04ef661988b/src/Caliburn.Micro.Platform/BindingScope.cs#L124-L126

I can't see why there would be any differences from design-time from at runtime here, though.

Originally posted by @nietras in https://github.com/Caliburn-Micro/Caliburn.Micro/issues/214#issuecomment-1103793550

nietras commented 2 years ago

@vb2ae I have filed an issue on Visual Studio for this at https://developercommunity.visualstudio.com/t/WpfSurface-XAML-designer-does-not-instan/10020304#T-ND10032251 Here the response is this appears to be an issue with CM as quoted below. If you understand the specific issue and can help out or perhaps imagine a fix based on it I'd appreciate it.


I’m starting with the sample from Github: https://github.com/Caliburn-Micro/Caliburn.Micro/tree/master/samples/setup/Setup.WPF If I got something wrong here, please provide sample code+XAML to reproduce the problem.

In MainView.xaml, I changed the TextBlock to look like this:

<TextBlock x:Name="Title" />

So Calburn.Micro is looking for the MainViewModel.Title property. In the designer, Calburn.Micro won’t find that property, in fact it won’t even search for that property because nothing is triggering the Calburn.Micro binding process.

That lead me to the documentation for using Calburn.Micro with the XAML designer:

https://caliburnmicro.com/documentation/design-time It says you need to set d:DataContext=“…” and cal:Bind.AtDesignTime=“True”, so I did that. Also I created a default constructor for MainViewModel that sets a value for Title that the designer can show. It didn’t work all the time for me, until I moved the AtDesignTime property to the TextBlock, where the whole file was:

<UserControl x:Class="Setup.WPF.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:cal="http://caliburnmicro.com"
             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:viewmodels="clr-namespace:Setup.WPF.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             d:DataContext="{d:DesignInstance Type=viewmodels:MainViewModel, IsDesignTimeCreatable=True}">
    <StackPanel Orientation="Vertical">
        <TextBlock x:Name="Title" cal:Bind.AtDesignTime="True" />
    </StackPanel>
</UserControl>

While that works, it’s not right to put cal:Bind.AtDesignTime on any element but the root. When debugging, I see that if I put cal:Bind.AtDesignTime at the root element, then binding happens before all child elements are created, which means that there is nothing to do (no named child elements created yet). When I put cal:Bind.AtDesignTime on the TextBlock, it’s already created of course and binding works.

To me it seems like Calburn.Micro is supposed to support design-time binding. It may have worked in the past. But something broke along the way. The binding process is attempted too early, before the named elements are added. I think it needs to be fixed in Calburn.Micro.


vb2ae commented 2 years ago

Ok I will look at the Microsoft's comments