The name ... does not exist in the namespace - c#

This problem is pretty much the same as this one. However my problem is that I get this error but it does compile run and find it find.
My XAML with the error is a resource dictionary (in a different project) and it is trying to use an enum called MainViewMode in a style. I have inherited the code from a previous team so I do not know why it was originally written like this but the xaml consists of all the path data for icons (using the <geometry> tag) as well as all of the Styles. I don't think this is relevant to the problem but just thought I would highlight it anyway.
As I said, when the program runs it does work fine however the designer mode is not working on one of the forms I need to modify and it is making it really annoying.
xmlns:cenum="clr-namespace:ABC;assembly=DEF"
...
<Style x:Key="MainViewToggleButtonStyle" TargetType="ToggleButton">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="CommandParameter" Value="{x:Static cenum:MainViewMode.Overview}" />
...
</style>
I have a resource dictionary, it calls an enum in a different project and says that it can't find it. It does however find it at runtime and work fine.
The main issue is that the designer doesn't load for any forms using this resource dictionary.

I believe your problem is that your namespace is incorrect. Take a look at the top answer in the question you linked to and notice how they have the example namespace. Since you're getting the enum from a different project you will also want to add that project as a reference.
To get the namespace right find the folder that the enum class is in, or if it's not in a folder just the project name. The namespace will be like one of the following,
If it's in a folder try something like this
ProjectName.FolderName;assembly=ProjectName
If not try something like this
ProjectName;assembly=ProjectName

Related

WPF font resouce not working, possible issue with font name

I am trying to use a font called Suisse Int'l Cond which is located in my project as fonts/SuisseIntlCond-Regular.otf and a build action of Resource
Other examples of OTF font in the project work without issue.
Works:
<Setter Property="FontFamily" Value="fonts/moon.otf #moon"/>
Dosn't Work:
<Setter Property="FontFamily" Value="fonts/SuisseIntlCond-Regular.otf #Suisse Int'l Cond"/>
My guess is it might be an issue with the ' char in the name or something wrong with the font itself but I have been unable to confirm either of these as the issue.
Importing and then exporting the font using Glyphr Studio resolves the issue even with keeping the same name.
How do I change a font's metadata (specifically a title)?

How do I dynamically change which Resource folder I get an image from?

