WPF MessageBox is not displayed at all if long message is passed - wpf

I ran into situation when some MessageBoxes with error messages were not displayed at all. After closer investigation, I was able to narrow down the problem to cases when a very long string is passed as messageBoxText.
In such cases, call MessageBox.Show does not display anything, returns MessageBoxResult.No and, in most cases, there's a message in the Output window saying The thread 0xHEXNUMBER has exited with code 0 (0x0). For me, this method silently fails.
This is very strange - WPF is a very mature technology and I expect this code to work according to spec or throw some exception (e.g. OutOfMemory, StackOverflowException). I've debugged the program and no manged or unmanged exceptions are thrown and caught.
What is the root cause of MessageBox not being displayed? Is there some easy way to debug such things (since no exceptions are thrown or logged). What is the messageBoxText length limit and what does it depend on (I could check that empirically on my computer, but would be conclusive for one OS/platform/.NET Framework version at best)?
Repro:
Here's a code which demonstrates the problem (it can be used with standard WPF Application template in Visual Studio).
MainWindow.xaml:
<Window x:Class="LongMessageInMessageBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Click="ShortMessage_Clicked">Short Message</Button>
<Button Grid.Row="1" Grid.Column="0" Click="LongMessage_Clicked">Long Message</Button>
</Grid>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace LongMessageInMessageBox
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/// Method which shows that displaying short message works correctly
private void ShortMessage_Clicked(object sender, RoutedEventArgs e)
{
Console.WriteLine("ShortMessage_Clicked");
var result = MessageBox.Show("Short Message");
Console.WriteLine(result);
}
/// Method which shows that displaying a long message does not work
private void LongMessage_Clicked(object sender, RoutedEventArgs e)
{
Console.WriteLine("LongMessage_Clicked");
var longTextBuilder = new System.Text.StringBuilder(10000);
longTextBuilder.Append("Long Message \n");
for (int i = 1; i <= 100000; i++)
{
longTextBuilder.Append(" ").Append(i);
}
var result = MessageBox.Show(longTextBuilder.ToString());
Console.WriteLine(result);
}
}
}
Sample from Output window:
ShortMessage_Clicked
OK
ShortMessage_Clicked
OK
LongMessage_Clicked
No
The thread 0x55bc has exited with code 0 (0x0).

MessageBox class in WPF (and WinForms) wraps the Win32 MessageBox function, and doesn't throw an exception. In the old Win32 world error is reported by setting the last-error code, and if you don’t call GetLastError to check, you can just keep going without knowing something is broken.
From Reference Source
//so it just translates the return code to a MessageBoxResult
MessageBoxResult result = Win32ToMessageBoxResult (UnsafeNativeMethods.MessageBox (new HandleRef (null, owner), messageBoxText, caption, style));
I have not tested the capacity of the lpText string in the Win32 MessageBox function. But it is not surprising if the capacity limit is something like 1024, 4096 or 65536, after all, MessageBox function is designed to display short message.

Related

"Help/Tutorial" page in WPF menu bar

I've added a menu bar to application, because I wanted to add there a little tutorial under "Help" menu. Like Help - > "How to use the program", that's the idea.
I would like this to be, a pop up window that has ability to include text, as well as photos to better explain the program. It should be able to have a little bit of formatting (just a simple spaces, tabulator, new lines) so the text with photos looks clean.
To sum up, I need an idea, a tutorial, a guide, an adivce - you name it what should I use to implement those things, because right now I am in a blank spot.
#edit Hope this clarifies my question.
To do this you need menu items:
<Window x:Class="WPFTestApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Menu>
<MenuItem Header="Help">
<MenuItem Header="How to use this program" Click="MenuItem_Click"></MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
And you would need a click handler:
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFTestApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
HelpWindow window = new HelpWindow();
window.Show();
}
}
}
Then, you would need to create a new window with all the help text you would like. You would need to design that window and I think that is really dependent on what the application does.

VS2013 : can't locate custom control resource on xaml page

I just installed vs2013 and I carried a WPF project, previous developed with VS2010, under this environment. Happens to me a very strange thing that obviously does not happen with 2010 .... I have my own custom control that defines a runtime URI to a dictionary ... the strangeness lies in the fact that in the code of xaml page, when i define the use of my control, i've got the error reported in subject, but if i start the dubug, everything works fine and the style is correctly applied .... someone got an idea?
the code look like that :
the custom control
using System;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
using System.Windows.Data;
namespace myListView
{
public class myListView : ListView
{
// ... some dependency property
protected override void OnInitialized(EventArgs e)
{
Uri uri = new Uri("Skins\\myDictionary.xaml", UriKind.Relative);
dictionary = Application.LoadComponent(uri) as ResourceDictionary;
//... rest of code
base.OnInitialized(e);
}
}
}
the xaml page
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xlw="clr-namespace:myListView;assembly=myListView"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xlw:myListView>
<!-- the editor tell me that can't find the Skins/myDictionary.xaml -->
</xlw:myListView>
</Grid>
</Window>

