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

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>

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.
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.

Related

UWP styles and themes in a nuget cannot find resource file

I have a UWP project and a class library for theme dictionary and some control default styles all merged into 1 file called "Themes.xaml" and I reference to that project and simply import it in "app.xaml" like so :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GI.UI/Themes.xaml" />
<ResourceDictionary Source="/Styles/_FontSizes.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
and it works as expected but when I create a nuget with the same styles project and use that instead, then project throws an exception it is unable to locate Themes.xaml at this path.
Update 1
I also tried adding an empty code behind file with "InitializeComponent()" for the ResourceDictionary "Themes.xaml" and it works perfectly when I import in the way showed below, but again it only works when I reference the project and doesn't work with nuget.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<theme:Themes />
<ResourceDictionary Source="/Styles/_FontSizes.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
(Themes.xaml.cs) Resource Dictionary code behind.
using Windows.UI.Xaml;
namespace GI.UI
{
public sealed partial class Themes : ResourceDictionary
{
public Themes() => InitializeComponent();
}
}
Themes.xaml
<ResourceDictionary
x:Class="GI.UI.Themes"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:BelowWindows10version1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract, 7)"
xmlns:Windows10version1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 7)"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<!-- some styles and themes here -->
</ResourceDictionary>
Update 2
Noticed this warning of an exception by xaml compiler .
So Now I have the code of that nuget project along with almost empty sample app, feel free to reference from project and then try referencing from nuget package as well.
project url : https://github.com/touseefbsb/UWP_Styles_Nuget
nuget package : https://www.nuget.org/packages/GI.UI/0.0.16
I have also given the nusepc file in the project which I used to create the package.
Note
When I create the nuget file I get following warning
I was able to solve the issue by following the folder structure as shown below :
and then editing nuspec file the following way :
You can try using like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///GI.Standards.Theme/Theme.xaml" />
<ResourceDictionary Source="/Styles/_FontSizes.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Using the ms-appx to refer to a file that comes from your app's package.
Source="ms-appx:///LibraryName/Folder/File.xaml"

Xamarin styles doesn't work - xamarin.forms

my Application is always with a white screen if I use style.
When I debugged I found out it doesn't leave the line
InitializeComponent();
that is in the app xaml cs
it builds, but after gets white screen forever.
this is my app xaml:
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="neoFly_Montana.App">
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<Color x:key="BackButton">Black</Color>
<Style x:Key="button" TargetType ="but">
<Setter Property="BackgroundColor" Value="{StaticResource BackButton}"/>
</Style>
</ResourceDictionary>
</Application.Resources>
The exception:
Xamarin.Forms.Xaml.XamlParseException: Position 11:14. Type but not found in xmlns http://xamarin.com/schemas/2014/forms occurred
Help please
There is no such type but...
Replace TargetType ="but" with TargetType="Button", and everything should work fine.
And BTW, looking at your code, it's a good habit to adopt strict naming rules, in this way you don't mix out lower/upper casing or key names. I say this because the key of your color is pascal-case, while the button's is in lower-case.
You should also use more specific keys, not just button, but I believe this was just for the example.

Error finding the resource dictionary [Fluent.Ribbon]

I have added fluent.dll as reference. After that add a folder on my project called "Themes" and pasted the all theme folder on "Themes" directory. On my application.xaml i have added the below code i.e
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!--Attach Default Fluent Control's Theme-->
<ResourceDictionary Source="pack://application:,,,/Fluent; Component/Themes/Office2013/Generic.xaml" />
</Application.Resources>
</Application>
But when run it show a error that
an error occurred while finding the resource dictionary
I am using Vs2013 .net ver4.5.
Your spacing is wrong
<ResourceDictionary Source="pack://application:,,,/Fluent; Component/Themes/Office2013/Generic.xaml" />
should be
<ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Office2013/Generic.xaml" />

ResourceDictionary in MergedDictionaries Source causing VS 2012 Designer not to load

the line ResourceDictionary Source="..." ist underlined and the VS designer throws an Exception. Everything was working in VS 2008
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MySolution.MyProject;component/Styles/MyStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Exception:
Exception:Value cannot be null. Parameter name: item (Same for the InnerException)
at Microsoft.Expression.Platform.InstanceBuilders.ClrObjectInstanceBuilder.InstantiateChildren(IInstanceBuilderContext context, ViewNode viewNode, DocumentCompositeNode compositeNode, Boolean isNewInstance)
what is the "build Action" set for your MyStyles.xaml?
try setting this to "page" if it is not already set-up like this.
see this other answer for details: (it seems to me that this could be related in a way)
Style TargetType causes XamlParseException when not attached to debugger

Getting AG_E_PARSER_BAD_PROPERTY_VALUE while data binding in WP7 User control

I have created a user control in Silverlight which basically animates the size of a rectangle.
<StackPanel x:Name="LayoutRoot" Background="Gray"><StackPanel.Resources>
<Storyboard x:Name="myStoryboard" Completed="myStoryboard_Completed">
<DoubleAnimation x:Name="ExpandY" From="{Binding Path=From}" To="{Binding Path=To}" Duration="00:00:2"
Storyboard.TargetName="myScaleTransform"
Storyboard.TargetProperty="ScaleY">
<DoubleAnimation.EasingFunction>
<BackEase Amplitude="0.0" EasingMode="EaseInOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</StackPanel.Resources>
<Rectangle x:Name="myRectangle" MouseLeftButtonDown="Mouse_Clicked"
Fill="{Binding Path=Barcolor}" Width="35" Height="50" RadiusX="2" RadiusY="2" MouseEnter="myRectangle_MouseEnter" Stroke="White" StrokeThickness="0" MouseLeave="myRectangle_MouseLeave">
<Rectangle.RenderTransform>
<ScaleTransform x:Name="myScaleTransform" CenterY="50"/>
</Rectangle.RenderTransform>
</Rectangle>
</StackPanel>
I have bound an object which implements INotifyPropertyChanged to this as the data context.
Now, the whole thing works fine when I run it as a silverlight control in an ASP.NET host page. But when I port the same code to create a WP7 user control I get the AG_E_PARSER_BAD_PROPERTY_VALUE pointing to the location where From="{Binding Path=From}" is located in the Double animation "ExpandY".
Is there anything different how the data is bound in a silverlight control and a WP7 user control?
The problem here is that DoubleAnimation is not a FrameworkElement and in Silverlight for WP7 you can only set a binding on a FrameworkElement. In order to achieve this effect you'll need to manipulate the animation in code-behind, possibly by wrapping the behavior up into a separate UserControl.
Same as this question: Binding to 'To' In Storyboard
Based on your tags I'm assuming that you're using SL4 on the web version. WP7 is based on Silverlight 3 and doesn't support binding in exactly the same way.
I suspect your problem is due to the same as in Dynamically change gradientstop color