Error in WPF Visual Studio Designer?

Check the following scenario (others may also apply) [you can create a project, just copy the code in this file in the right file]:

a - Create a ResourceDictionary with the base material (Resources.xaml):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush Color="Red" x:Key="Test" /> <Style TargetType="{x:Type GroupBox}" x:Key="Test2" > <Setter Property="Background" Value="Blue" /> </Style> <Style TargetType="{x:Type TextBlock}" > <Setter Property="Foreground" Value="Green" /> </Style> </ResourceDictionary> 

b - create a user management base, where others inherit, containing the main resources (UserControlBase.cs):

 using System.Windows.Controls; using System; using System.Windows; namespace ResourceTest { public class UserControlBase : UserControl { public UserControlBase() { this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("ResourceTest;component/Resources.xaml", UriKind.RelativeOrAbsolute) }); } } } 

c - Create a UserControl inheriting the database (UserControl1.xaml):

 <ResourceTest:UserControlBase x:Class="ResourceTest.UserControl1" 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:ResourceTest="clr-namespace:ResourceTest" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" > <Grid> <GroupBox BorderBrush="{StaticResource Test}" Margin="3" Header="Test" Style="{DynamicResource Test2}" > <TextBlock Text="TESTTEST" /> </GroupBox> </Grid> </ResourceTest:UserControlBase> 

Results : StaticResources are not allowed (and Test BorderBrush is not loaded). DynamicResources are allowed (background is blue), but the designer says that he cannot find the resource in any case (the first time it works fine, but when you open / close the constructor, the resource cannot be resolved). Unnamed resources, such as the TextBlock style, work fine.

Is this a designer's mistake or am I doing something wrong? Is it good to declare resources dynamic in a scenario where resources will never change?

enter image description here

Thanks in advance.

+6
source share
1 answer

It seems that the designer has a problem with the resolution of MergedDictionaries , which are defined in the time code during development.

An even worse example can be seen by moving the ResourceDictionary to your Initialization.

 public UserControl1() { this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("TempProject;component/Resources.xaml", UriKind.RelativeOrAbsolute) }); InitializeComponent(); } 

In this case, the DynamicResource parameter cannot even be resolved at design time, indicating that the design-time view does not necessarily call constructors, as you might expect. I tested this with Visual Studio 2012, so this behavior has not changed since 2010.

in terms of your source test code, note that StaticResource successfully binds, as expected, at runtime (a red frame appears) regardless of the โ€œerrorโ€ and the absence of a red border, time.

I see two options:

  • Use DynamicResource where necessary to solve them during development. While you can use StaticResource , related issues with errors and lack of development-time representation will clearly be a problem. Other answers seem to indicate that there can be no big difference in performance between the two now.

  • Just create a ResourceDictionary in UserControl.Resources and do not inherit from the base class. While you are shortening some code using the base class, you are not more efficient since a new instance of ResourceDictionary will be created each time. Since you cannot (AFAIK) extend from the base class with the XAML interface, you can bet that MergedDictionary was not bound at this level of abstraction.

+1
source

Source: https://habr.com/ru/post/949727/


All Articles