Cannot access resource defined in app.xaml - wpf

I am using Visual Studio 2010 RC1.
I define a resource "Brush2" in app.xaml_:
<Application x:Class="VideoThumbnails.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<RadialGradientBrush x:Key="Brush2" RadiusX="1" RadiusY="1" GradientOrigin="0.3,0.3">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#ffc0c0" Offset="1"/>
</RadialGradientBrush>
</Application.Resources>
</Application>
In my Mainwindow I am trying to use that resource:
...
<Border Margin="4,2" BorderBrush="Black" BorderThickness="2" CornerRadius="4"
ToolTip="{Binding Path=FullPath}" HorizontalAlignment="Stretch"
Background="{StaticResource Brush2}">
...
No matter what I do it always raises an exception at runtime (Resource not found). I changed
build action without success.
How can I use resources defined in app.xaml?

If you've set the Startup Object to a custom class you need to create the custom Application class and also call its InitializeComponent method, like this:
App app = new App();
app.InitializeComponent();
Update: As #qqww2 suggested the InitializeComponent call can be moved inside the App class constructor.

I start my application not with a StartupUri in App.xaml, but with a event handler in App.xaml.cs.
When using override Startup() resources from App.xaml are not loaded:
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
...
}
}
But when using event Startup the resources are loaded just fine:
<Application x:Class="OxyplotTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="OnStartup">
And code behind:
public partial class App
{
private void OnStartup(object sender, StartupEventArgs e)
{
...
}
}

Nothing you have done is incorrect. You either have 1) screwed up the project build somehow while randomly doing things to try to get it to work or 2) something else is going on here and we'll never know without the exception details.
I would highly suggest you try to repro this in a fresh brand new WPF project. Do the following steps (and ONLY the following steps):
Create a new WPF project, add the exact same brush to app.xaml, then open Window1 and bind the window's background to the resource. Run the app.
It should work as expected. If not, come back with the exception details. If it does, compare this new project with your current one to see what you are doing differently.

I know there is an already accepted answer, but I thought I would add my solution as well. I had code that was working, but some configuration change broke the resource references in the designer. In executing code, it worked fine.
After some initial research, I determined that the BuildAction property for App.xaml should be set to ApplicationDefinition. My was set to Page. However, that causes some issues with multiple entry points. Main() was already defined in App.xaml.cs. The compile error was indicating another entry point in App.g.cs (which is an autogenerated file).
I ended up using the approach #3 described at http://www.infosysblogs.com/microsoft/2008/09/how_to_write_custom_main_metho.html. The basic idea is that you create a new class that is only responsible for startup. In my case, I named it Startup.cs. It should have code that is similar to this:
using System.Threading;
namespace MyNamespace
{
public class Startup
{
[System.STAThreadAttribute()]
private static void Main()
{
var app = new App();
app.InitializeComponent();
app.Run();
}
}
}
Then in the project settings, change the Application -> Startup object so that your new class is selected.

I had a similar problem and solved it, so I figured I may as well post my solution. I kept getting the Resource not found error only at runtime as described above. In my Windows 8.1 c# App, I was using a style I had defined, and it showed up fine in Blend and the designer view, but didn't work at runtime. I was trying to use this style in a SettingsFlyout I had created following these instructions. After getting that to work, I set up a field in App.xaml to hold onto my flyouts (Preferences and ColorSettings) so I wouldn't be making a new one every time.
public static Preferences preferences;
public static ColorSettings colorsettings;
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
preferences = new Preferences();
colorsettings = new ColorSettings();
}
After poking around and googling for about an hour, I figured out that I was creating the flyouts too early, and when they were created they couldn't access the application's resources. So I moved their creation down to App.OnLaunched() and that solved the problem.
I'm not sure if that's the best way to go about things, but it worked. So, try to pinpoint where you're trying to access the resources you want, and if you're maybe trying too early. Sorry for the vagueness and maybe incorrectness, I'm really new to WPF.

