Cannot set startup object in WPF VB.NET Project - wpf

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

Related

The type 'Window' does not support direct content

I have a WPF solution built with VS 2015 composed of several projects. Suddenly I started receiving a warning in design mode stating the following:
The type 'Window' does not support direct content.
I understand how some controls do not support direct content, but System.Windows.Window should. I get the same warning with UserControl, and as far as I know, any other control that typically supports direct content.
Everything compiles and runs fine, but having the blue underlines through all of my XAML is bothersome. Has anyone else come across this?
Below is a screenshot:
Make sure you reference System.Xaml. Clean and rebuild the project. Works on VS 2015 Update 1.
At least in a WPF IronPython project, adding the System.Xaml reference to the project solved the problem for me:
An important thing to note here is that adding seemingly any reference will make the problem go away temporarily -- until Visual Studio is restarted. System.Xaml, on the other hand, appears to keep the problem at bay. I even tried removing the reference, whereafter the problem returned upon restarting Visual Studio.
For me this error was happening because I added a WPF Window to a class library project.
For some reason (unknown by me), Visual Studio doesn't give us the option to select the WPF Window template from the "Add New Item..." dialog box if the project was not created as a WPF Application. Instead, it only offers the option to add a WPF User Control. Because of that, I selected the User Control template for the new item, and then edited the source code to make the XAML to become a Window object rather than a User Control.
<!-- The new item was created as an UserControl, but what I needed was a Window object. -->
<UserControl>
...
</UserControl>
<!-- Changed it to Window and made other necessary adjustments. -->
<Window>
...
</Window>
The problem was actually in the code-behind. Since it was created as an User Control, the window partial class was inheriting from UserControl, like the following:
public partial class MyWindow : UserControl
{
public MyWindow ()
{
InitializeComponent();
}
}
To fix it I just had to remove the inheritance, making my window class inherith from nothing, like this:
public partial class MyWindow
{
public MyWindow ()
{
InitializeComponent();
}
}
After removing the inheritance, Visual Studio didn't show the error "The type 'Window' does not support direct content." anymore.
on behalf of #mark Richman I edited the Itemtemplate to automatically Reference "System.Xaml".
Just in case some is interested:
can be found in: "[VS InstallDir]\Common7\IDE\ItemTemplates\VisualBasic\WPF\[InputLocale]\WPFWindow"
BR,
Daniel
Add System.Xaml and UIAutomationProvider references to your project, after that clear solution and then build again
in Visual studio 2019 :
I searched for ( System.Xaml.dll )
and I added it as a reference
its worked well
found it in this location:
" C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8 "

Converting WPF application to UserControl for use in WinForms application via ElementHost

I'm trying to convert this WPF application to WPF UserControl so I could use it in WinForms application via ElementHost. I'm new to WPF and have never even touched it prior to this attempt so I might be going about it completely the wrong way.
I got the UserControl project to compile, however, my StaticResources get underlined in VS with message "The resource 'x' could not be resolved". I've tried moving the xamls with the needed x:Key elements up one level (the same level as the UserControl Dijagram.xaml), but it still cannot resolve them.
In the WinForms app, when trying to add the UserControl through designer by selecting hosted content in ElementHost tasks I get the following error:
An error occured trying to create an object of type
'DijagramLC.Dijagram'. Make sure the type has a default constructor.
(even though default constructor exists).
However, if I add it in codebehind, like this:
wpfUserControl = new Dijagram();
elementHost1.Child = wpfUserControl;
Controls.Add(elementHost1);
the code compiles but throws this runtime error: "System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '20' and line position '4'. ---> System.Exception: Cannot find resource named 'MyToolbar'. Resource names are case sensitive."
I've tried with and without App.xaml and adding ResourceDictionary elements for problematic xamls, but basically, I have no idea what I'm doing :)
I've uploaded the code to my SkyDrive and would really appreciate it if someone could take a look and tell me what I'm doing wrong:
https://skydrive.live.com/redir.aspx?cid=21be1f8e850e85cc&resid=21BE1F8E850E85CC!353
I hate jumping in blindly to new techonoly like this, but I have had no choice this time, and need to know if my requirement is even achievable this way.
You are probably getting the error because you use resources from a resource dictionary which is not loaded. Loading a resource dictionary in the hosting application will probably solve the issue:
// When hosting a WPF usercontrol inside an element host,
// application resources are not loaded, so we need to load them manually.
var resources = new ResourceDictionary
{
Source = new Uri("/UNIT4.MKB.GUI.XAML.Dashboard.Resources;component/resources.xaml", UriKind.Relative)
};
// Check for null reference
if (Application.Current != null)
{
//Merge the loaded ResourceDictornairy with the dummy application Resources.
Application.Current.Resources.MergedDictionaries.Add(resources);
}
The problem is, you need THE default constructor like this:
public CreatedPollsUC()
{
InitializeComponent();
}
If you have any other code in the constructor, the error occures:
public CreatedPollsUC()
{
InitializeComponent();
// ... more code
}
So if you want to use further code in the constructor, you need to apply the control first to the element host. Then you can edit the constructor.

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?

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.

