wpf window throws exception when trying to show - c#

I created a WPF application and it runs perfectly on several computers.
There is one computer that keeps throwing an "Object reference not set to an instance of an object." exception.
I can't install Visual Studio on that computer, but i found the line that causes the problem:
var m_GCSWindow = new GCSWindow();
m_GCSWindow.Show();
everything runs perfectly until the Show().
i can't figure this one out because:
it works on all the other computers.
it's MVVM, no code behind.
what could be the problem?
Edit:
I was finally able to install visual studio on the problematic computer, and found that if i remove a single line from the XAML everything loads ok.
<Image Source="{Binding MapView.MapImage,Converter={StaticResource ImageConverter}}"
Stretch="None" MouseLeftButtonDown="Image_MouseLeftButtonDown"
ContextMenu="{StaticResource MapRightClick}" />
so i thought the problem might be in the converter, and put a break point in it, but the exception occurs before.
Edit 2:
After a little more investigation i found that MouseLeftButtonDown="Image_MouseLeftButtonDown" is what causes the problem.
when i remove that line everything works.
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (ImageClickCommand != null) ImageClickCommand.Execute(e.GetPosition(sender as IInputElement));
}
that's the code, and i put a break point in it but still i never get there.
I even tried moving the event to another control, but i still have the same issue.

Maybe m_GCSWindow is null perhaps you could put in a null check .
Does GCSWindow reside in a DLL? Maybe the DLL is missing? Maybe one of its depdencies is missing.
You could also put a try catch around that call

I've had this issue before. The bug can probably be found in the converter
Try debugging the converter
Make sure it gets called
Make sure it gets the value that you want it to convert
Make sure it doesn't crash when converting
Make sure you get the value you want
Etc etc etc
Also - Can you post the code for ImageConverter?

Related

UWP Use Shy Header for ListView

I basically copied code from here and then changed variables to mine. I have also copied the files under the folder ExpressionBuilder.
And inside the ShyHeader.cs, there is such line of code:
var scrollViewer = gridView.GetFirstDescendantOfType<ScrollViewer>();
So I tried same thing for my ListView but it says undefined:
SongsListView.GetFirstDescendantOfType<ScrollViewer>()
So I put a ScrollViewer around the SongsListView and use that. But I fail to get it working for my project. The header is neither sticky above nor shy. It just gets scrolled normally. Can anyone help me debug it?
SongsListView is here.
SongsListView is used here in HeaderedPlaylistControl.
ShyHeader is implement here at the bottom.
HeaderPlaylistControl is used in AlbumPage, MyFavoritesPage and PlaylistsPage.
So I tried same thing for my ListView but it says undefined
This is extension method for DependencyObject. You could find it here and copy it to you project.
So I put a ScrollViewer around the SongsListView and use that. But I fail to get it working for my project.
I checked your code, you need call SetShyHeadermet method when
PlaylistInfoGrid loaded.
private void PlaylistInfoGrid_Loaded(object sender, RoutedEventArgs e)
{
SetShyHeader();
}

Some lines of code in form not being executed?

I have a form in my program that has two drop downs and three buttons. When I run the form, I want the two drop downs to be blank, and one of the buttons to be disabled. I had this setup and working fine, but I have since been working on other classes and forms in the program and, coming back to refine some things, I have found that the code no longer works. The lines in particular are:
private void mainForm_Load(object sender, EventArgs e)
{
this.roomsTableAdapter.Fill(this.bookingSystemDatabaseDataSet.rooms);
timeBox.ResetText();
roomBox.ResetText();
updateTextOutput();
bookButton.Enabled = false;
}
The first line uses a TableAdapter to fill the associated 'rooms' drop down box, which works, however the code after this line does not seem to be executed at all. Commenting out the line means that they are executed, but obviously my 'rooms' drop down would be empty. I have tried moving the lines to the main execution point of the form, but the same thing happens.
Furthermore, I have tried to fill the 'rooms' drop down in 'other ways', including:
roomBox.DataSource = roomsTableAdapter.Fill(bookingSystemDatabaseDataSet.rooms);
and:
roomsTableAdapter.Fill(bookingSystemDatabaseDataSet.rooms);
roomBox.DataSource = roomsTableAdapter;
Of course, these attempts are very similar, but I figured I'd give it a shot. As it turns out, the exact same thing happens in each case; the 'roomBox' is filled, then the rest of the code is ignored. Debugging by stepping through backs up what I already thought to be true, the code is not even executed as far as I can tell. There are no errors, and the program runs, so why are these lines being ignored?
Whilst the code appeared to run fine, an exception was actually being thrown - as sgmoore suspected, but it did not cause a break. gunr2171 gave the prognosis:
Press Ctrl+Alt+E to bring up the Exceptions window, then tick the box for Common Language Runtime Exceptions under Thrown. This enabled me to see that an error (Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.) was actually apparent. I resolved this by removing the reference to rooms in the data set and recreating it.
Solved with help from sgmoore and gunr2171.

Error with tao SimpleOpenGlControl

