How to create pseudo synchronous download in WP7 - silverlight

I have a class with public DownloadAsync Method inside, that downloads Content over Webclient. I create an Object of that class and call the download Method.
My problem is: I would like to block Elements on UI (e.g. Buttons) until the download is done. I could not find any solution so far.
One Idea is: I could call a MessageBox with message like "download is done" in the Downloadcomplete Method and to call somehow an Eventhandler for MessageBox. But how?
Any idea how to solve my problem?
EDIT: I know hot to disable Elements, but because of asynchronuous download in the download method, I don't know when the download is over in order to enable back the elements

add an event to your data class, and when the download has finished then trigger the event handler.
then in your page do something like this in your initialiser
BusyMessage.Visibility = Visibility.Visible;
this.DataContext = MYDownloaderClass.downloadedData;
MyDownloaderClass.hasFinished += new EventHandler(hasFinished);
}
void hasFinished(object sender, EventArgs e){
BusyMessage.Visibility = Visibility.Collapsed
}

You should just disable all the elements - set IsEnabled to false on buttons etc. If you want a quick and dirty solution - you can overlay the screen with a Rectangle, Border, Grid or Popup.

Try a busy indicator with an overlay.
http://www.minddriven.de/index.php/technology/dot-net/windows-phone/wp7-xaml-viewmodel-busy-indicator

Related

Starting a WPF application as hidden

I'm writing a WPF application and want it to start as a hidden window. I've created the Window object and set its Visibility property to Visibility.Hidden before calling Application.Run(). Then, I have an event handler for Window.Loaded that also sets the visibility to Visibility.Hidden. Between the call to Application.Run() and the callback to OnWindowLoaded(), there is a black outline of the window that flashes up on the screen and then disappears. It's like the window manager is creating a drop shadow for the window or something and then hides it immediately.
After running my project through instrumentation, I finally found that Window.Show() was somehow getting called. So, I looked into the source code at http://www.dotnetframework.org/Search.aspx:
Application.Run() ends up calling a private method named Application.RunInternal().
RunInternal() checks the visibility of the Window object that was passed in to the Run() method.
If the Visibility property is not Visibility.Visible, a call to Window.Show() is made.
I then looked at the source for System.Windows.Window:
Window.Show() sets the Visibility property on itself (the window) to be Visibility.Visible.
Based on this, I don't see how to force the window to stay hidden. By trying to make the window invisible at startup, I'm causing the Application object to call Window.Show(); I don't understand why the Application object even cares about the window's visibility. It's been a frustrating experience... :-(
I've seen other answers that say to not call Application.Run() and to instead set up your own event dispatchers, but that seems like overkill for something that should be easy. I just want the main window to stay hidden, for no "flicker" to appear at app startup, and for the window to become visible when I'm ready for it to do so (which happens later in my application logic).
Can anyone offer a suggestion?
Did you remove the StartupUri entry in App.xaml? If you do, the App class won't instantiate the window for you and show it. You can do this by yourself by overwriting the App.OnStartup method.
Basically, I build a composition root in this OnStartup method and just create a window at the end of the process:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Do your custom initialization code here
MainWindow = new MainWindow();
MainWindow.Show();
}
If you really want to omit the whole application build up process (which I wouldn't recommend, as you won't have features like the fallback to Application Resources), you can create a Dispatcher by yourself using this code:
var dispatcher = Dispatcher.CurrentDispatcher;
var synchronizationContext = new DispatcherSynchronizationContext(dispatcher);
SynchronizationContext.SetSynchronizationContext(synchronizationContext);
Dispatcher.Run();
The comment on this Answer finally led me to find the solution to this issue. I needed to display multiple windows on multiple screens at once, and by minimizing the window it gives me the performance I needed. Thanks.

Always disable paste

For a "common" look and feel I have an app that shows the paste/cut/copy buttons in a ribbon but I want to have paste always disabled no matter what field is selected. How do I approach this?
By default all the controls in WPF have a default ContextMenu that allows copy, paste and cut. You can disable this menu by setting this property as “{x:Null}”, but the keys associated with the menu options still work. In order to disable this commands we can use the DataObject class, which have handlers to attach any DepencencyObject in case on Copy or Paste:
DataObject.AddPastingHandler(control, this.OnCancelCommand);
DataObject.AddCopyingHandler(control, this.OnCancelCommand);
Finally in the event handler we need to cancel the current command:
private void OnCancelCommand(object sender, DataObjectEventArgs e)
{
e.CancelCommand();
}
The CancelCommand method will cancel any ApplicationCommand.Copy, ApplicationCommand.Paste and ApplicationCommand.Cut sent over the control.Now if you want to enable copying than delete the dateobject calling AddCopyingHandler code and it will work than.

Setting focus to a textbox on load of Windows Phone 7 app

I have a TwoWay data-bound textbox inside my page. I want to do something pretty simple, but I can't for the life of me work out how to do it. When the page loads, I want to set the focus to the textbox if it doesn't have any text in it.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (tbSearch.Text == "")
tbSearch.Focus();
}
This doesn't work. It doesn't fail, it just doesn't do anything. Am I doing something strange wrong? I've also tried putting this in the constructor, to no avail.
Chris
Does doing this from the pages Loaded event work for your requirements?
It will work from there.

Blend 4 Beta: How to change image source as part of Timeline

I'm trying out Blend 4 beta, and looking for a way to do a simple thing:
When a mouse is hovered on an image,
the image should change its source to a different image.
When the MouseLeave happens, the image changes back.
I know I can do it in source code, but I'm looking for a code free way to do it, without hand coding xaml.
Blend 4 seems like the perfect fit. But I've tried to set this using Event Triggers that start stories, or using Visual States, but Blend does not seem to "remember" that the image source has changed. It remembers me changing other properties of the image (such as visibility, scale etc..) but Image source is what I'm after.
Is this a bug in blend, or am I doing something wrong?
One option is creating a custom action and attaching it to the image. It still involves code, but is kinda blendy.
public class ImageSwitchAction : TriggerAction<Image>
{
public ImageSource TargetImage { get; set; }
protected override void Invoke(object o)
{
AssociatedObject.Source = TargetImage;
}
}
After adding the class to your project and building, you can drag the new behavior to any image objects in the timeline, and configure the trigger and ImageSource in the action properties. In your case, add one action for MouseEnter and one for MouseLeave.
easy way:
first, subscribe the events of your image mouse enter and mouse leave then in this events
use image.setsource(new Uri(new image url)) to set the image source for each event
if u have troubles i can post real code here latter
regards
Rui

Right click menu under winforms

I want to make a right click menu for my winforms app. It will have the same two things in it no matter where it pops up. A little hunting and pecking leads me to the conclusion that winforsm either doesn't support this in a trivial way or has hidden it under some name I havn't guessed yet. I think I can make it work with the Click event and manually creating a menu in the right place, bla bla bla... Yuck, I can thing of a half dozon thing right now that I would get wrong the first time around. Someone has got to have a better way.
Am I missing some easy way to add this?
Is there some library/widget I can copy/paste in to handle the grunt work for me?
Add a System.Windows.Forms.ContextMenuStrip item to the form, then set the form's ContextMenuStrip property.
Put a ContextMenuStrip on your form, design your menu, then set the ContextMenuStrip property on the form to the component created.
After putting a ContextMenuStrip on your form, Add it to your Control (you can do it in Control's Properties) and then , use code like this for showing menu:
private void myTree_MouseClick(object sender, MouseEventArgs e)
{
myTree.ContextMenu.Show(myTree, new Point(e.X, e.Y));
}
more on MSDN.

Resources