WPF Window messages not firing while popup window animation is running

I have created a little Popup window in WPF, which shows and hides with a 500 millisecond fade-animation.
The Popup is shown when the PreviewMouseUp of a TextBox control is fired, and hidden when the focus of the TextBox is lost.
The problem is that if I have two of these TextBoxes, the animation of the Popup-window seems to block all Window Messages sent to the main window while the animation is going. The PreviewMouseUp of the second TextBox is fired only right after the animation of the first TextBox's Popup is complete.
Is there a way to make the fade-animation of my Popup Window NOT to block Window Messages while the animation is running?
Example XAML file:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="22,26,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" PreviewMouseUp="TextBox_PreviewMouseUp_1" LostFocus="TextBox_LostFocus_1"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="22,54,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" PreviewMouseUp="TextBox_PreviewMouseUp_1" LostFocus="TextBox_LostFocus_1"/>
</Grid>
</Window>
Example Code file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
{
Popup p = new Popup();
p.Width = 100;
p.Height = 100;
p.Placement = PlacementMode.Left;
p.PlacementTarget = (TextBox)sender;
p.Child = new Border();
p.IsOpen = true;
((TextBox)sender).Tag = p;
}
private void TextBox_LostFocus_1(object sender, RoutedEventArgs e)
{
Popup p = (Popup)((TextBox)sender).Tag;
DoubleAnimation anim = new DoubleAnimation(100, 0, new Duration(new TimeSpan(0, 0, 1)));
p.BeginAnimation(WidthProperty, anim);
}
}
}
If you click quickly both textboxes, you notice that the other Popup won't appear (and the textbox doesn't get focus) while the animation is running.
What I have found so far, it seems like if the animation is really intensive (high framefrate), the window messages are blocked until the animation is complete. If I set the application framerate to lower value, like 30FPS, then the problem goes away. But this is not an option for me because I wan't the animations to be as smooth as possible.
This happened because I was animating the margins of the popup window - which generates a lot of window messages when the window size frequently changes. (Which ofcourse jams the whole windows messagepump of the application for the duration of the animation).
So the solution is not to animate the popup itself but a child control of the popup instead (for example a panel), and set the popup to allow transparency so we get the same effect but are actually not animation the actual window size.

What is the WPF answer to this?

