Type module:Header not found in xmlns clr-namespace:MyModule - xaml

I am trying to dynamically load an MAUI class library using Assembly.Load and open a page from it. This page references another ContentView. It works perfectly fine when running in debug mode, but when just starting the app manually (still compiled using the Debug configuration) it crashes with the following exception:
android.runtime.JavaProxyThrowable: Microsoft.Maui.Controls.Xaml.XamlParseException: Position 11:14. Type module:Header not found in xmlns clr-namespace:MyModule
at Microsoft.Maui.Controls.Xaml.CreateValuesVisitor.Visit(ElementNode node, INode parentNode)
at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode rootnode, HydrationContext visitorContext, Boolean useDesignProperties)
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties)
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Boolean useDesignProperties)
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, Type callingType)
at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[Home](Home view, Type callingType)
at MyModule.Home.InitializeComponent()
Here is the Home page:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:module="clr-namespace:MyModule"
x:Class="MyModule.Home"
BackgroundColor="White"
NavigationPage.HasNavigationBar="False">
<ContentPage.Content>
<StackLayout>
<module:Header />
</StackLayout>
</ContentPage.Content>
</ContentPage>
And here is the ContentView that is being referenced:
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyModule.Header">
<StackLayout Orientation="Horizontal" HeightRequest="100" >
<Image Source="mylogo.png" HorizontalOptions="Start" Margin="10"></Image>
</StackLayout>
</ContentView>
I also have the same error when running for windows, but on windows the error occurs whether I am debugging or not:
Type module:Header not found in xmlns clr-namespace:MyModule
I have tried adding [assembly: XamlCompilation(XamlCompilationOptions.Compile)] but it also didn't change anything.
I specifically do not want to reference the module project directly and would like to load and register everything dynamically.

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.

How to use ThemeManager in code behinde

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.

How to resolve an entry point path error?

Issue:
I've moved the views in my application to a new folder named, 'Views'. But after moving the views I noticed I'm getting a navigation_failed event.
Debugging Steps:
In order to debug this I set a breakpoint on the exception message within the event. This then lead me to cause of the error, the compiler can't find the location of the moved view.
I tried to resolve this by updating the entry point and NavigationPage paths in the AppManifest.xml. To the new path of the view and the exception points that this path isn't correct.
Error: "No XAML was found at the location '/ParkingTagPicker.Views.MainPage'."
I updated the entry point here in the manifest file:
EntryPoint="ParkingTagPicker.Views.MainPage"
Question:
Can anyone advise on the correct path string in this case?
My MainPage.xaml location is as follow in the solution:
<phone:PhoneApplicationPage x:Class="ParkingTagPicker.Views.MainPage"
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:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
SupportedOrientations="Portrait"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
shell:SystemTray.IsVisible="True"
mc:Ignorable="d">
When a page is in a subfolder, you must specify the full path for it, for example "Views/MainPage.xaml"
Same is valid when navigating between pages in the app, for example:
NavigationService.Navigate(new Uri("/Pages/ChartComparePage.xaml", UriKind.Relative));

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