VS 2015 WPF: Dictionary causes Window to freeze / Namespaces not created - c#

I'm toying arround a bit with the new VS 2015, and I found two strange behaviors, I really can't explain. Probably you can help me, if this are simple bugs or Im doing something generally wrong:
Í have a simple Project, the Mainwindow has just a Datagrid:
<Grid>
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Test" />
</DataGrid.Columns>
</DataGrid>
</Grid>
In the App.XAML I load a Dictionary in a Subfolder:
<Application.Resources>
<ResourceDictionary Source="Dictionaries\AppDictionary.xaml" />
</Application.Resources>
The Dictionary is looking like this:
<Style TargetType="Grid">
<Setter Property="Margin" Value="10" />
</Style>
Now, as soon as I load the Window, it seems to be frozen forever. I tested some Grid-Properties, but this happens only, when I set the Margin it seems.
I checked the Inheritance from DataGrid or DataGridTextColumn, but they don't seem to inherit from the Grid anyhow.
They second exception:
I need to create a Class in the Dictionaries-Subfolder, otherwise i keep getting the exception:
The type or
namespace name 'Dictionaries' does not exist in the namespace
'WpfApplication3' (are you missing an assembly
reference?)
I didn't work with WPF for a while, but I'm almost certain, I didn't have such problems before, but I might be wrong on this case.
Edit: When I break the Debugging I get the Message:
"Your app has entered a break state, but there is no code to show because all threads where executing external code".

Your information doesn't seem complete but It seems that you are not building the Dictionaries\AppDictionary.xaml file. You need to set its Build Action to Resource.
Also, your dictionary should look like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionaries\AppDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Related

Custom Font working in Uno.WASM, but not working in Uno.UWP

Since i have not seen something helpful after searching for a while, i decided to ask here:
I have a small testing Project, just to import a custom Icon Font from the interwebs.
Current Proedure is the following:
my MainApp.xaml consists of a single Textblock, which is referencing a Style-element in Styles.xaml, which in turn references a FontFamily where i load the ttf file per platform.
In WASM it works great (base64 URI), but in UWP i just cant get the font-icon to display at all.
The Style.xaml gets imported perfectly by the App.xaml, the Size gets applied, but it seems there is something wrong with the FontFamily-Tags.
I tried:
Installing my font by hand => Worked like a charm. So its probably not the font file?
https://platform.uno/docs/articles/features/custom-fonts.html , but it didn't help.
https://blog.mzikmund.com/2020/01/custom-fonts-in-uno-platform/ which in turn, didn't change much.
https://github.com/MartinZikmund/blog-2020/tree/master/Uno.CustomFonts even after cross-referencing here, i couldn't get it to work.
https://github.com/unoplatform/calculator/blob/uno/src/Calculator.Shared/Styles.xaml i also cross-checked with the official calculator-port, but even after setting things up like they did, nothing changed.
Reinstall VS (i use the Community Version).
Here's my Code:
MainApp.xaml:
<Page
x:Class="Testing.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Testing"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="L" Style="{ThemeResource IconTextStyle}" />
</Grid>
</Page>
Styles.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:xamarin="http://uno.ui/xamarin"
xmlns:macos="http://uno.ui/macos"
xmlns:wasm="http://uno.ui/wasm"
xmlns:skia="http://uno.ui/skia"
xmlns:win="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"
mc:Ignorable="xamarin wasm macos skia"
>
<win:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</win:FontFamily>
<macos:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</macos:FontFamily>
<wasm:FontFamily x:Key="IconScratchedFontFamily">Small-Icons-Scratched</wasm:FontFamily>
<skia:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</skia:FontFamily>
<win:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</win:FontFamily>
<macos:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</macos:FontFamily>
<wasm:FontFamily x:Key="IconClearFontFamily">Small-Icons-Free</wasm:FontFamily>
<skia:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</skia:FontFamily>
<Style TargetType="TextBlock" x:Key="IconTextStyle">
<Setter Property="FontFamily"
Value="{StaticResource IconScratchedFontFamily}" />
<Setter Property="FontWeight"
Value="Normal" />
<Setter Property="FontSize"
Value="116" />
</Style>
</ResourceDictionary>
App.xaml:
<Application
x:Class="Testing.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Testing">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Found the solution:
<win:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#PWSmallIcons</win:FontFamily>
<macos:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#PWSmallIcons</macos:FontFamily>
Background: The part after the # indicates the name of the font, as it is written in the .ttf File, not name of the Font as I want to reference it, which is pretty obvious if you think about it.
It was nearly impossible to figure out with my provided code, so I decided to update my question for anyone else running into this problem.
For Win and macOS the way to define the FontFamily in the Resources is by using just the FontName that in your case it would be something like:
<win:FontFamily x:Key="IconScratchedFontFamily">Small-Icons-Scratched</win:FontFamily>
<macos:FontFamily x:Key="IconScratchedFontFamily">Small-Icons-Scratched</macos:FontFamily>
and
<win:FontFamily x:Key="IconClearFontFamily">Small-Icons-Free</win:FontFamily>
<macos:FontFamily x:Key="IconClearFontFamily">Small-Icons-Free</macos:FontFamily>
With this, you should be able to use your FontFamily definitions in any of your Styles.
Hope this helps.-

How to include MaterialDesignXamlToolkit to WPF class library?

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);
}

Using resource dictionary from a separate file gives me a designer error: Failed to set Source