I have used WPF to develop two moderately sized applications. I was much impressed by the cleanness of WPF and its features. When I explained to one of my colleagues (Who happens to develop business apps) the various benefits of WPF, he challenged me with this problem which had me totally stumped:
The Problem:
He coded an application in the following way in about 2 minutes:
Open a new WinForms project.
Define a class Loan.
Build project.
Define an object data source using Loan.
In Data Sources explorer, change view type of the Loan data source to Details.
Drag the data source onto the Form in the designer.
Supply the data source with a Loan[] containing one object.
Build and run application.
The code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinForms_DataBinding_Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
loanBindingSource.DataSource = new Loan[] { new Loan() };
}
}
public class Loan
{
public decimal Amount { get; set; }
public decimal Rate { get; set; }
public decimal Total { get { return Amount * Rate; } }
}
}
The designer:
The application:
Now whenever you change the value of Amount or Rate in the window, the value of Total changes accordingly. After explaining that this is a very useful feature in business apps where any changes you make to one property in an entity immediately updates the view where calculated properties are refreshed instantly making the user experience better. Considering that the typical business entity class has a lot of properties, this saves a lot of coding. Then he asked me to do the same in WPF.
I first explained to him that I do not understand what sort of black magic goes on here. How does the Total textbox update itself automatically? This is my first question:
Q1. The Loan class does not implement INotifyPropertyChanged or something similar. So how does the Total textbox get updated when the Amount or Rate textboxes lose focus?
Then I told him that I do not know how to do the same thing so easily in WPF. However, I wrote the same app in WPF with 3 TextBlocks and 3 TextBoxs in the UI. I also needed to make Loan class implement INotifyPropertyChanged. Added backing fields to Amount and Rate. Whenever these properties were being set, I raised a property changed notification for the property Total. In the end, I was left with an app with badly aligned controls which did the same thing as the WinForms app. However, this was way harder to do than the WinForms method.
I came home and then had the bright idea of drag-dropping the Loan data source on to the WPF window (After I changed the view mode to detail). Sure enough, I got the same kind of UI as in WinForms app and after setting the data source to the same Loan[] as in WinForms app, it seemed to be complete. I ran the app, changed the Amount and Rate fields hoping to see Total change itself automagically. However, I was disappointed. The Total field did not change:
The code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WinForms_DataBinding_Example;
namespace WPF_Grid_Example
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource loanViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("loanViewSource")));
// Load data by setting the CollectionViewSource.Source property:
loanViewSource.Source = new List<Loan>() { new Loan() };
}
}
}
The xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:WinForms_DataBinding_Example="clr-namespace:WinForms_DataBinding_Example;assembly=WinForms_DataBinding_Example" mc:Ignorable="d" x:Class="WPF_Grid_Example.MainWindow"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
<Window.Resources>
<CollectionViewSource x:Key="loanViewSource" d:DesignSource="{d:DesignInstance {x:Type WinForms_DataBinding_Example:Loan}, CreateList=True}"/>
</Window.Resources>
<Grid>
<Grid x:Name="grid1" DataContext="{StaticResource loanViewSource}" HorizontalAlignment="Left" Margin="121,123,0,0" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Amount:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
<TextBox x:Name="amountTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding Amount, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
<Label Content="Rate:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="1" VerticalAlignment="Center"/>
<TextBox x:Name="rateTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="1" Text="{Binding Rate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
<Label Content="Total:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="2" VerticalAlignment="Center"/>
<TextBox x:Name="totalTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="2" Text="{Binding Total, Mode=OneWay}" VerticalAlignment="Center" Width="120"/>
</Grid>
</Grid>
</Window>
Q2. I was confounded before by the black magic of WinForms, I was confounded now because the same black magic did not work in WPF. Why?
Q3. How do I make the WPF version to update the Total field automatically as in the WinForms example?
Q4. Which platform is better/faster for this sort of business app development? If I am to make a better argument on behalf of WPF, what should I be looking at?
I hope I was clear about the problem. Please let me know if any clarifications are needed. Thanks.
Q1: If you look at the designer file for the Windows Form you'll see about 300 lines of code generated for your 3 textboxes. Some of this code is similar to:
this.amountTextBox.DataBindings.Add(
new System.Windows.Forms.Binding("Text",
this.loanBindingSource, "Amount", true));
The Binding and the BindingSource co-operate to update the bound values and cause all bound controls to be updated every time one of the values changes (using reflection).
Q2: Because the WPF designer doesn't create a .Designer.cs file and the associated mess of code. You need to explicitly implement INotifyPropertyChange, which can be simplified by using say MVVM Light's ViewModelBase, e.g.
public class Loan : ViewModelBase
{
public decimal Amount
{
get
{
return this.amount;
}
set
{
if (Set(() => Amount, ref this.amount, value))
{
RaisePropertyChanged(() => Total);
}
}
}
Q3:
1) When Amount or Rate changes raise the property change notification for that property but also for the computed property 'Total'.
2) Modify your bindings on Amount and Rate to Binding="{Binding Amount, UpdateSourceTrigger=LostFocus}"
Q4: WPF no question (IMHO). The WPF way is more testable and maintainable and understandable.
Answer to Q4:
Regardless of winforms having the ability to generate 3 silly textboxes for a class, WPF is a much better, scalable and powerful framework. It has much greater performance due to hardware acceleration and whatnot, and requires less or no code to do some tasks that take tons of code in winforms, such as this, or this:
<CheckBox x:Name="chk"/>
<TextBox IsEnabled="{Binding IsChecked,ElementName=chk}"/>
Also, tipical Line-of-Business applications have to deal with thousands or hundreds of thousands of records, and UI Virtualization makes a huge difference.
The bottom line is that winforms, regardless of having some designer goodies (which are more a feature of Visual Studio than winforms itself), is nowhere near as practical and adequate when it comes to Line of Business.

Openaccess: How to get data from DB

Silverlight newbie here, trying to do a simple databinding:
Im trying to get a list of all users in a test db, but i cant;
Im using Openaccess and followed all tutorials on how to create a Domain Model, then a Domain Service;
Im having problems executing the following code, which returns always 0 even though i have 200 lines on db:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using SLTest.Web;
namespace SLTest {
public partial class MainPage : UserControl {
private TestDomainContext link = new TestDomainContext();
public MainPage() {
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
private void MainPage_Loaded(object sender, RoutedEventArgs e) {
int count = 0;
LoadOperation<DomainUser> loadOperation = link.Load(link.GetDomainUsersQuery());
loadOperation.Completed += (s, a) =>{
theList.ItemsSource = link.DomainUsers;
count = link.DomainUsers.Count();
};
}
}
}
Also, why is that simply doing
int count = link.DomainUsers.Count();
doesnt work as it does in ASP.NET?
Here is the XAML:
<UserControl x:Class="SLTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="theGrid">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" x:Name="theList">
<DataTemplate>
<TextBlock Text="{Binding FullName}"></TextBlock>
</DataTemplate>
</ListBox>
</Grid>
</UserControl>
I am guessing that the link object is your actual domain service and as such you should not be addressing it directly in the callback method as all query operations in your service are asynchronous and will not yield any result of addressed that way.
Instead you can use the asynchronous callback methods arguments to get the information you are requesting like that:
loadOperation.Completed += (s, a) =>
{
LoadOperation<DomainUser> loadedObjects = (LoadOperation<DomainUser>)s;
theList = loadedObjects.Entities;
count = loadedObjects.Entities.Count();
};
That should work.

Resources