I'm new in mahapps.metro Themes . when i used ThemeManager it works just once and then in second use it rise an null reference exception . for example first i select Blue Theme it works but When after that i select green theme it not work and rise a null reference Exception .
here is a sample code :
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Blue"),
ThemeManager.GetAppTheme("BaseDark"));
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Green"),
ThemeManager.GetAppTheme("BaseDark"));
what is wrong ?
This lines should work at this point in App.xaml code behind or at your main Window constructor.
using System.Windows;
using MahApps.Metro;
namespace MetroDemo
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Blue"),
ThemeManager.GetAppTheme("BaseDark"));
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Green"),
ThemeManager.GetAppTheme("BaseDark"));
}
}
}
Make sure you are adding the necessary resources to the App.xaml, cause you want to change the resources of your App!
<Application x:Class="MetroDemo.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>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<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" />
<!-- Accent and AppTheme setting -->
<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>
Hope this helps.
Related
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"
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.
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" />
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.
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