Binding MediaElement - c#

We are trying to switch our application from code behind coding to MVVM pattern.
We have a MediaElement in our XAML file :
<MediaElement Name="MyMedia" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="MediaEnd" MediaOpened="MediaBegin" Margin="10,24,13.6,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Visibility="Visible" />
For the moment, with the code behind coding approach, we can access the MediaElement directly in the .xaml.cs file to call methods like MediaElement.play() or MediaElement.pause().
The problem is that we are currently blocked when we try to convert our code to MVVM, because we can't access the MediaElement in the .cs file.
We are trying to bind the MediaElement itself so that we can call play or pause methods, but we haven't succeeded yet.
Any ideas ?
Thank you in advance.

This link describes how to control it http://msdn.microsoft.com/en-us/library/ms748248.aspx
it uses an event but still calls the MediaElement.play() in the event handler.

Related

MediaElement XAML -> AutoPlay not a property?

I have created a MediaElement in XAML and assigned a .mpg as the source.
Looking at the XAML controls gallery from Microsoft, there are some examples such as:
<MediaPlayerElement Source="Assets/SampleMedia/fishes.wmv"
MaxWidth="400"
AutoPlay="True" />
However, I am faced with invalid markup that is returning:
The property AutoPlay was not found in type MediaElement
I have tried removing the AutoPlay and using a MediaOpened event to start the video from code behind,but there is never any trigger even?
I am not having too much success.
DO I need to add any additional resources to access certain properties?
<MediaElement x:Name="backdropMotion" Source="Images/inMotion_video.mpg"
Stretch="Fill" Margin="198,126,-326,229" Grid.Column="2"
LoadedBehavior="Play" IsEnabled="True" IsMuted="True"
Grid.Row="1" Grid.ColumnSpan="2" MediaOpened="MediaOpenedEvent"
AutoPlay="True"
>
Ultimately, I am trying to run a media element to a panel that effectively acts as an animated background, looping almost like a .gif. First step is to get the element running.

Setting the LoadedBehaviour and UnloadedBehaviour of MediaElement WPF Application(C#)

I am playing a video in a MediaElement in a WPF application. However when I am programatically invoking the Play() , Pause() or Stop() I get an error message saying:
Cannot control media unless LoadedBehavior or UnloadedBehavior is set to Manual.
I have no idea about it.
Can someone tell me how can these be set to MANUAL?
In XAML you can set the value to the properties like this:
<MediaElement x:Name="player"
LoadedBehavior="Manual"
UnloadedBehavior="Stop" ... />
Or using code behind:
player.LoadedBehavior = MediaState.Manual;
player.UnloadedBehavior = MediaState.Stop;

WPF ContextMenu for tray icon

I'm having a WPF application which I can minimize to tray. When I normal-click it, the window shows again.
Now I'm wondering how to create a simple ContextMenu?
The ContextMenu has to get filled with x options which onclick will run a function. For now I just need an 'Exit'-item linked to an 'Exit_Click' method.
Something I've tried is:
ContextMenu menu = (ContextMenu)this.FindResource("NotifierContextMenu");
menu.IsOpen = true;
menu doesn't know of any IsOpen value.
Other examples like to use a lot of different things. One of them requires me to create a HostManager for some reason.
I just need a simple ContextMenu. How can I achieve this?
As #H.B. mentioned Hardcodet's NotifyIcon is pretty darn good for WPF Taskbar icons. Sucks you don't get that out of the box with WPF but you might as well use it and address your issue than wait for Microsoft to fix it(They really should just add that library into standards)
Now to solve your issue(Using above solution):
Download the solution
Build the library
Add it to your source control if you have one and add a reference to it(Hardcodet.Wpf.TaskbarNotification.dll) in your project
Now in your MainWindow.xaml you could just have something like:
<Window ...
xmlns:tb="http://www.hardcodet.net/taskbar"
...>
...
<Grid>
<tb:TaskbarIcon>
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Click="Exit_Click"
Header="Exit" />
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
...
</Grid>
</Window>
and MainWindow.xaml.cs with the click handler like you needed:
private void Exit_Click(object sender, RoutedEventArgs e) {
Application.Current.Shutdown();
}
I do recommend spending some time looking at the examples coming with the source code of the library to familiarize yourself with your available options. Trust me wpf has it way too easy when it comes to helper libraries. Try some of qt helper libraries and you'll know what "buried in there somewhere" literally means in opensource helpers.

Add a Behavior to an Image

In my program I have an System.Controls.Image to which I want
to add a shaking Behavior from here Animate Image in button to shake
I copied the ShakingBehavior to my project to it's own class,
and I already have the Blend SDK.
Now I added the XAML Part to my Image
<Image Source="myImage.png" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Col}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
<i:Interaction.Behaviors>
<local:ShakeBehavior RepeatInterval="5" SpeedRatio="3.0"/>
</i:Interaction.Behaviors>
</Image>
Now I get an error that I cannot get rid of!
(As I do not have an english VS here, I try to translate the message)
The Interaction type has no attachable Behaviors property
probably just a namespace to add (the i alias), but I cannot bring it to work
edit
More information: The Image is contained in a button, which is inside a datatemplate.
And (at best) I want to start the animation when another button in the view is clicked.
(this Behavior shall help to find the button as a hint)
Since Mohib doesn't want to, i will post the correct line that I have missed:
this one should be without those magic unprinted values that can be found in Mohibs answer.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

wp7, listbox of ~80 items not populating when navigating back to it [duplicate]

(Scenario: Windows Phone 7 / Silverlight)
I have a ListBox that i will simplify to this XAML:
<ListBox ItemsSource="{Binding Path=ImageLinks}"> <!-- ImageLinks a collection in ViewModel -->
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path=ImageSource}" /> <!-- ImageSource is a string with the url to the image-->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now, the above code works, but the problem is that as the item is rendered/loaded or whatever it starts downloading the image but while doing so, it blocks the UI. And since more than one item fits at the time, the UI gets blocked until all of the corresponding images are downloaded.
So, the question is, how do i get this functionality without the UI being blocked while downloading the images (and avoiding redownloading all of them each time the view gets navigated to)?.
Thanks in advance.
Well problem Solved, Thanks to all of you who took the time to help me.
Delay created a solution to exactly this problem.
See his blog entry on the topic at http://blogs.msdn.com/b/delay/archive/2010/09/02/keep-a-low-profile-lowprofileimageloader-helps-the-windows-phone-7-ui-thread-stay-responsive-by-loading-images-in-the-background.aspx
You could populate ImageLinks in a secondary thread that is not tied to the UI and bind it directly from the code-behind once it is populated instead of direct XAML binding.
You could also use the PersistentImageCache class from my Kawagoe toolkit, which has been designed precisely for this use case. Let me know if it helps! :)

Categories

Resources