Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Modal-Dialogs and MessageBox-Like behaviour.
31 replies. Latest Post by kobruleht on April 8, 2009.
(0)
Kevmeister
Member
249 points
119 Posts
05-28-2008 9:09 PM |
Simple problem, so I hope there is a simple answer:
I want to display a message-box. Not a Windows box using Browser.Window.Alert( ), but something within Silverlight itself in order to get pretty visuals (eg. using the Popup class etc).
The problem is not displaying the content but handling the messages. A traditional implementation (eg. WinForms, perhaps even WPF) would result in the MessageBox function running its own message pump, until such time as one of the message-box options has been clicked, and then the result can be returned to the caller.
Is this possible in Silverlight or am I going to have to jump through hoops and do it all asynchronously?
(<whinge>I really dislike obfuscating my code with asynchronous behaviour when I have no good need for it</whinge>).
The WPF resources certainly don't help me: there's no Window class (for Window.ShowDialog), there's no DispatcherFrame (to run an inner message pump), etc.
sladapter
All-Star
17441 points
3,172 Posts
05-28-2008 9:39 PM |
I wrote a Draggable window object which you can put anything in the window. It has IsModal property you can set. It will basically block the window below until you close the popup window. You might want to take a look and modify it to a message box. It's probably not a read modal dialog but just act like one.
Read this thread:
http://silverlight.net/forums/p/12467/41324.aspx#41324
05-28-2008 10:31 PM |
I shall have a play - thanks.
05-29-2008 10:35 PM |
I've looked at your example code - which I commend you on.
But really it only solves the modality of the UI, not the sequentiality of the procedural code that is usually associated with modal dialogs.
So it seems that, at the moment, a Silverlight (XAML) message-box that stops program execution and awaits a response, just like a real Win32 MessageBox, is not possible, because there is no way I can see to start a nested message pump (or message loop).
Microsoft, if you're listening, I think this is something you should give consideration to if you want to simplify development of LOB applications.
05-29-2008 10:45 PM |
That's what I mean it's not a read modal dialog. Let's hope in beta 2 we can see something new.
Yi-Lun L...
25052 points
2,747 Posts
05-30-2008 12:28 AM |
Hello, first please note Silverlight is significantly different from Win32. There's no way to get involved with message loop. Even if you can achieve that on Windows, what about Mac?
For your question, if I understand clearly, you have some code like this:
void SomeMethod()
{
//Execute A.
Dialog.ShowDialog();
//Execute B.
}
You want to execute B after the dialog has been closed by the user. Yes, currently Silverlight doesn't have Modal Dialog. So you have to create a Closed event in your dialog. Then in the main page, in SomeMethod, you need to remove Execute B. Instead, handle the Closed event of the Modal Dialog, and Execute B in the event handler. If you have a lot of Modal Dialogs, you can create a code only base dialog. We understand the inconvenience. But unfortunately there's no time for us to build a Window and Dialog system in Silverlight 2. After all, this is not what a common web application needs. Think how many times have you seen such applications on the web. This is something that we're considering for VNext. But I can't give you any details on that at this time. It may or may not be in the next version...
Simbalight
245 points
87 Posts
05-30-2008 3:27 AM |
Hi! Using asynchronous behaviour is only one part of the problem. We can work around it as mentioned. The bigger problem in my eyes is to achieve modality. I couldn't find any sample, nor could I figure out how to achieve it. It's simple to avoid mouseclicks. But there's always the possibility to navigate with the keyboard.
Any possibilty to block that? Sth like blocking the entire main container?
sladaper: If you ever find a solution for this please let me know!
05-30-2008 5:27 AM |
I think you can handle the modal dialog's LostFocus event. In the event, you check if the current focused element is inside that dialog. If not, you set the focus back to the original element. You can get the current focused element by querying FocusManager.GetFocusedElement().
06-02-2008 6:45 AM |
Yi-Lun Luo - MSFT:Hello, first please note Silverlight is significantly different from Win32. There's no way to get involved with message loop. Even if you can achieve that on Windows, what about Mac?
Okay, admittedly direct access to the message loop may not be the appropriate solution, but there is no denying that a procedurally-modal dialog-box type behaviour is useful - be it for data entry purposes or message boxes, etc. The current thread of execution should stop and wait for a response from the dialog.
Yes, it can be worked around, but I would argue that platforms that need "too much working around" start to lose their appeal and lose their productivity.
Yi-Lun Luo - MSFT:After all, this is not what a common web application needs. Think how many times have you seen such applications on the web.
I don't disagree with the viewpoint, but I think you're not necessarily looking totally in direction where I am. Silverlight has the potential to become, amongst other things, an important tool for building rich line-of-business applications delivered through the browser, partly because it will be easier, perhaps substantially so, than doing so using HTML/AJAX.
An important facet that nearly all large scale LOB apps needs to do is item selection (eg. picking a foreign key), and selection is often handled currently in HTML apps as a "popup". It might be done as a "simulated popup" using a DIV in HTML or it might use a separate Window, but either way there is often call for this kind of mechanism over-and-above a simple embedded drop-list. The popup might need searching features, or there might a tree-navigational structure, or there might be a very large amount of data that needs on-the-fly filtering, all reasonable examples of functionality that is "bread and butter" for a typical LOB application.
True popup windows in HTML are a pain because you have no way to control parent-child modality and/or parent-child lifetime whereas at least with a DIV you can.
Silverlight, when used for LOB applications, is going to have the same functional issues to tackle in some way. There's no denying it can't be done now by breaking up procedural code and dispersing it all over the place to run piecemeal as the outcome of different execution handlers, but that's not an elegant solution in my opinion - it's a hack because the platform doesn't offer a mechanism which is fairly well expected these days of any modern GUI.
So I hope you consider this strongly for vNext and/or final release version 2.
Ciaran M...
157 points
71 Posts
06-03-2008 6:36 AM |
I agree with you totally Kevmeister. I too would like a simple modal dialog type behaviour. Well if they allow you to pause/resume a thread rather than just send it to sleep it might be possible to create a MsgBox on another thread... maybe. But you can't do that either.
I'm all for clean code... but code needs to be easy to read and navigate too. Requiring EVERYTHING be a response to an event is awkward in the extreme.
I think MS are missing a real market if they neglect the needs of business oriented software.
Dave Relyea
Participant
1084 points
249 Posts
06-08-2008 10:49 AM |
You might want to check this out:
http://blogs.msdn.com/devdave/archive/2008/06/08/using-popup-to-create-a-dialog-class.aspx
michael412
18 points
6 Posts
06-15-2008 4:05 PM |
I must add my voice to those who are requesting modal dialog capability for Silverlight. I'm having limited success with the LostFocus event. The argument that this is not what a common web application needs is irrelevant. The point of new technologies is not to reinforce current practices but rather to permit new ways of working. The only reason that modal dialog behaviour is not what common web applications need is that to date Microsoft based web applications have not had the ability to use modal dialogs and therefore have been written without them. Anyway, the statement isn't even correct. Plenty of web applications currently do provide this functionality to help users complete fields, etc - they just have to use other ways of doing it. For that matter one could argue that good old drop down lists are modal dialogs, albeit buttonless ones.
We need modal dialogs for our web apps.
CleverCoder
203 points
157 Posts
06-17-2008 5:50 PM |
If they don't give us the modal dialog, then can you at least provide a mechanism and platform support for building one? A bit of irony here is that the reply I am typing here is being done through a MODAL popup window on an HTML page controlled through javascript and CSS. (And Ajax). I think MS has underestimated the need for modal windows or the ability to disable input to others..
Please consider this for 2.0. At least a viable solution!!
Magikos
222 points
144 Posts
06-17-2008 6:51 PM |
I've created a messagebox class and the xmal to look like a standard messagebox with a grid that fills the screen preventing any click throughs to the UI until the the window is dealt wit.
A border control in the center with my message and buttons. I pass to the class a callback method in the constructor and when the control is closed I call back to that method with a custom set of event args that have the button press results.
I think you should try something like that. it works perfect for me.
06-18-2008 8:33 AM |
Magikos: I think you should try something like that. it works perfect for me.
Yes, so we get UI modality. I would also like procedural modality so I don't have to break up my procedural code into a spaghetti-nightmare mess because I have to use a call-back function to continue on from a messagebox..
If you are listening Microsoft, it is *annoying* to have to continously work-around limitations in the technology. I live with that as a Beta user but I want Microsoft to listen to its user-base and understand that modal behaviour is wanted.
06-18-2008 10:16 AM |
We are listening, but we have finite time and resources. But we plan on having more versions.
06-18-2008 10:18 AM |
Dave,
Do you mean we might have another beta before the final release? Please do not forget the combobox if you do !
06-19-2008 8:14 AM |
Of course MS are listening... you can see it every day on the forums. Frankly I think the frustration/impatience you're hearing should be both encouraging and congratulatory. To get the level of functionality that is in Silverlight in it's first few releases has been no small feat. It's been done in view of a far more expectant/critical audience than most products ever had to contend with. It took Flash/Flex 10+ years to get where they are today. MS is attemting something far bigger in a far tighter timescale.
I'd love 'proper' modal dialogs too, but as it is, it's a pain, but not a showstopper by any means. Work hard guys!
06-19-2008 3:38 PM |
Dave Relyea: You might want to check this out: http://blogs.msdn.com/devdave/archive/2008/06/08/using-popup-to-create-a-dialog-class.aspx
You might want to check this out: http://blogs.msdn.com/devdave/archive/2008/06/08/using-popup-to-create-a-dialog-class.aspx
Dave, this has solved my problem beautifully! This is definitely a good solution for now. It doesn't address the procedural issue, but it gets me past my issue.
Cheers!- Sean
bhelzer
3 points
3 Posts
07-31-2008 5:12 PM |
Of course I would like a general modal dialogs. But for 2.0 I would be satisified with just a MessageBox.
I just want to put up a simple message when the user goes to a different part of the application: "Your data has changed do you want to save it? Yes - No - Cancel".
And no, I can't do this asynchronously, because if I go on the application will lose it's state. So please don't tell me it's as simple as listening to some event. Since I am already in an event and need to respond to it correctly with user input.
codism
372 points
121 Posts
07-31-2008 5:29 PM |
bhelzer: I just want to put up a simple message when the user goes to a different part of the application: "Your data has changed do you want to save it? Yes - No - Cancel". And no, I can't do this asynchronously, because if I go on the application will lose it's state. So please don't tell me it's as simple as listening to some event. Since I am already in an event and need to respond to it correctly with user input.
Basically, you need to redesign your dialog box to accept a callback. The dialog box will call the callback with the user's input. The following code are from our production:
MessageBox.ShowYesNoQuestion("Are you sure you want to delete the buyer?", (r) => { if (r == MessageBoxResult.Yes) { _buyerColl.Remove(bge.Buyer); } });
08-04-2008 10:17 AM |
I wouldn't expect too much sympathy in your efforts to find a truly asynchronous solution. Just follow this thread long enough. :) As nice as it would be, it does make a certain amount of sense with respect to how the plugin works. There is no message pump like there is in Win32 / WinForms.. I think you will need to follow the async route to get to where you're trying to go. It would be helpful if there was a solution or helper classes in SL. SOmething like a "Dialoge" base control that has properties for "OkAction" or... "NextActivity"... or .. whatever. I have a suggestion. Why not incorporate a messageloop like behavior in SL and actually give us all what we want: true modality! :)
hammadmirza
324 points
107 Posts
08-05-2008 3:01 AM |
Why dont you people try Telerik Rad Controls for Silverlight, it contains a Window Control in 2nd release
Well if you want to make your own, i suggest you somethings:-
the Window Control must have instance of LayoutRoot,Reason: When a Dialog is shown and you dont want that the UI below that dialog must not be accessed, you need to show a rectangle (full width and height, LayoutRoot.Childern.Add(rectangle) ) with translucent background, just above the UI and below the dialog box(ZIndex)the Window Control must recieve BrowserResizeEvents,Reason: If a Dialog is currently active, and user resizes the browser, you need to resize that translucent rectangle to new dimensions, or you may want to center align the window.
I did all this in Silverlight 1.1, so any more questions you can ask.
grava
87 points
18 Posts
08-05-2008 6:34 AM |
Only way to Resync APM model of Silverlight is plumbing SL with a "Custom" message loop.
the method :
public voi a()
f1();
modal1.ShowModal();
f2();
have to be rewritten in something like:
public void a()
actionQueue.Start();
actionQueue.Execute(new Action(a));
actionQueue.Execute(new Action(ShowDialog));
actionQueue.Execute(new Action(b));
actionQueue.End();
Try to think at ActionQueue as a Queue of System.Action<T> (or Func) and at the end() statement start the dequeuing of the actions Synchronously ... the only Great problem is that you have to Resync with the RootLayout.Dispatcher for refreshing the UI.
We're writing an article about the resync problem which involves, for instance, a call to a WCF service, a Prompt and another call to WCF, in which we don't use APM (async programming model with "OnComplete" delegates), but plain C# Code.
Stay tuned, we've implemented everything and it seems it's ok ... we're investigating in more complex cases if we could run in starvation, race conditions or something else that ... well, it's the problem that pushed MS to remove sync methods ... Blocks our main UI Thread .
GG
jemiller
445 points
237 Posts
08-26-2008 5:50 PM |
Yi-Lun Luo - MSFT:Think how many times have you seen such applications on the web. This is something that we're considering for VNext. But I can't give you any details on that at this time. It may or may not be in the next version...
Think how many times have you seen such applications on the web. This is something that we're considering for VNext. But I can't give you any details on that at this time. It may or may not be in the next version...
I've seen it a lot in the Flex apps that I've developed...
Jamie H
33 points
40 Posts
08-28-2008 5:04 AM |
I also agree, having modal dialog functionality is pretty cirtical for app development. I work for a web design company and all of our "web 2.0" designs are includuing modal dialogs that we have to hack up with html divs that cover the entire screen. I wanted to tell everyone we could do this much easier in Silverlight, but without modal dialog support, it is as much of a hack in Silverlight as it is in HTML/javascript right now.
09-20-2008 6:29 PM |
Hi all,
I created a Connect Feedback request for modal dialog box-like behavior for Silverlight at the following URL. If you agree with me, please rate my feedback. Thanks.
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=368862
lingbing
Contributor
2249 points
406 Posts
09-21-2008 2:36 AM |
I think I must say something about this issue. I have read Dave's blog and known his idea about how to solve this problem. Yes, it works, just as we want, however, when we design a usercontrol, we sould consider both its activity and its presentation. Simply if we make a canvas with a transparent backgroud, the user could not click through thd popup, and if we add a close event and handle it we can make some code run after the event fires, but the problem is that when the users download our application, they will find the Behaviour is not like a MessageBox or Modal-Dialog, even they cannot click through, even all functions work well, If you have a look at OpenFileDialog in silverlight, it is completely windows-like, its behaviour is as same as both win32 and wpf, so that when users use it, they will find that they are using an application just like at desktop but not at a browser. I think that is why we want MS to create a Modal-Dialog and we can use ShowDialog method to show it to user just like desktop form applications.
I don't know if you agree with me at this point, however, we must acknowledge that some common controls are necessary, such as ComboBox (Dropdown), Treeview, RichTextBox...and so on. I know MS silverlight team is busy with much work, hope you work well!
lepipele
44 points
22 Posts
01-16-2009 12:20 PM |
Here is best modal dialog control I've found:http://www.codeproject.com/KB/silverlight/slmodal.aspx
ygoldman
4 points
2 Posts
01-24-2009 4:04 PM |
I've put together a generic framework for creating modal popups - article is here: http://blog.yuriy.org/2009/01/building-modal-popup-framework-with.html. It
Goals of the Framework:
davides77
19 Posts
01-30-2009 9:02 AM |
Hi guys,
I tried the modal dialog and all works fine but...
but I put a datagrid in the modal dialog, all works fine but when I go over the header of the colums all the silverlight application disappear!!!
why?????
tnx very much!!!
kobruleht
161 points
582 Posts
04-08-2009 10:07 AM |
Silverlight modal messagebox is like standard silverlight control, it routes click event to sl button if its button is placed in the same location in screen as sl button.
We can hopefully create our modal control and after it show hidden messagebox to create modal loop. After clicking we can release modal messagebox.