How to disable media element (.gif) while program is running - c#

I have a media element in my wpf application which includes a loading gif. When the user selects a long running process, it basically displays a loader gif to let the user know that the process is running. What I am trying to do is to disable it once the long running process has stopped running. I have included the XAML and process ended event for it below. Also, the main part, this is how I am starting the gif and stopping the gif. I might be wrong there.
loadGif.Play();
// Long running process(s)
loadGif.Pause();
loadGif.Close();
XAML BELOW:
<MediaElement Name="loadGif" HorizontalAlignment="Left" LoadedBehavior="Manual" MediaEnded="process_Ended" Height="22" Margin="609,226,0,0" VerticalAlignment="Top" Width="31" RenderTransformOrigin="-0.258,0.523" Source="C:\\ajax-loader.gif"/>
Process Ended:
private void process_Ended(object sender, RoutedEventArgs e)
{
loadGif.Position = new TimeSpan(0, 0, 1);
loadGif.Play();
}
Also, I forgot to mention what happens right now, at the moment, the gif starts when its supposed to start and after the process(s) are done, it disappears and a black square takes its spot. Since the background of my application is white, its just a small black box sitting there after the processing is done. Essentially, the media element spot goes all black. Thanks for any help!

Set the Visibility of the MediaElement to either Collapsed or Hidden, depending on how you want the layout to work.
loadGif.Visibility = Visibility.Collapsed;

Related

Splashscreen Rect coordinates returning Screen dimensions on Win 10 Mobile

I am trying to create an extended SplashScreen on my UWP app for which the SplashScreen image coordinates are needed. I am using the following code in App.xaml.cs:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
if (e.SplashScreen != null)
var SplashLocation = e.SplashScreen.ImageLocation;
//Rest of initialization...
}
However, if I inspect SplashLocation which is a Rect, SplashLocation.Height and SplashLocation.Width return 1280 and 768 which is the dimension of my phone's screen. All other properties are 0.
Is this a known bug in Win 10 Mobile [build 10536]? It works fine on desktop Windows 10.
Actually the issue is that the code given on the MSDN docs is wrong. Have a look at their samples on Github instead. You can find it here:
Splash Screen Sample
Notice the differences between the MSDN docs and the sample:
ExtendedSplash DOES NOT extend page
They use a ScaleFactor for phone
The root element of the XAML is Grid and not page.
I followed their sample (after hours of wondering) and it all worked well. I hope they update their docs to reflect the correct thing.
As a workaround, here's what I did:
Create an Image in XAML inside a Grid (and NOT inside a Canvas as suggested by the msdn Docs)
<Image Name="ExtSplash"
Stretch="Uniform"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Source="Assets/Splash/SplashScreen.png"/>
Now, the trick is to make it work on both Mobile (which doesn't give correct Rect coordinates and Desktop (which works as expected)
So, in the loaded event of Page in code behind, I used this code:
if (splash.Top < splash.Left)
ExtSplash.Margin = new Thickness(splash.Top);
else
ExtSplash.Margin = new Thickness(splash.Left);
where Splash = e.SplashScreen.ImageLocation from the OnLaunched event in App.xaml.cs.
This works because, Splash.Top and Splash.Left both return 0 in case of Mobile. So the app just displays the splashscreen fully extended to the width. In case of Desktop, the Image may have some Left or Top Coordinates as it's reported correctly on desktop, where I set them as Margin for the centrally aligned uniform stretched image.
NOTE 1: There may be cases where a slight variation may occur between splashscreen and the ExtendedSplash image. But in my testing it worked fine. It will continue to work even when in a later build MS fixes the issue, so you can take your time to implement the standard method then.
NOTE 2: I had some issues with this logic if scale-400 splashscreen image was provided. For some reason WM10 picks up the highest resolution available. So I just supplied 100, 125, 150 and 200 scale images (skipped the 400)
I ended up ditching the SplashScreen.ImageLocation approach. The Microsoft tutorial does not work on mobile at the time of writing, and the sample is hackish. Instead I used a simple layout like this for the extended splash screen:
<Grid>
<Image x:Name="splashScreenImage" Source="Assets/SplashScreen.png" MaxWidth="620" MaxHeight="300"/>
<!-- plus some other control -->
</Grid>
The magic is to use MaxWidth/MaxHeight.
On mobile the the image will be slightly different positioned vertically (because of status bar and navigation bar I guess), but otherwise correct including size. On desktop the image seems to be exactly positioned and sized as the initial splash.

Acces violation while scaling with a composite transform on wp8.1

I have created a flashlight app for Windows Phone. As i already acces the camera to enable the flash, i thought it would be a nice feature to also allow the user to view the previewframes, and be able to zoom in to get a better look at things.
I have a canvas with a composite transform (as i also have to rotate the camera to match potrait mode). I use a slider (started with pinch, but i want one hand control) to increase and decrease the scale of the canvas, effectively zooming in and out. So far so good and on windows Phone 8 (HTC 8x) this works fine, i can go crazy on the slider and nothing bad happens.
On Windows Phone 8.1 (Lumia 930) however, at seemingly randoms moments when moving the slider, the app just closes down and in the debug output the following line is shown:
The program '[3164] TaskHost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
I have no idea how to find the cause of this as the moment of the crash seems to be different, its just Always related to the scaling of the canvas. I am hoping someone can point me in the right direction so i can solve this.
The canvas in XAML:
<Canvas x:Name="viewfinderCanvas">
<Canvas.Background>
<VideoBrush x:Name="viewfinderBrush"/>
</Canvas.Background>
<Canvas.RenderTransform>
<CompositeTransform x:Name="rt" />
</Canvas.RenderTransform>
</Canvas>
And my code for setting the scale:
private void Zoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (canZoom && !isFocusing)
{
zoomValue = Math.Round(Zoom.Value, 1);
rt.ScaleX = zoomValue * staticValue;
rt.ScaleY = zoomValue * staticValue;
ZoomValue.Text = zoomValue.ToString() + " x";
}
}
Where canzoom is set to true on the loaded event of the mainpage, and isFocusing is set to true upon the autofocus.