I can concur that resources get messed up easily if you have something in App constructor. Move initialization of your own global objects into OnStartup method:
protected override void OnStartup(StartupEventArgs e)
{
}

Related

Window is not recognizing an application resource

To reproduce the error:
Create a new MVVM-Light WPF application.
Copy MainWindow.xaml to MainWindow2.xaml Rename MainWindow2's class name to MainWindow2 (and the constructor)
Rename MainWindow2 window class attribute to "x:Class="MvvmLight2.MainWindow2"
Remove StartupUri from App.xaml
Add the following to App:
protected override void OnStartup(StartupEventArgs e)
{
new MainWindow().Show();
new MainWindow2().Show();
}
Run the application and get error:
Cannot find resource named '{Locator}'. Resource names are case sensitive. Error at object 'System.Windows.Data.Binding' in markup file 'MvvmLight2;component/mainwindow.xaml' Line 10 Position 9.
To resolve the error:
Remove DataContext="{Binding Main, Source={StaticResource Locator}}" from both windows.
Add the following line to both windows' constructors:
DataContext = new ViewModelLocator().Main;
The application now runs.
The question is why doesn't it recognize the Locator even though it's defined as an application resource?
Update:
I just noticed that I can add the same resource on both xaml and code without any visible side effects. The question now becomes, is there a problem with this? Does it create a duplicate resource or it doesn't because they have the same key?
More than just hacking it, I'm trying to understand what's going on.
Managed to fix this by adding InitializeComponent() inside Application.Startup event handler:
App.xaml
<Application x:Class="SomeNamespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup"> <!-- Important to use Startup -->
App.xaml.cs
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
InitializeComponent(); // <-- Important to set this!
var window = new MainWindow();
window.Show();
}
}

Memory leak when using SharedResourceDictionary

