Window is not recognizing an application resource - wpf

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

Related

How to customize startup of WPF application?

When a new WPF Application project is created, MainWindow.xaml, App.xaml and their corresponding code behind classes are automatically generated. In the App.xaml there is an attribute that defines which window is going to be run initially and by the default it's StartupUri="MainWindow.xaml"
I have created a new Dispatcher class in the same project. At startup, I want the instance of that class Dispatcher to be constructed and then one of its method to run. That method would actually create and show the MainWindow window. So how do I modify the App.xaml or App.xaml.cs in order to make it happen? Or, if it cannot be done by App, how should I implement it? Thanks.
You can remove the StartupUri attribute from the App.xaml.
Then, by creating an override for OnStartup() in the App.xaml.cs, you can create your new instance of your Dispatcher class.
Here's what my quick app.xaml.cs implementation looks like:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
new MyClassIWantToInstantiate();
}
}
}
Update
I recently discovered this workaround for a bug if you use this method to customize app startup and suddenly none of the Application-level resources can be found.
Try to use the Startup event (class Application) - MSDN.
You can show MainWindow in this event handler - after you create a Dispatcher instance.
1.In App.xaml, To replace the StartupUri with a subscription to the Startup event.
Use the event in App.xaml.cs .
For instance,
Startup="Application_Startup" in .xaml.
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
// Create the startup window
MainWindow wnd = new MainWindow();
// Do stuff here, e.g. to the window
wnd.Title = "Something else";
// Show the window
wnd.Show();
}
}

Throw exception when access image from App.xaml.cs

First, try this.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="80" Width="100" Loaded="Window_Loaded">
<Image x:Name="image"/>
</Window>
and
public partial class MainWindow : Window
{
public static BitmapImage okImage = new BitmapImage(new Uri("pack://Application:,,,/ok.png"));
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
image.Source = okImage;
}
}
REMEMBER put any image named ok.png to the project directory. This runs well and shows an image in the window.
Now try to move public static BitmapImage okImage = new BitmapImage(new Uri("pack://Application:,,,/ok.png")); to App.xaml.cs.
Run again, we get TypeInitializationException. Why?
I know I can define okImage in MainWindow.xaml.cs, but if I insist it must be defined and initilized in App.xaml.cs, how should I do?
Usually TypeInitializationException iteslf is not very helpful - always check it's InnerException property.
In your case, it says "Invalid URI: Invalid port specified.", which is much more helpful.
It tells that the problem isn't image itself, it's Uri constructor which throws exception.
But why it succeeds in MainWindow.xaml.cs and fails in App.xaml.cs?
It's all about UriParser schemes. UriParser defines way of parsing some parts of Uri and is involved in Uri object initialization. When your MainWindow object is accessed for the first time, it's static constructor is called and your image is created. At this moment, your application has already registered a UriParser for pack:// scheme, and Uri parser succeeds. But when you try to do the same thing in Application.xaml.cs, custom UriParser for pack:// scheme is not yet registered, and default one fails.
Solution:
Do not instantiate your images in Application static constructor. You may still keep fields or properties and make them static if you want, but move all your image initialization code to OnStartup():
public partial class App : Application
{
public static BitmapImage okImage;
protected override void OnStartup(StartupEventArgs e)
{
okImage = new BitmapImage(new Uri("pack://application:,,,/ok.png"));
base.OnStartup(e);
}
}

Is there a way to start a WPF application without StartUpUri that doesn't break something else?