Dynamic Application-level resources are not dynamic when hosted in ElementHost

I'm hosting a WPF UserControl in a WinForms container. Now, I want to be able to theme/skin the UserControl. To do this, I've got several resource dictionaries that define the "skins." When my app starts up I create a "new System.Windows.Application()" so that Application.Current exists. To change the theme the old skin is removed and a new skin is merged into the Application level resource dictionary at runtime. However, this does not change any of the dyanamically referenced resources in the UserControl. I tried this in a straight WPF application and it worked just fine. Am I missing something, or is it not possible to do this at all? By the way, if I add a skin into the application resources before the UserControl is initialized it will work but I cannot change the skin after that.
To repo this in the most basic way:
Create a new WinForms application. Add a WPF UserControl to the app. This is simple enough:
<UserControl ...>
<Grid>
<Button
Background="{DynamicResource ButtonBG}"/>
</Grid>
</UserControl>
Create two ResourceDictionaries, White.xaml and Black.xaml (or whatever) that have a SolidColorBrush with the key ButtonBG with respective color. In Form1.cs, add two Buttons and an ElementHost. Set the child of the ElementHost to an instance of the UserControl we just created. Wire up the buttons to events that swap the skin:
private void White_Click(object sender, EventArgs e)
{
Application.Current.Resources.MergedDictionaries[0] =
(ResourceDictionary)Application.LoadComponent(
new Uri(#"\WpfThemes;component\White.xaml", UriKind.Relative)));
}
private void Black_Click(object sender, EventArgs e)
{
Application.Current.Resources.MergedDictionaries[0] =
(ResourceDictionary)Application.LoadComponent(
new Uri(#"\WpfThemes;component\Black.xaml", UriKind.Relative)));
}
In Program.cs, ensure that Application.Current exists and set the initial skin:
[STAThread]
static void Main()
{
new System.Windows.Application();
Application.Current.Resources.MergedDictionaries[0] =
(ResourceDictionary)Application.LoadComponent(
new Uri(#"\WpfThemes;component\White.xaml", UriKind.Relative)));
...
}
Now, when the White button is clicked I would expect the button in the UserControl to turn white and when the Black button is clicked I would expect the button to turn black. This does not happen, however.
Does anyone know why? Is there a solution?
Edit: Idea: Perhaps, if there's a way to force re-evaluation of DynamicResources when the theme changes, that would work.
Thanks,
Dusty
I think this may be an overlooked issue in the WPF framework.
From what I can tell via Reflector, it appears that when the Application resource dictionary is catastrophically changed (a change that will likely have wide ranging effects like adding, removing, or replacing a skin), there is code that loops over all of the Windows in the application and forces them to re-evaluate their DynamicResources. However, other elements that I would consider top-level in WPF like ElementHosts do not get the same treatment. This leads to the behavior that I'm experiencing.
My workaround to this issue is to manually go through all of my ElementHosts individually and add, remove, or replace the skin ResourceDictionary file. It's not perfect, but it gets the job done.
Dr. WPF came to my rescue when I was trying to do something similar. He shows how to create the Application object in WinForms. Now you can reference everything as StaticResource just like in a WPF application.
http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/
Another workaround would be to create a dummy window and specify the content of the elementhost as content.
If you look into the Application and check how it handles changes of resourcedictionaries, you see that it only notifies windows..
The only thing you should remind is to never show the window (-> exception), and to close it when disposing the elementhost, so the application can shutdown properly.

Resources