I'm trying to add a resource dictionary to a page but I keep getting a designer error in my xaml. The app runs fine with no issue but the designer error bothers me.
Here's how I did it. Both works fin at run time. But it's saying Failed to set "Source". Any clues?
That also gives an error to all the static resource I used from the resource dictionary.
Update:
This is another approach. Instead of directly adding it to the Page's resources, I added it to the Application.Resources still cant resolve the styles. I'm using VS2017 v15.4.4
Steps to reproduce:
List item
Create a new UWP Application Project (App1)
Create a new UWP Class Library (ClassLibrary1)
In ClassLibrary1, add a new ResourceDictionary (Dictionary1.xaml).
In Dictionary1.xaml, add a Style, let's say a button style.
<Style TargetType="Button" x:Name="ButtonStyle1" x:Key="ButtonStyle1">
<Setter Propeprty="Background" Value="Red" />
</Style>
In the ClassLibrary1, add a new BlankPage (BlankPage1)
In the BlankPage1, add a button and use ButtonStyle1
`
In App1, merge the resource dictionary to App.Resources (inside App.xaml).
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionary>
<ResourceDictionary Source="ms-appx:///ClassLibrary1/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionary>
</ResourceDictionary>
</Application.Resources>
Make the BlankPage1 the start page, go to App.xaml.cs and and change MainPage to BlankPage1:
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(BlankPage1), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
Run the application. It will run just fine. But the problem is in the designer where it cannot recognize the style from the resource dictionary.
I followed your steps and found a typo and an issue.
The typo: In Style step (Perhaps only in the sample), There is typo in the word 'Proeprty':
<Style TargetType="Button" x:Name="ButtonStyle1" x:Key="ButtonStyle1">
<Style.Setters>
<Setter Property="Background" Value="Red" />
</Style.Setters>
</Style>
The issue in the way you are merging dictionary, it should be using the following syntax:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
After fixing both issues, Visual studio designer didn't complain about anything.
Did it fix the issue for you? If not then your steps to reproduce aren't complete.

UWP - Reference StaticResource from different style resource dictionary: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'

Hello my team and I recently started developing an win10 uwp application. Application will have a lot of views and components so heavy use of styles is expected, so we need to organize our styles through file/folder structure we did this using following structure (unfortunately I cannot embed images yet see the link):
Anyways my Resource.xaml merges all other dictionaries as following:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Colors.xaml" />
<ResourceDictionary Source="/Resources/Icons.xaml" />
<ResourceDictionary Source="/Resources/Fonts.xaml" />
<ResourceDictionary Source="/Resources/Converters.xaml" />
<ResourceDictionary Source="/Resources/Buttons.xaml" />
<ResourceDictionary Source="/Resources/RadioButton.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
And in my App.xaml I reference this dictionary:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
Now I managed to find the source of the problem in my RadioButton.xaml I reference a brush defined in Colors.xaml using StaticResource lookup:
<Setter Property="Foreground" Value="{StaticResource TopMenuTextBrush}" />
If I remove this line everything will start but with it I get following exception:
Exception {Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.
Failed to assign to property
'Windows.UI.Xaml.ResourceDictionary.Source' because the type
'Windows.Foundation.String' cannot be assigned to the type
'Windows.Foundation.Uri'. [Line: 28 Position: 37]} System.Exception
{Windows.UI.Xaml.Markup.XamlParseException}
Interesting thing is when I start the app with this line commented and uncomment it visual studio will recognize the brush and apply it correctly, it only breaks on application start.
We used same approach before when developing WPF, so I'm thinking it might have to do with something regarding application deployment.
All help is greatly appreciated.
Exception = {Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.
The problem is that you have used wrong ResourceDictionary source . I found the Resources.xaml and other xaml file stored in the same level directory in your screenshot. So you could not declare the parent directory of these xaml files within source. Please modify ResourceDictionary like the following
<ResourceDictionary Source="Colors.xaml"/>
For more you could refer to ResourceDictionary and XAML resource references.

WPF Ribbon Changing Theme

Hi I am creating my first WPF application that uses Ribbons. The problem I am having is I cannot change the theme of the ribbons. I want to use the Office 2007 Black theme. I have looked everywhere and everywhere I find roughly the same example, I have tried writing it from scratch, copy pasting and everything but it keeps throwing an exception when I try t run the application.
Exception
Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '11' and line position '18'.
Inner Exception
{"Cannot locate resource 'themes/office2007black.xaml'."}
Xaml Code
<r:RibbonWindow x:Class="DtcInvoicer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Title="Dtc Invoicer"
Width="1000" Height="650" WindowStartupLocation="CenterScreen">
<r:RibbonWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/RibbonControlsLibrary;component/Themes/Office2007Black.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</r:RibbonWindow.Resources>
<Grid x:Name="LayoutRoot">
<r:Ribbon x:Name="TopMenuRibbon" Title="First Ribbon">
<r:RibbonTab Header="Home" />
<r:RibbonTab Header="Company" />
<r:RibbonTab Header="Employees" />
<r:RibbonTab Header="Receivables" />
<r:RibbonTab Header="Payables" />
<r:RibbonTab Header="Inventory" />
<r:RibbonTab Header="Settings" />
</r:Ribbon>
</Grid>
</r:RibbonWindow>
Anyone have idea why that might be happening? The RibbonControlsLibrary is in my references, everything works fine and the ribbons come up in the blue theme until I try to switch to black or silver, then I get the exception.
Resources that are specific to a theme are kept in a resource dictionary with a specific file name. These files must be in a folder named Themes that is a subfolder of the folder that contains the control.
See this MSDN article for more details.

Categories