if you worked on some larger wpf applications you might be familiar with this. Because ResourceDictionaries are always instantiated, everytime they are found in an XAML we might end up having one resource dictionary multiple times in memory. So the above mentioned solution seems like a very good alternative. In fact for our current project this trick did a lot ... Memory consumption from 800mb down to 44mb, which is a really huge impact. Unfortunately this solution comes at a cost, which i would like to show here, and hopefully find a way to avoid it while still use the SharedResourceDictionary.
I made a small example to visualize the problem with a shared resource dictionary.
Just create a simple WPF Application. Add one resource Xaml
Shared.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="myBrush" Color="Yellow"/>
</ResourceDictionary>
Now add a UserControl. The codebehind is just the default, so i just show the xaml
MyUserControl.xaml
<UserControl x:Class="Leak.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:SharedResourceDictionary="clr-namespace:Articy.SharedResourceDictionary" Height="128" Width="128">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Leak;component/Shared.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Rectangle Fill="{StaticResource myBrush}"/>
</Grid>
</UserControl>
The Window code behind looks something like this
Window1.xaml.cs
// [ ... ]
public Window1()
{
InitializeComponent();
myTabs.ItemsSource = mItems;
}
private ObservableCollection<string> mItems = new ObservableCollection<string>();
private void OnAdd(object aSender, RoutedEventArgs aE)
{
mItems.Add("Test");
}
private void OnRemove(object aSender, RoutedEventArgs aE)
{
mItems.RemoveAt(mItems.Count - 1);
}
And the window xaml like this
Window1.xaml
<Window.Resources>
<DataTemplate x:Key="myTemplate" DataType="{x:Type System:String}">
<Leak:MyUserControl/>
</DataTemplate>
</Window.Resources>
<Grid>
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Content="Add" Click="OnAdd"/>
<Button Content="Remove" Click="OnRemove"/>
</StackPanel>
<TabControl x:Name="myTabs" ContentTemplate="{StaticResource myTemplate}">
</TabControl>
</DockPanel>
</Grid>
</Window>
I know the program is not perfect and propably could be made easier but while figuring out a way to show the problem this is what i came up with. Anyway:
Start this and you check the memory consumption, if you have a memory profiler this becomes much easier. Add (with showing it by clicking on the tab) and remove a page and you will see everything works fine. Nothing leaks.
Now in the UserControl.Resources section use the SharedResourceDictionary instead of the ResourceDictionary to include the Shared.xaml. You will see that the MyUserControl will be kept in memory after you removed a page, and the MyUserControl in it.
I figured this happens to everything that is instantiated via XAML like converters, user controls etc. Strangely this won't happen to Custom controls. My guess is, because nothing is really instantiated on custom controls, data templates and so on.
So first how we can avoid that? In our case using SharedResourceDictionary is a must, but the memory leaks makes it impossible to use it productively.
The Leak can be avoided using CustomControls instead of UserControls, which is not always practically. So why are UserControls strong referenced by a ResourceDictionary?
I wonder why nobody experienced this before, like i said in an older question, it seems like we use resource dictionaries and XAML absolutely wrong, otherwise i wonder why they are so inefficent.
I hope somebody can shed some light on this matter.
Thanks in advance
Nico
I'm running into the same issue of needing shared resource directories in a large-ish WPF project. Reading the source article and the comments, I incorporated a couple fixes to the SharedDirectory class as suggested in the comments, which seem to have removed the strong reference (stored in _sourceUri) and also make the designer work correctly. I tested your example and it works, both in the designer and MemProfiler successfully noting no held references. I'd love to know if anyone has improved it further, but this is what i'm going with for now:
public class SharedResourceDictionary : ResourceDictionary
{
/// <summary>
/// Internal cache of loaded dictionaries
/// </summary>
public static Dictionary<Uri, ResourceDictionary> _sharedDictionaries =
new Dictionary<Uri, ResourceDictionary>();
/// <summary>
/// Local member of the source uri
/// </summary>
private Uri _sourceUri;
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get {
if (IsInDesignMode)
return base.Source;
return _sourceUri;
}
set
{
if (IsInDesignMode)
{
try
{
_sourceUri = new Uri(value.OriginalString);
}
catch
{
// do nothing?
}
return;
}
try
{
_sourceUri = new Uri(value.OriginalString);
}
catch
{
// do nothing?
}
if (!_sharedDictionaries.ContainsKey(value))
{
// If the dictionary is not yet loaded, load it by setting
// the source of the base class
base.Source = value;
// add it to the cache
_sharedDictionaries.Add(value, this);
}
else
{
// If the dictionary is already loaded, get it from the cache
MergedDictionaries.Add(_sharedDictionaries[value]);
}
}
}
private static bool IsInDesignMode
{
get
{
return (bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
typeof(DependencyObject)).Metadata.DefaultValue;
}
}
}
I am not quite sure if this will solve your issue. But I had similar issues with ResourceDictionary referencing controls and its to do with lazy hydration. Here is a post on it. And this code resolved my issues:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
WalkDictionary(this.Resources);
base.OnStartup(e);
}
private static void WalkDictionary(ResourceDictionary resources)
{
foreach (DictionaryEntry entry in resources)
{
}
foreach (ResourceDictionary rd in resources.MergedDictionaries)
WalkDictionary(rd);
}
}

Application.LoadComponent makes application freeze on shutdown

