xamldesignerblenduwpvisualstates

Using Staticresouce in Visual State causes Blend Designer to break: "Invalid attribute value Unknow for property Background"


I'm trying to change the Background of a Grid by using a Visual State and a Staticresouce as the value in the Setter of the Visual State. It works just fine at Runtime but the designer shows the following error (which doesn't help me a lot):


Exception: Der Text zu diesem Fehlercode wurde nicht gefunden. (unknown error)

Stacktrace: at Windows.UI.Xaml.Hosting.XamlUIPresenter.Render() at Microsoft.VisualStudio.DesignTools.WindowsXamlDesigner.Views.WindowsUIXamlImageHost.RenderWorker() at Microsoft.VisualStudio.DesignTools.WindowsXamlDesigner.Views.WindowsUIXamlImageHost.RenderScheduler.OnRender(Object object) at Microsoft.VisualStudio.DesignTools.WindowsXamlDesigner.Views.WindowsUIXamlImageHost.RenderScheduler.b__26_0(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Invalid attribute value Unknown for property Background. InnerException: None


This is what I tried:

<Grid x:Name="grid">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="TestGroup">
             <VisualState x:Name="TestState">
                  <VisualState.Setters>
                       <Setter Target="grid.(Panel.Background)" Value="{StaticResource BackgroundBrush}" />
                  </VisualState.Setters>
             </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</Grid>


<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="BackgroundBrush" Color="Black"/>
</ResourceDictionary>


<App>
    ...
    <App.Resources>
         <ResourceDictionary>
              <ResourceDictionary.MergedDictionaries>
                   <ResourceDictionary Source="Resources/Test.xaml"/>
              </ResourceDictionary.MergedDictionaries>
         </ResourceDictionary>
    </App.Resources>
</App>

enter image description here


Solution

  • The current Blend is really buggy so I am not surprised if you see errors like this.

    One workaround is, reopen your page that contains this resource in Blend. You should see a dialog popping up (see below image). Then select your Test.xaml file from the Available dictionaries dropdown and hit OK.

    enter image description here

    By doing so blend will generate a DesignTimeResources.xaml file under your project Properties and it should eliminate the error on the designer.


    However, I also noticed that your <ResourceDictionary.MergedDictionaries> isn't wrapped within a <ResourceDictionary>. Shouldn't it be something like this instread?

    <App.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Test.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </App.Resources>
    

    Update

    This is indeed a Blend bug which even happens with built-in resources. To replicate, try building the project and then go to the States tab and select a VisualState, right after you will see the Blend Designer crashed. Note that I have Run project code in XAML Designer (if supported) turned off.

    Currently the only way to remove the crash is to reopen the xaml file.