I'm trying to implement 2 different themes for a WPF Application. I found this excellent post by #bendewey:
"Can WPF themes be used to include multiple skins for an application that can be changed at runtime?"
And have used it to implement two themes. I have created two Resource Folders, Lets call them ResourcesTheme1 and ResourcesTheme2 and Two Xaml Resource Dictionary files (Style1.xaml & Style2.xaml) that have the styles.
With this I have been able to set the following styles:
In Style1.xaml
<Style x:Key="HomeViewBackGroundImage" TargetType="Image">
<Setter Property="Source" Value="/ResourcesTheme1/Background.png" />
</Style>
In Style2.xaml
<Style x:Key="HomeViewBackGroundImage" TargetType="Image">
<Setter Property="Source" Value="/ResourcesTheme2/Background.png" />
</Style>
And this has worked a treat for me (if there's a better way please feel free to suggest it).
No the problem I have is that within my mainPage xaml I want to put on a row of buttons and each button has it's own image that it get's from binding to an ObservableCollection. I would like the Binding to look into the correct resource folder, but don't know how to make it happen without writing code behind.
So what I have is the following Binding:
<Image Name="ContentImage" Source="{Binding ImageName}" Stretch="UniformToFill">
And the following code behind:
if (useTheme1)
{
imageName = "/PlayingWithThemes;component/ResourcesTheme1/" + imageFileName;
}
else
{
imageName= "/PlayingWithThemes;component/ResourcesTheme2/" + imageFileName;
}
Any thoughts on how I could make the source for the image something like:
Source="ResourceFoler + {Binding ImageName}"
Or is there something more generic than:
/PlayingWithThemes;component/ResourcesTheme1/"
That I could use.
Thanks in advance.
A

WPF - Import custom fonts in C#

I would like to import custom fonts on my WPF application so that they work without having the client to install them.
All the answers I have found so far are in XAML, I would like to do it only in C#.
My fonts are in Resources/Fonts/.
I have already tried this :
Fonts.GetFontFamilies(new Uri("pack://application:,,,/Resources/Fonts/#"));
But it didn't work.
I did everything bluetoothfx said but it still did not work.
Then I changed the Build action of my fonts (it was to Content), to Embedded Resource, and it worked. Resource works also for me.
Thanks anyway.
I think the way that you are working will not work.
At first create a folder name fonts then Add the font to your project, change its Build Action to Content.
Now you need to find the internal name (Real name) of the font not the font-file name. You can have it by opening the font file and you can see it on top.
Now edit App.xaml
<Application.resources>
<style x:key="MYFONT_INTERNAL_NAME">
<setter property="TextElement.FontFamily"
value="pack://application:,,,/fonts/#MYFONT_INTERNAL_NAME" />
</style>
Now use it in your code like:
<TextBlock Style="{StaticResource MYFONT_INTERNAL_NAME}" FontSize="16" Text="Font Style" />
To know more search here:
http://www.alteridem.net/2014/02/24/custom-fonts-in-wpf-applications/

Trigger Properties in XAML

I am beginning to learn XAML and I am following along with the MCTS Self-Paced Training book. I've come to the section about triggers (more specifically Property Triggers) and I stumbled upon something that is rather annoying.
Here is my code:
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property=""
</Style.Triggers>
</Style>
Now, when I want to get the property that Trigger points to I want Intellisense to list all the available properties and it doesn't. Is that something I'm doing, a problem with VS, or is it just not supported in that scenario. Besides this, it works when I set the Setter for the Trigger. For example:
<Setter Property="INTELLISENSE WORKS" Value="Something" />
I'd really like for the properties of the Button to show up like IsMouseOver.
Check your version of visual studio and upgrade to Visual Studio 2010 Service Pack 1 that everything should work as expected (Y).
Visual Studio 2010 (without the service pack) does not have intellisense for triggers.

Cannot find a Resource with the Name/Key

I'm trying to unit test a user interface using the Silverlight 4 Toolkit.
When I attempt to instantiate the UserControl, it's throwing an exception because in the XAML of the UserControl it's using a Style defined App.xaml.
Is there a way to load the resource somehow before I instantiate the UserControl? Am I going about this the wrong way?
Here's the unit test code:
[TestMethod]
public void ExerciseTimePeriodUserInterface()
{
CustomUserControls.TimePeriodFilter timePeriodFilter = new CustomUserControls.TimePeriodFilter();
}
Here's the reference to the style in the UserControl:
<Border Style="{StaticResource FilterBorderWrapper}">
And lastly, here's the style defined in the App.xaml:
<Style TargetType="Border" x:Key="FilterBorderWrapper">
<Setter Property="Background" Value="#F1F5FB" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="#CBD9E9" />
<Setter Property="CornerRadius" Value="2" />
<Setter Property="Margin" Value="2" />
</Style>
If all your resources placed into ResorceDictionaries. You can simple create Application instance and add that Dictionary to the resources. Please look at the sample:
Application _app = new Application();
ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri("pack://application:,,,/Gui.Mvvm;component/Themes/YourResourceDictionary.xaml");
_app.Resources.MergedDictionaries.Add(dictionary);
For my WPF app this works fine. After this code was written I was able to test my Template Selectors, DataTemplate Selectors and so forth. All code using in codebehind calls to
Application.Current.FindResource()
works quite good.
You can't easily unit test User controls out of context. Too many dependencies.
You can test your view models with unit tests (which should be where all your code is anyway) and the Silverlight controls with some form of GUI automation (or humans if you can't afford the latest GUI test tools).
As VC 74 implied, if you are not already using MVVM, you probably should (if you want to do Silverlight unit testing).
Rick,
basically, I was getting the same error. Later then, i simply copied the Resources and all definitions to the Test-Projects App.xaml file (I also have a Styles.xaml resource), and my UI Tests work without problems.
Of course, it's never the best solution to copy "anything", but hey, I really don't care about the styles. Plus, you could even define own styles for the UI Testing Panel.
HTH
Thomas

Categories