I'm developing a c# Windows Phone 8.1 App in VC2013 and stumbled over an odd issue.
To make my Code more 'clean', I decided to put different parts of my App into different folders. There is no problem with calling XAML pages from these folders in the c# code.
BUT I seem to have issues linking these folders in XAML Code itself. For example I have following structure:
root, Files: App.cs+xaml, Mainpage.cs+xaml
|
-- Folder: Login
|
-- Files: LoginPage.cs+xaml
-- Folder: Converters
|
-- Files: converterClass.cs
To use the IValueConverter from the converterClass.cs like I always did, I put the following in the Header of my XAML file:
<Page
x:Class="myApp.Login.LoginPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:meinStundenplaner.Login"
xmlns:myConverter="using:myApp.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Styles/standardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<myConverter:DateTimeConverter x:Key="DateTimeConverter" />
... some more converters
</ResourceDictionary>
</Page.Resources>
...
Now it happens, that if I type <myConverter: the autocomplete does find my converter classes like usually, but when compiling (it is compiling) and testing on device none of the converters works and also I get an error in the Error-list saying
The name 'DateTimeConverter' in namespace 'using:myApp.Converters' does not exist.
Where did I go wrong?
wow ... after using VS2015 and the same error persisted, I played a little bit around with settings and stuff.
I kept being curious, why it would compile and deploy even it triggered so many errors. The problem appeared to be inconsistent.
The solution was different than expected:
You have to set Solution Platforms to x86 instead of ARM (which I needed to deploy on device). Luckily VS2015 has this option in a handy shortcut, what you can't say about VS2013.
Nevertheless it's solved...
Related
I'm having problems with loading resources (images, font families, styles) from an external resource dll. I would like to have all resources (images, fonts, styles...) in one resource dll and set them to be accessible over all projects (libraries) in this solution. Those projects in solution are different libraries that are referenced and called by main application.
So far i tried several different propositions how to do it but none of them work.
I'm using Visual studio 2019 and compiling for .net 4.6.2 - if that means something...
First I created an resource library called myapp.resources. Inside this project I have a folder named Fonts and inside that folder is the Lato-Thin font.
Also at the root of the project I created a resource dictionary called Fonts.xaml
-Project
-Fonts
-Lato-Thin.ttf
-Fonts.xaml
the structure of the Fonts.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myapp.resources">
<FontFamily x:Key="LatoThin">Fonts/#Lato Thin</FontFamily>
</ResourceDictionary>
In the main app App.xaml I added the loading of that ResourceDictionary:
<ResourceDictionary x:Key="Dict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/myapp.resources;component/Fonts.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
The last step is adding this fontfamily to label control:
<Label Grid.Row="0" Grid.Column="0" Content="some random text for test" FontFamily="{StaticResource ResourceKey=LatoThin}"/>
But I always get the error that the LatoThin font resource can't be located.
The same error I get with any other resource type like images, styles.
Of course I added the references to projects and every file is where it should be.
The only thing that is working is, for example adding image to buttons in this way.
<syncfusion:ButtonAdv x:Name="btnSelectFile"
Grid.Row="1" Grid.Column="2"
VerticalAlignment="Center" SizeMode="Small"
Height="26" Width="26" Label="Button" Margin="3,3,3,3"
SmallIcon="pack://application:,,,/myapp.resources;component/Images/add.png"/>
I tried to find some complete tutorials to follow but I always have the same error.
So, my question would be: How to solve this?
Thanks for any advice.
Make sure your font file (Lato-Thin.ttf) has its build action set to Resource.
In your Fonts.xaml make sure to use this format:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myapp.resources">
<FontFamily x:Key="LatoThin">/myapp.resources;component/Fonts/#Lato Thin</FontFamily>
</ResourceDictionary>
Tip: You can remove pack://application:,,, from your URIs to make them shorter.
I'm trying to use MaterialDesignXamlToolkit in my WPF class library (.NET framework). I'm following their official quick start tutorial, but since i do not have App.xaml, i had to make some adjustments. Apperently some step was wrong, but i do not know which one.
1) I installed MaterialDesignXamlToolkit using Nuget.
2) I created ResourceDictionary with the following code: (i specified the key because there is an error if i don't)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="123">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ResourceDictionary>
If i remove <ResourceDictionary x:Key="123"> element, then i get an error:
System.Windows.Markup.XamlParseException: Set property 'System.Windows.ResourceDictionary.Source' threw an exception.
FileNotFoundException: Could not load file or assembly 'MaterialDesignThemes.Wpf, Culture=neutral' or one of its dependencies.
3) My 'main screen' is Page, so i added the resource to it:
<Page.Resources>
<ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
</Page.Resources>
4) The obvious problem occurs here (this is the second step of the official tutorial): i add the following code to my Page:
<Page ...
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="13"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="{DynamicResource MaterialDesignFont}">
But i get a warning that: The resource {MaterialDesignBody, MaterialDesignPaper, MaterialDesignFont} could not be resolved.
Some of the solutions i tried pointed out that the ResourceDictionary's build action should be page, and it is.
Any help would be greatly appreciated!
The accepted solution worked for me. To avoid the dummy code though, I was also able to get MDXT working by adding the following to the code-behind of the resource dictionary:
Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignThemes.Wpf.dll"));
Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignColors.dll"));
Now that i've solved the problem, i realize one important information is missing from my question: i was following MVVM pattern (so all my code behind files were empty).
The problem was with the way Revit (the application that i was building a plugin for) loads libraries that a plugin is using. I still do not understand the internal logic of it, but the following two lines added to the code behind of the first page what is being loaded solved the problem for me:
ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Accent);
Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
I cannot stress enought that those two lines of code are a complite bullshit (since i do not want to place any logic to code behind), but the libraries won't otherwise be loaded. This code somehow 'forces' Revit to load Material design libraries (1st line of code uses MaterialDesignTheme.Wpf, and the 2nd MaterialDesignColors), since (i assume) it can already tell at compile time that those libraries are needed.
Remove the <ResourceDictionary x:Key="123"> element from your ResourceDictionary to begin with:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
You should then be able to set the properties using property element syntax after you have set the Resources property:
<Page ...
d:DesignHeight="450" d:DesignWidth="800">
<Page.Resources>
<ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
</Page.Resources>
<Page.Background>
<DynamicResource ResourceKey="MaterialDesignPaper" />
</Page.Background>
</Page>
Without adding those lines.
Double check if the MaterialDesign dll file get copied to the output path of the application.
I have seen such issue before, just adding nonsense code and Visual Studio realize your application that depends on your lib also depends on MaterialDesign lib and then copies the dll again as one would expect in the first place.
Instead of adding those lines you could then
Reference MaterialDesign directly in your application as well
Use a build event to make sure the DLL is copied to the build path.
This comment solves the problem for me,
but make sure you don't have another errors and if you have just find them and fix theme then try to run the project and it will work.
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
public MainWindow()
{
InitializeMaterialDesign();
InitializeComponent();
}
private void InitializeMaterialDesign()
{
// Create dummy objects to force the MaterialDesign assemblies to be loaded
// from this assembly, which causes the MaterialDesign assemblies to be searched
// relative to this assembly's path. Otherwise, the MaterialDesign assemblies
// are searched relative to Eclipse's path, so they're not found.
var card = new Card();
var hue = new Hue("Dummy", Colors.Black, Colors.White);
}
I have made several ResourceDictionaries for our applications team to use with their future applications. I have deployed the contents of the class library project containing these dictionaries to a .dll file and would like to be able to use the dictionaries by adding a reference to the .dll file in a new WPF solution where I hope to make a new application.
The Class Library in my example is called "NWF_Class_Library.dll" and is saved in the same folder in windows explorer as the MainWindow.xaml file. Is it possible to retrieve the resource dictionaries from within it?
I have read articles about the best way for an organisation to arrange their xaml resources, so it seems it must be possible, but all I find is ways to use the "//pack:application:..." syntax to reference xaml within the same solution as the wpf application. Here is a snippet of code, with the Source blank because nothing I have written has worked!
We had hoped that we could add the standard configurations as well as our more normal useful methods etc to a file that can be deployed with applications.
<Window x:Class="dll_ref_included.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary Source=""/>
</Window.Resources>
<Grid>
<Button Style="{StaticResource myButton}">This</Button>
</Grid>
</Window>
Try this:
<ResourceDictionary Source="pack://application:,,,/NWF_Class_Library;component/Dictionary1.xaml"/>
...where "NWF_Class_Library" is the name of the referenced assembly and "Dictionary1.xaml" is the name of the ResourceDictionary that is defined in this project.
You can refer to the documentation for more information about pack URIs and how to use them.
When you create a new WpfApplication project in Visual Studio you get the following XAML. Copying and pasting the URL http://schemas.microsoft.com/winfx/2006/xaml/presentation into the browser I expected to see the XSD file definition but I get an error. Why?
Thanks.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
The problem is most of wpf developer knows how it works but when you going to explain, it's become much difficult .. below is my try ... due to simplification it become large but i hope if you read to the end, you will understand how the definition thing works ..
Scenario:
I am a wpf beginner developer and searching for a wpf spinner on goggle. i got a link of font.awesome.wpf .. so i started to trying it. below code is written in document to add the spinner ..
<Window x:Class="DemoFontAwesome.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fa="http://schemas.fontawesome.io/icons/"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="20">
<fa:ImageAwesome Icon="AlignCenter" Spin="False" Height="48" Width="48" />
</Grid>
</Window>
Wow great .... It's working fine !!! ...
Suddenly!! i discover that i added a line there
xmlns:fa="http://schemas.fontawesome.io/icons/"
Not something like
xmlns:fa="clr-namespace:FontAwesome.WPF;assembly=FontAwesome.WPF"
then how visual studio knew which dll contain the ImageAwesome class!!! ... I added only FontAwesome.WPF.dll through nuget ..nothing else i did.. no additional xsd or xml file is there.. The schema link(http://schemas.fontawesome.io/icons/) is not available ...then how??
...Strange!!
However after 1 hours i ended up with below code..
<Window x:Class="DemoFontAwesome.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fa="http://schemas.fontawesome.io/icons/"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<fa:CssClassNameConverter Mode="FromIconToString" x:Key="sdfsdf"></fa:CssClassNameConverter>
</ResourceDictionary>
</Window.Resources>
<Grid Margin="20">
<fa:ImageAwesome Icon="AlignCenter" Spin="False" Height="48" Width="48" />
</Grid>
</Window>
The noticable part is fa:ImageAwesome and fa:CssClassNameConverter classes... They are from different namespace (using code behind i already checked it).. and i did not specified one extra line to specify any of FontAwesome.WPF or FontAwesome.WPF.Convertersnamespace.. then how the magic going on!! ..
Solution:
So i downloaded the source code of font.awesome.wpf .. and started search for the text http://schemas.fontawesome.io/icons/ there ... and finally i found the below lines in assembly.cs of font.awesome.wpf project..
[assembly: AssemblyVersion("4.5.0.*")]
[assembly: AssemblyFileVersion("4.5.0.7")]
[assembly: XmlnsPrefix("http://schemas.fontawesome.io/icons/", "fa")]
[assembly: XmlnsDefinition("http://schemas.fontawesome.io/icons/", "FontAwesome.WPF")]
[assembly: XmlnsDefinition("http://schemas.fontawesome.io/icons/", "FontAwesome.WPF.Converters")]
And the whole thing (the magic trick!!) revealed to me ..
In assembly.cs file the component defined the http://schemas.fontawesome.io/icons/ namespace .. so when i add fontawesome.wpf dll ... visual studio got it's namespace definition using refection .. and so how vs knows where the fa tag refers to ... So this is how it resolved to me... :)
Some theory
XML namespace name doesn’t match any particular .NET namespace. There are a
couple of reasons the creators of XAML chose this design. By convention, XML namespaces are often
uniform resource identifiers (URIs) as they are here. These URIs look like they point to a location on the
Web, but they don’t. The URI format is used because it makes it unlikely that different organizations will
inadvertently create different XML-based languages with the same namespace. Because the domain
schemas.microsoft.com is owned by Microsoft, only Microsoft will use it in an XML namespace name.
The other reason that there isn’t a one-to-one mapping between the XML namespaces used in XAML
and .NET namespaces is because it would significantly complicate your XAML documents. The problem
here is that WPF encompasses well over a dozen namespaces (all of which start with System.Windows). If
each .NET namespace had a different XML namespace, you’d need to specify the right namespace for each
and every control you use, which quickly gets messy. Instead, the creators of WPF chose to combine all of
these .NET namespaces into a single XML namespace. This works because within the different .NET
namespaces that are part of WPF, there aren’t any classes that have the same name.
The namespace information allows the XAML parser to find the right class. For example, when it looks
at the Window and Grid elements, it sees that they are placed in the default WPF namespace. It then
searches the corresponding .NET namespaces until it finds System.Windows.Window and System.
Windows.Controls.Grid
A namespace is a URI (a URN or a URL), but a URI is not always a URL. The URI used for namespaces is meant to uniquely identify names to prevent clashed. The XML Namespaces Working Group at the time decided to use a technique already know to uniquely identify things: URIs.
As a result, many people think it should actually point to something real. Occasionally that is true, but more often it is not, and it is not meant to be. It is an identitifier, not a location.
In the case of schemas, it is can be used to denote the target namespace, which is the namespace that must be used in documents that need to validated against the schema. To obtain the schema, you will have to ask the vendor. In this case, the schema can be found at a location similar or equal to C:\Program Files (x86)\Microsoft Visual Studio 10.0\Xml\Schemas, look for wpfe.xsd (however, to confuse matters more, Microsoft has decided to create an alias for the target namespace, which is why you will not see the same namespace you mentioned).
I feel like I must be going crazy, but I just changed the name of a property in a view model (a C# file) from Width to Size using Visual Studio's refactor-rename feature. When I was done, this error appeared in the Error window:
Error 2: The property 'Size' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
The error was referring to a XAML UserControl file. When I checked the file to see what was up, I realized that every attribute named Width had been changed to Size.
Example:
<UserControl x:Class="ApbSymbolGenerator.Views.Symbol"
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"
mc:Ignorable="d"
Size="{Binding Size}"
Height="{Binding Size}">
This is the first time I've ever had refactor-rename cause a change to a XAML file (besides the x:class value). (Note: I did not do a global find/replace, I did refactor-rename. I undid everything and performed the rename again, and it did the same thing.)
Strangely, it only affected one of several XAML files in my app that has a Width property.
Any explanation what could be going on here?
Looks like this is a bug (that won't be fixed) - Refactor Rename Bug