If I use Application.LoadComponent() to load a UserControl, Page or Window, my application freezes when I try to close it.
The app apparently closes, but the process keeps running. Easy to notice when debugging.
I've tested it under Windows 7 64bit and Vista 32bit. In both cases I have used VS2008 and .NET 3.5.
A repro can be built by creating a wpf application as follows:
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}
public void LoadCopy() {
var uri = new Uri("/WpfApplication1;component/window1.xaml", UriKind.Relative);
var copy = (Window)Application.LoadComponent(uri);
MessageBox.Show(copy.Title);
}
private void Button_Click(object sender, EventArgs e) {
LoadCopy();
}
}
Does anyone know what might be happening? And how to solve it?
Try assigning the owner to the created assembly i.e.
copy.Owner = this;
I was able to close your example after doing this.
I think it is because you are calling LoadComponent() on what is also your Main Window ( http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow.aspx ), i.e. the startup uri, in your case Window1. The program is probably entering some loop when you close it because closing a Main Window by default shuts down the Application and your two instances of Window1 are waiting on each other (A.K.A. a deadlock)! Albeit seemingly only after making the Application invisible (so it seems to have closed).
If you still must use use LoadComponent() on Window1 I think you would need to not make it your startup uri by changing the StartupUri of your Application:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"> <!-- change this -->
</Application>
Or change Application.ShutdownMode ( http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx ) to OnLastWindowClose:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"
ShutdownMode="OnLastWindowClose">
</Application>
I have build you application on Windows 7 32bit under .Net 4.0 and 3.5.
I works fine for me. I think you problem is configuration specific.
Which configuration do you have? Do you reference any assemblies except default WPF project references?

Cannot set startup object in WPF VB.NET Project

I am using VS2010 VB.NET, working on a solution that has a number of projects. I have been developing on it for a while now, and in an attempt to debug a custom class inherited from ObservableCollection (which by the way would not load symbols when debugging even though it was apparent that the breakpointed line was being called), I changed the startup object for the startup project to a different WPF window which I had a couple of controls that I set aside for debugging.
Immediately I was confronted with 'Sub Main' was not found in . I tried changing the startup object back to the normal startup window, but now the Startup Object dropdown only has "Sub Main" as it's only option. I changed the StartupURI back in the App.xaml, to no avail.
Anyone else seen this?
How can I get it back to using the original window?
As a side note, is there a setting somewhere that would cause the debugger not to load symbols for an assembly? I know the DiskCollection class is being instantiated, but a breakpoint on the constructor always says Breakpoint cannot be hit, No Symbols loaded.
Cory
The startup in WPF is different then in winforms; it's set by the App.xaml file. Edit that in xaml mode and you will notice this:
<Application x:Class="WpfApplication6.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
The StartupUri sets which form will start things.
If you wanted code to kick things off instead you can remove the StartupUri and do this instead:
Startup="Application_Startup"
Then provide the code in the App.xaml.cs file like so:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
}
}
So aparently in the Project Properties page, the Enable application framework setting was unchecked. this apparently tells the compiler to use the StartupUri attribute to determine the startup page instead of using the main sub (or method; does this setting appear in C#?).
Somehow the setting was unchecked and so Sub Main was the only option in the StartupURI dropdown, the Windows Application Framework Propertie3s group was disabled, and the StartupUri attribute in the app.xaml was unused.
Now I know...
Cory

How to properly use a Subclassed silverlight control?

I created this class where I wanted to override this specific event for the content control.
public class MyContentControl : ContentControl
{
protected override void OnKeyUp(KeyEventArgs e)
{
//do something
//..........
e.Handled = true;
}
}
However when I am using this control in my XAML everything compiles and works fine, but I am getting a runtime exception and I am not sure exactly why? Could someone point me in the right directions....
The exception I get is
XamlParseException
UPDATED (Shows where abouts of the exceptions)
In the XAML I have:
xmlns:ctrls="clr-namespace:SilverlightProject.CustomControls"
and I use it like:
<ctrls:MyContentControl Grid.Column="0" x:Name="_contentControl" VerticalAlignment="Center" HorizontalAlignment="Center" />
Content controls require a default template to be created somewhere (e.g. in generic.xaml). They do not have a matching XAML file like user controls.
If you can provide more information, I can show you how to create an appropriate template.
There are many reasons for this.
To start with. Give the fully qualified namespace.
Instead of
xmlns:ctrls="clr-namespace:SilverlightProject.CustomControls"
Use
xmlns:ctrls="clr-namespace:SilverlightProject.CustomControls;assembly=YourAssebmlyName"
If that, doesn't help.
You directly attach your control with the application. Then, enable Debug -> Exceptions. Check Thrown option for the CLR execetion checkbox. You have fair amount of possibility to get to know the cause.

Resources