Windows Phone Navigation transitions delay and blank screen in between

I'm using WP Toolkit to do the transitions between my app pages, it works fine , but I get this strange delay in between transitions while navigating from one page to another, it just shows a blank screen which obviously doesn't look good, without any transition it opens the page straight away without any delay or blank screen. This has taken almost 2 days of my time and I don't know what's wrong, I'd appreciate it if someone can help me with it or suggest another page transition library .
(I tried WP7Contrib transitions but I have the same problem with that, not sure if its my app or the libraries)
In fact the background between transitions is black and to avoid that kind of behavior I solved the issue by setting the background in the App.Xaml.cs
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new TransitionFrame();
var brush = new ImageBrush
{
ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Background.jpg", UriKind.Relative)),
Opacity = 0.8d
};
RootFrame.Background = brush;
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
With that all my pages have my background and the black background is no longer displayed during the transition.
I would suggest you to create your own slide transitions between pages. Its quite simple actually. Create a storyboard and play them at onNavigatingFrom and onNavigatedTo functions in both the page you are navigating from and page you are going into respectively. It just gives me what and how i wanted in my applications. Removing additional references makes your code more optimized.

Hosting XNA inside WPF - Weird "Phantom" Window issue

I'm trying to host an XNA game inside a WPF window using the Windows Forms host control. I've got a weird problem that a "Phantom" window is created when I run the game. It is created exactly at the first call to game's Update method exits.
Here is the Update code, the default one from a new XNA project:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
The window is created after I step from the last curly brace at the very bottom. (yes, weird to have it NOT in base.Update but the after the } after it.)
I have a Windows Forms host as I've said, with the code below:
The relevant XAML is here (I've obviously got the Forms namespace etc. set up so no need to paste here):
<WindowsFormsHost Margin="12" Name="windowsFormsHost1">
<forms:Panel x:Name="p"></forms:Panel>
</WindowsFormsHost>
and in codebehind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Viewer v = new Viewer(p.Handle);
v.Run();
}
where the Viewer is my Game class (from XNA), with:
public Viewer(IntPtr handle)
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
panelHandle = handle;
graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
}
void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = panelHandle;
}
This technique is from a blog post I've found a few days ago (I can't find it again now, it is EXACTLY the same way -- with the exception that the post was old and doesn't have the issue I'm having).
The problem is that, I build and run, but along with my "normal" window, there is a phantom window created. In my main window, XNA renders correctly. But the phantom window (the Window title is my assembly's name), doesn't have anything in it, is not resizable, cursor doesn't render inside it, and it acts like the main window in the sense that when I minimize this window, my main window stops rendering the XNA content until I un-minimize that phantom window. My program doesn't quit until I close BOTH windows. (If I close the phantom only, my main window, as you guess, stops rendering my XNA content). I tried iterating over my application windows, with 'App.Current.Windows', and all I've got is my main window listed there, that's why I'm calling that semi-responsive window the "phantom" window. It is not visible in my object model in WPF.
When I wrote "XNA WPF phantom window", the first I've got was this: WPF: How to determine the origin of a phantom window?
So I went and tried the Snoop. But Snoop probably also relies on the Windows iteration, and it also doesn't see any extra window there. I use the crosshair-drag feature (you drag the cursor around the screen and Snoop tells which process and window it belongs to), and Snoop tells that the Phantom Window is actually my MainWindow. But my MainWindow is ALSO the same window, according to Snoop. So this phantom is somewhat closely related (or maybe a "child"?) of my MainWindow instance, but I need a way to close it (or at least, hide it).
Any ideas?
The two good ways of embedding XNA in WPF that I know of are in this post (one is described in the post itself, the other is described in the first link in the post):
http://blogs.msdn.com/b/nicgrave/archive/2011/03/25/wpf-hosting-for-xna-game-studio-4-0.aspx
Both have code samples and I've used the WriteableBitmap version before with good results. (I was creating a development tool that didn't need to show anything larger than 384x384 so FPS was acceptable; if you need high FPS with a large back buffer (e.g. 800x600, 720p, etc.) you'll definitely want to use the HwndHost method).
I'd recommend trying whichever one of those two best fits your needs. When you try to use the window XNA creates, XNA's WinForms window expects to be a top level window. I've tried messing around with solutions such as what you've posted in the past but have never gotten beyond the phantom window issue.
I wrote this and finished it just now, maybe this can help you. I wrote it for the express purpose of XNA <-> WPF Compatibility; that is, having XNA be able to render inside a specific Control on WPF. Give it a shot:
https://xnaml.codeplex.com/
Besides being just a workaround, I think we are stuck with this being the best until something new comes in..
After further investigation, I've found out that the phantom window is the "window handle" of the Game instance, and it is required for the game to keep running. It could be accessed as the game's window handle, and creating (actually getting reference to an existing) a form from it. The game also prevented the input mechanisms of the objects such as WPF TextBox, and I got a very inconvenient (but works for me now) workaround by deriving the TextBox class and overriding the OnKeyDown manually (it caught the even but weirdly wasn't putting in text input.) and raising a TextInput event manually. A bit hacky, but works. And with the form, I can just set the opacity to zero and move it somewhere not in the middle, and it will stay there.
But my solution is just a hacky workaround and Mike's solution is definitely better, so if you are reading this and just about to start off a new project like these, follow his answer.

C# ToolTip blocking execution?

I have no clue how to ask this question but this is a last ditch effort as I am stumped.
I have this bit of c# code that will send off to a server (using JSON) a request to draw a wms layer to a map. That server goes and does its duty and bam the wms layer appears on the map.
IN this bit of code I sometimes wanna output the times they are clicking on to render this map (its a timeline based render), so using a UserControl in c# that contains a picture box, I render this timeline and then put up a tooltip that shows the various times as the mouse moves along this pictureBox.
The Tooltip is defined as globally:
ToolTip myToolTip;
Then when I use it in my bit of code:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
//...blah blah blah stuff to calculate time where user clicked based on width of timeline drawn in the picturebox etc.
prisms.callServer("Timetable", "setSelection", selection, null); //call to server to tell it to render the map at the time defined by selection
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//...blah blah..code to calculate mTheDate showing what the time is where they are where they are hovering
myToolTip.Show(mTheDate, this, toolTipX, toolTipY, 100);
}
So when the scenario above happens, the map doesn't render until i move the mouse off the userControl that has the picturebox on it.
BUT if i take out the myToolTip.show in the MouseMove then minute I click the map renders (i realize it could have lots to do with the WMS server I am hitting etc etc, but it leads me to believe it is something with the tooltips, since removing it makes things work well enough that the map renders without having to move my mouse off the picturebox control).
So I realize this is a vague, hazy question but I am stuck and just throwing this out there for any ideas. Maybe there is something more to the tooltips I don't understand or I need a different approach that does not use the tool tips?
I should also state that the prisms.Call server does get executed (using breakpoint figured this out) even with tool tip there so not sure what is going on.
You could always hide the tooltip (if its showing) while you run the WMS call, then resume showing the tooltip.
I do not have any tooltip-specific knowledge.

Categories

Resources