"Value cannot be null" error in App.xaml with ResourceDictionary (WPF) - c#

Using .NET Framework 4.6.1 and I'm using a UI kit that I've installed via NuGet and they are referenced correctly in the project.
App.xaml
<Application x:Class="ExampleApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:adonisUi="clr-namespace:AdonisUI;assembly=AdonisUI"
xmlns:local="clr-namespace:ExampleApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.DarkColorScheme}" />
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.ClassicTheme}" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="ExampleApp.MainWindow"
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:local="clr-namespace:ExampleApp"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Window.Style>
<Style TargetType="Window"
BasedOn="{StaticResource {x:Type Window}}" />
</Window.Style>
<Grid>
<StackPanel Margin="10">
<Button Content="Click Me"
HorizontalAlignment="Center" />
</StackPanel>
</Grid>
</Window>
Issues:
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.DarkColorScheme}" />
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.ClassicTheme}" />
Both lines in App.xaml are getting the following error:
Value cannot be null. Parameter name: item.
I have tried multiple fresh projects, building and rebuilding and I keep getting this error. I am able to build the project and I can see the styles from the UI kit correctly applied on MainWindow even though the error is still there.
However the styles don't appear on the designer window, I'm not sure if it's related to the error I'm getting or not.
Any ideas what could cause this?