I've been trying for hours to get to the point where I can start a WPF application and have full control. I want to be able to create a ViewModel, create a View (Window), set the data context of the View to be the ViewModel, then show the View.
I've tried lots of methods, the most promising being to change the App.xaml to be a page and then adding my own Main method. Unfortunately this doesn't work properly because VS2010 then does not show the styles from the App.xaml in the designer, though they do work when running the app.
Is there a way to do what I want? If not, how do people normally start MVVM apps in WPF, creating a ViewModel outside of the View itself?
I would use the Startup event. You can add this to the App.xaml and remove the StartupUri line. When you add it, Visual Studio can create the event for you within the App.xaml.cs file. You can initialise your ViewModel and View within.
Here is one simple way...
<Application
x:Class="Demo.Ux.WpfApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
Here is the basic App.xaml.cs
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
try
{
var mainView = new MainView();
mainView.Show();
mainView.DataContext = new MainViewModel();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
Application.MainWindow can be used as well. The first displayed Window will be assigned to MainWindow auto-magically. Of course, you can skip creating your mainView and write directly to MainWindow which would thin out the syntax as well.
MainWindow = new MainView();
MainWindow.Show();
MainWindow.DataContext = new MainViewModel();
One final note, I'm doing the Show before the data bind. You need to do this to avoid a situation where the MainViewModel throw an exception during creation. If the MainView hasn't been shown, the app will close without letting you see the error.
in our application, we have choosen the way which you already proposed: writing a new Main method. You also have to make some changes in the project application settings then (no startup object). The app xaml has to look something like this:
<Application x:Class="EVOCURA.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup"
Exit="Application_Exit">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--Custom Controls-->
<ResourceDictionary Source="<your resources here>"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The code behind will look something like this:
public sealed partial class App : Application
{
static App()
{ }
public App()
{ }
private void Application_Startup(object sender, StartupEventArgs e)
{
// create the main window and assign your datacontext
MainAppWindow main = new MainAppWindow();
main.DataContext = <your datacontext here>
main.Show();
}
[STAThreadAttribute]
public static int Main(string[] args)
{
App app = new App();
app.InitializeComponent();
app.Run();
return 0;
}
}
Have a look at the Startup Event and notice, that no default StartupUri is specified im App.xaml
You could also pass the DataContext in a new constructor of your MainWindow, or create the DataContext directly in xaml.
The simplest way to assign an instance of the ViewModel to the DataContext of the view is in the code behind of the Window.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new myViewModel();
}
}
For the first part of your question, you can have the control of your application in the StartUp event
<Application x:Class="myApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
Code Behind :
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
// Place your code here
}
}

Cannot access resource defined in app.xaml

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

How to change StartupUri of WPF Application?

I am trying to modify App.cs and load the WPF XAML files from code behind but its not working as it should.
No matter whatever I try to set as StartupUri it doesnt start, the program quits after this.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
LoginDialog dlg = new LoginDialog();
if (dlg.ShowDialog() != true)
return;
switch (dlg.ChoiceApp) {
case ChoiceApp.CustomerEntry:
StartupUri = new Uri("/MyApp;component/Forms/CustomerEntry.xaml",
UriKind.Relative);
break;
case ChoiceApp.VendorEntry:
StartupUri = new Uri("/MyApp;component/Forms/VendorEntry.xaml",
UriKind.Relative);
break;
}
}
}
Now I even did trace and found out that LoginDialog is working correctly and is returning values correctly but setting "StartupUri" does not work.
I checked in reverse assembly that DoStartup method of App gets called after OnStartup, so technically my StartupUri must load, but it doesnt, in App.xaml startup uri is not at all defined.
Note: Bug Confirmed
I noticed that ShowDialog sets Application.MainWindow and when dialog ends, it sets it back to null, and because of this setting StartupUri does not work after calling Modal Dialog in OnStartup or Startup event.
There is no error or exception about invalid uri or anything like that.
This method works without DialogBox being called in Startup event or OnStartup, i think calling showdialog on this method causes something like its mainwindow being set to expired window and it shuts down after this.
Akash, I ran into this exactly issue trying to implement a LoginDialog just like yours. The dialog does not have a bug, but rather the behavior is by design.
Not a bug. The default ShutdownMode of
Application is OnLastWindowClosed, so
as soon as the first window is closed
your application will start shutting
down! Change to OnExplicitShutdown and
it will work, but you'll have to
manage the shutdown.
See this previous StackOverflow question: WPF ShowDialog returns null immediately on second call
instead of overriding the OnStartup() method, hook into the event instead.
in the XAML
<Application x:Class="SOTestWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
in the code behind:
private void Application_Startup(object sender, StartupEventArgs e)
{
var rnd = new Random();
if (rnd.NextDouble() > 0.5)
StartupUri = new Uri("/SOTestWPF;component/Window1.xaml", UriKind.Relative);
else
StartupUri = new Uri("/SOTestWPF;component/Window2.xaml", UriKind.Relative);
}
This is only my test case and I have verified that it performs correctly (randomly :D)
Just try in OnStartup() :
StartupUri = new Uri("Forms/CustomerEntry.xaml", UriKind.Relative);
Do you still have a StartupUri specified in the XAML? If so, remove it and see if that helps.MSDN Source
If not, you may need to approach this differently: have your Dialog as your startup, then from that point open another Window based on the selected value.

Resources