I am creating an usercontrol contains a "Tao.Platform.Windows.SimpleOpenGlControl".
In my control's constructor, I have
{
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();
}
My problem:
When I use the control on a "Windows Forms Application" it's ok, but if I put the computer at hibernate or sleep mode, when visual studio is open and form that contains the control, is in design mode, the next time I turn it on this error comes up:
Fatal Error
can not activate the gl rendering context
and visual studio is not responding!
What's wrong here? I am doing something wrong?
I am using Tao framework.
After implementing OpenGL chart solution, I encounter those error.
Every time I tried to close form, error occurred.
After few times of debugging, I found the reason.
The reason is like this.
On my form closing, Draw function tried to use OpenGlControl object ONE MORE TIME.
So I make condition to check the additional flag.
I solved my problem in this way:
In control's InitializeComponent(); I removed simpleOpenGlControl1 Initialization and then in control's Load() function, I have
isDesignMode = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
if (!isDesignMode)
{
// init simpleOpenGlControl1
}
Now when my control is used in a project, there is no simpleOpenGlControl1 in design mode to make problem!

Binding height, width, etc to a variable

I don't know if I'm just not understanding what I've found so far, or if it really is too complex to apply to such a simple idea as it seems. I'm trying to bind a button's height and width to variables that are stored in user settings. I can't seem to get this to work, or rather I simply don't how to, as in what commands to use. The issue lies in not knowing what to put in the Binding field of the xaml. If anyone could point to a guide that involves just this, could explain what to do I would be very appreciative.
Edit: I've solved the problem of binding the variable, it now saves to the User setting file when it should. Now I'm having an issue with the value stored in user setting beig overwritten every time the program loads with the default value. I am running this through VS debug menu selection, so I suppose the issue could lie there, but I've tried publishing it and running and still getting the same results. Any ideas?
Assuming by 'User Settings' you mean the built-in Settings not a custom implementation:
See http://blogs.windowsclient.net/bragi/archive/2008/07/03/using-settings-in-wpf-or-how-to-store-retrieve-window-pos-and-loc.aspx for an example of this - essentially you want to set up TwoWay bindings to Properties.Settings.Default: note that you have to define the settings in advance using the Settings UI, and you have to call Properties.Settings.Default.Save() when the app exits to persist the settings.
I'm posting this answer so that hopefully somebody else can read it and avoid such a ridiculous problem. First off, as far as the initial question, Staurt answered it quite nicely. But my edit above brought up a new but related problem. I ended up fixing it on accident.
The whole purpose of this was that I have a slider bar that adjusts the size of a shortcut button dock. The slider worked, but as I said above it would reset itself every time I reloaded. The issue in this case was that I have the buttons set to resize as the slider moves, so I used the slider_ValueChanged event as you can see here:
private void iconSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
try
{
Properties.Settings.Default.iconHeight = Convert.ToInt32(iconSizeSlider.Value);
Properties.Settings.Default.iconWidth = Convert.ToInt32(iconSizeSlider.Value * 1.3);
Properties.Settings.Default.Save();
//iconWidth.Text = buttonWidth.ToString();
//ButtonRefresh();
}
catch (FormatException)
{
}
}
While trying to use the Run To Cursor part of VS2010, I got tired of having to F11 through a multitude of loading steps, so as a debugging tool I added a bool fullyInitialized flag. This solved the problem completely. Apparently (which I didn't realize before), when the slider was first initialized it considered the value to have changed, so when it ran through the ValueChanged method, it reset everything to default. So adding a simple conditional around the try-catch to check for the fullyInitialized flag solved everything. Hopefully this helps somebody else.

Help! Data binding does not work in Silverlight on Mac

I have writen a small applet in Silverlight and, while it works fine on Windows, it seems that on OSX the data binding part of the application (all those NotifyPropertyChanged calls) do not work. Does anyone know why this is? I've tried under both Firefox and Safari with the latest 2.0 download installed.
Your usage of the model object instance in Page seemed odd to me right away. It is not downright incorrect but unusual to me. Some experimentation led me to a working solution, albeit without knowing the cause of the error that happened in the first place. Not many people instantiate objects directly in the DataContext assignment, which is probably why this is not a well-known (and fixed!) defect.
Remove the DependencyObject base class from MyModel.
Make the MyModel instance be a resource of Page, instead of instantiating it directly into the DataContext.
Modify the Button_Click event handler to load the resource, instead of the named Page child object.
All done!
Code snippets for the working solution follow.
Page.xaml
<UserControl.Resources>
<my:MyModel x:Key="TheModel"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource TheModel}">
Page.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
((MyModel)Resources["TheModel"]).BeginUpdateBitmap();
}
MyModel.cs
public sealed class MyModel : INotifyPropertyChanged
{
Please also include the source code with your question in the future. It would have made this quite a bit simpler.
Did you try using remote sliverlight debugging to the mac? I'd expect getting the debugger setup and turning on 1st chance exceptions has a good shot at showing you the problem.

Categories