I just tried it and it does the same thing to me... using Visual Studio Enterprise 2017 15.9.7
If you look at AdonisUI.ResourceLocator in a decompiler (I used Telerik's JustDecompile), you'll see the definitions:
public static Uri ClassicTheme
{
get
{
return new Uri("pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml", UriKind.Absolute);
}
}
public static Uri DarkColorScheme
{
get
{
return new Uri("pack://application:,,,/AdonisUI;component/ColorSchemes/Dark.xaml", UriKind.Absolute);
}
}
public static Uri LightColorScheme
{
get
{
return new Uri("pack://application:,,,/AdonisUI;component/ColorSchemes/Light.xaml", UriKind.Absolute);
}
}
If you change your App.xaml to reference using these values then it works.
<ResourceDictionary Source="pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml" />
<ResourceDictionary Source="pack://application:,,,/AdonisUI;component/ColorSchemes/Dark.xaml" />

Related

WPF: Mahapps Metro Watermark is not showing up

I am working on a program, and in the settings I want watermarks to tell the user what he can insert there. I used Mahapps Metro, but the watermarks didn't show up, so I created a blank project with only one textbox, but it still doesn't show up.
My Code:
<Controls:MetroWindow x:Class="TestWPF.MainWindow"
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:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:local="clr-namespace:TestWPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Background="White">
<TextBox Controls:TextBoxHelper.Watermark="TestWater" Height="25" Width="200"/>
</Grid>
</Controls:MetroWindow>
The outcome:
(The background is actually "Wheat" to make the textbox more visible)
Am I missing anything why it doesn't work?
Add this in the App.xaml of the your WPF Project.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I've tried this and this is working fine after this.
Go through the documentation provided here.

WPF Xaml suggesions not working on VS2019 16.4.4

I have a project (WpfControlLibrary1:WPF user control library) which includes MyDictionary.xaml. This dictionary includes resource definitions like :
<SolidColorBrush Color="AliceBlue" x:Key="MY_COLOR"/>"
I have another project(WpfCustomControlLibrary1:WPF custom control library).
WpfCustomControlLibrary1 references WpfControlLibrary1 to use styles. WpfCustomControlLibrary1 has App.xaml file like this.
<Application x:Class="WpfCustomControlLibrary1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And I have a UserControl1.xaml in WpfCustomControlLibrary1 like this.
**<UserControl x:Class="WpfCustomControlLibrary1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800">
<!--Suggestion not working for MY_COLOR-->
<!--Please open the solution in VS 2013 and see the Suggestions working-->
<Button Background="{StaticResource MY_COLOR}" />
</UserControl>**
Every thing is OK. But the problem is Static resource suggestions not working in VS2019.
I'm opening the solution with VS2013. Suggestions working in VS2013

Resource Dictionary not working - Exception raised for Name/Key not found

I have just started working with Resource Dictionaries and I am stuck on this because my resource dictionary is not working at all. I have tried code-behind and XAML but every time I get exceptions and the app crashes.
If I reference the Dictionary through XAML I get the exception at runtime that Name/Key is not found. The code I used in App.xaml is:
<Application
x:Class="WatchfreeWebsite.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WatchfreeWebsite.Helpers">
<Application.Resources>
<TransitionCollection x:Key="TransCollection">
<EdgeUIThemeTransition Edge="Right"/>
</TransitionCollection>
<ResourceDictionary x:Key="resourcesDictionary">
<ResourceDictionary.MergedDictionaries>
<local:GlobalTemplates Source="Helpers/GlobalTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The resource dictionary holds aDataTemplate and a MediaTransportControlsStyle but I cant seem to reference it through XAML because it gives syntax errors and during the runtime the page produces exception while loading XAML at InitializeComponent(); stage.
Resource Dictionary:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WatchfreeWebsite.Helpers"
x:Class="WatchfreeWebsite.Helpers.GlobalTemplatesClass"
xmlns:data="using:WatchfreeWebsite.Helpers">
<DataTemplate x:Key="StreamBoxItemTemplate"
x:DataType="data:StreamingHelper">
<TextBlock Text="{x:Bind StreamName, Mode=OneWay}"
Style="{StaticResource BodyTextBlockStyle}"
TextWrapping="NoWrap"
MaxLines="1"
TextTrimming="WordEllipsis"/>
</DataTemplate>
<Style TargetType="MediaTransportControls"
x:Key="myCustomTransportControls">
<Setter Property="IsTabStop" Value="False" />
.......
</Style>
</ResourceDictionary>
The class behind the resource dictionary is:
public partial class GlobalTemplatesClass
{
public GlobalTemplatesClass()
{
this.InitializeComponent();
}
}
I reference the DataTemplate inside the above style and this style is referenced in another page as:
<MediaPlayerElement x:Name="MediaView"
Grid.Row="2"
Source="{Binding MediaSourceObject, Mode=OneWay}"
DoubleTapped="MediaView_DoubleTapped"
AreTransportControlsEnabled="True">
<MediaPlayerElement.TransportControls>
<data:CustomTransportControlsHelper Style="{StaticResource ResourceKey=myCustomTransportControls}"/>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
But this is not working and there is a red line below the resource name saying that the resource is not found.
Is there something that I am missing? If someone can help me here please provide your suggestions. Thanks
When you add multiple items under your resources, each of them should fall within the <ResourceDictionary> tag and not directly under <Application.Resources>.
That's because Resources itself is a dictionary, so you're in effect trying to replace that collection rather than add elements to it. Docs here: https://msdn.microsoft.com/en-us/library/system.windows.application.resources%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
I created a sample project with just an App.xaml at the project base level, a folder called Helpers and a ResourceDictionary under Helpers named GlobalTemplates.xaml to match yours.
Created a simple brush as an example in GlobalTemplates.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1.Helpers">
<SolidColorBrush x:Key="DefaultForeground" Color="DarkGreen" />
</ResourceDictionary>
In App.xaml
<Application
x:Class="App1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<TransitionCollection x:Key="TransCollection">
<EdgeUIThemeTransition Edge="Right"/>
</TransitionCollection>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Helpers/GlobalTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And then in MainPage.xaml successfully referenced the style from the dictionary:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Foreground="{StaticResource DefaultForeground}">Hello world</TextBlock>
</Grid>
</Page>

Strange 'missing an assembly reference?' error in wpf user control library

i have below error in my C# wpf user control library :
Severity Code Description Project File Line Error CS0234 The type or
namespace name 'StylesTemplates' does not exist in the namespace
'MHToolkit'(are you missing an assembly reference?)'`
i have resource dictionary in the folder,
the error will be disappear when i Exclude StylesTemplate Folder from the project
and i am using it in usercontrol
this my user control code :
<UserControl x:Class="MHToolkit.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:local="clr-namespace:MHToolkit"
xmlns:ST="clr-namespace:MHToolkit.StylesTemplates"
xmlns:VM="clr-namespace:MHToolkit.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<VM:MainViewModel x:Key="MainVM" />
</UserControl.Resources>
<UserControl.DataContext>
<Binding Source="{StaticResource MainVM}" />
</UserControl.DataContext>
<Grid>
</Grid> </UserControl>
i am using vs2014 and .net 4.5 .
i have no idea how to fix this error ?
any help will be appreciated
tanx in advance
image of solution explorer window below
Add CardViewStyle.xaml as the Resource Dictionary
Check the below code
Add this to the current Usercontrol
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MHToolkit;component/StylesTemplates/CardviewStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Otherwise you can add App.xaml file and used all apllication
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MHToolkit;component/StylesTemplates/CardviewStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Error visualizing Mahapps metro style

I'm trying to use the Mahapps's Metro Style v1.0 stable in VS2013 with Windows 7. According to the initial modification in the quick start section, the main window should look like this:
but instead look like this:
When I used version 0.7 worked fine.
Has anyone else experienced this?
Interesting fact:
The second image is what I get, it is saved in .jpeg, but when I saved the screenshot in .png format, it looks like this:
You should have always these basic stuff:
App.xaml
<Application x:Class="WpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Window xaml
<controls:MetroWindow x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="MainWindow"
Height="600"
Width="800">
<Grid>
<!-- now your content -->
</Grid>
</controls:MetroWindow>
Window xaml code behind
using System;
using MahApps.Metro.Controls; // MahApps.Metro reference
namespace WpfApplication
{
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
Hope this helps!
You can see how everything is implemented on very simple examples to GitHub MahApps.Metro
Surely you forgot to remove or add what is namespace in xaml page or not remove window parameters.

Categories

Resources