MediaElement Windows phone 8.1 - windows-phone-8.1

I have a weird bug here and i don't know how to call this,but here's the thing.. i have my MediaElement in my XAML ->> <MediaElement Height="10" Width="10" x:Name="Nomes"/> and i have a Button to call that Element which is mp3 audio, and works fine C# ->>
private async void AMN(object sender, RoutedEventArgs e)
{
Nomes.Source = new Uri("ms-appx:///Sounds/AMN.mp3", UriKind.RelativeOrAbsolute);
Nomes.Play();
await Task.Delay(TimeSpan.FromSeconds(1));
VibrationDevice vb = VibrationDevice.GetDefault();
vb.Vibrate(TimeSpan.FromMilliseconds(100));
await Task.Delay(TimeSpan.FromSeconds(1));
Frame.Navigate(typeof(AmericaDoNorte));
}
Here is my SecondPage Override Method
protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if(rootFrame == null)
{
return;
}
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
e.Handled = true;
}
}
And when the Vibrate Method is call i navigate through a new page,and works fine,but when i comeback to that page, the audio which supposed be to play when i hit the button play by yourself without i click, how this is possible? Thanks!

I guess your MediaElement property AutoPlay is enabled. Go to properties and have a look.
Hope this helps.
Thanks!

Related

Infinite Scrolling in a ListView

I'm trying to implemented infinite scrolling in my listview.
The approach is to get the scroll bar within the listview and hook an event handler to it's scroll event.
<CollectionViewSource x:Key="PaginatedData"
IsSourceGrouped="True"
ItemsPath="CommentaryList"
Source="{Binding paginatedCommentary}"/>
<ListView x:Name="commentaryListView"
Loaded="commentaryListView_Loaded"
ItemTemplate="{StaticResource CommentaryListTemplate}"
ItemsSource="{Binding Source={StaticResource PaginatedData}}"/>
private void commentaryListView_Loaded(object sender, RoutedEventArgs e)
{
var scrollViewer = commentaryListView.GetFirstDescendantOfType<ScrollViewer>();
var scrollbars = new List<ScrollBar>(scrollViewer.GetDescendantsOfType<ScrollBar>());
var verticalBar = scrollbars.FirstOrDefault(x => x.Orientation == Orientation.Vertical);
if (verticalBar != null)
verticalBar.Scroll += BarScroll;
}
private void BarScroll(object sender, ScrollEventArgs e)
{
if (e.ScrollEventType != ScrollEventType.EndScroll) return;
var bar = sender as ScrollBar;
if (bar == null) return;
if (e.NewValue >= bar.Maximum)
{
datacontext.pageCommentaryItems();
}
}
Can't seem to figure out what I'm doing here, but it's never hitting the BarScroll event handler.
Appreciate your help. Thank you :)
I recommend you use the ListView's ViewChanged event directly:
private void OnListViewLoaded(object sender, RoutedEventArgs e)
{
var listview = sender as ListViewBase;
if (listview != null)
{
// Attach to the view changed event
_scrollViewer = listview.GetFirstDescendantOfType<ScrollViewer>();
if (_scrollViewer != null)
{
_scrollViewer.ViewChanged += OnViewChanged;
}
}
}
private void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
// If scrollviewer is scrolled down at least 90%
if (_scrollViewer.VerticalOffset > Math.Max(_scrollViewer.ScrollableHeight * 0.9, _scrollViewer.ScrollableHeight - 150))
{
// Execute whatever you want
}
}
A much simpler solution would be to create an observable collection that implements the ISupportIncrementalLoading interface and bind it to your ListView. This will make the ListView do incremental scrolling without any need to implement it yourself. See https://marcominerva.wordpress.com/2013/05/22/implementing-the-isupportincrementalloading-interface-in-a-window-store-app/

Thumb.DragStarted event not working in visual studio 2013

I am trying to make a small music player for windows phone. I have added a slider functionality in the player. The slider works fine as the music plays. But I want to change the media according to how much i drag the slider, but cannot find any relevant event for it. I have tried value changed but it does not help. Also I tried Thumb.Dragstarted event but my visual studio gives an error.. this is the code so far:
XAML:
<Slider AllowDrop="True" x:Name="sld1" Thumb.DragStarted="sld1_DragStarted" HorizontalAlignment="Left" Margin="58,213,0,0" VerticalAlignment="Top" Width="351" ValueChanged="sld1_ValueChanged"/>
<MediaElement x:Name="bleep" Source="abcd.wav" AutoPlay="False" Visibility="Collapsed" MediaEnded="bleep_MediaEnded"/>
C#:
public Page1()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
private bool userIsDraggingSlider = false;
private void timer_Tick(object sender, EventArgs e)
{
if ((bleep.Source != null) && (bleep.NaturalDuration.HasTimeSpan) && (!userIsDraggingSlider))
{
sld1.Minimum = 0;
sld1.Maximum = bleep.NaturalDuration.TimeSpan.TotalSeconds;
sld1.Value = bleep.Position.TotalSeconds;
}
}
private void sld1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
tm_passed.Text = TimeSpan.FromSeconds(sld1.Value).ToString(#"mm\:ss");
}
private void sld1_DragStarted(object sender, DragStartedEventArgs e)
{
userIsDraggingSlider = true;
}
private void sld1_DragCompleted(object sender, DragCompletedEventArgs e)
{
userIsDraggingSlider = false;
bleep.Position = TimeSpan.FromSeconds(sld1.Value);
}
But since the DragCompleted and DragStarted events are not working I cannot provide the drag functionality to the slider.
What I identified from the Thumb class is that, you can't simply add Thumb.DragStarted="sld1_DragStarted within your Slider. You'll be able to add that kind of event only for Thumb control. Refer the bottom of the article for sample code.

Back button to go back in WebBrowser WP8 youtube site

I made a WebBrowser and it works except the back button in a youtube site. Youtube have a redirect to mobile version and this cause a loop
this code not works
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
webBrowser.InvokeScript("eval", "history.go(-1)" );
}
I know if it is a redirect (302) in this event?
webBrowser_Navigated(object sender, NavigationEventArgs e)
In the WebBrowserControl documentation you can find all available methods and events.
It is not very pretty but you should try that:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
webBrowser.GoBack();
e.Cancel = true;
}
void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
if (e.Uri.ToString().Contains("YouTubeUriYouGetAlwaysRedirectedTo"))
{
// Do some stuff here
e.Cancel = true;
}
}

How to change Background image of button on mouse click, created "Dynamically" ? - Windows Phone

I've created buttons dynamically, now unable to handle events on it, there is no usefull link on internet..
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
...
void button_MouseLeave(object sender, EventArgs e)
{
}
void button_MouseEnter(object sender, EventArgs e)
{
}
This code is not working ...
MouseEnter and MouseLeave are respectively triggered when the mouse pointer hovers on and out of an object. The event you're searching for is Click.
Other than that, your code should work:
button.Click += new RoutedEventHandler(button_Click);
void button_Click(object sender, RoutedEventArgs e)
{
// Whatever
}
Try the MouseLeftButtonDown and MouseLeftButtonUp events. Change the image to the new one in MouseLeftButtonDown and change it back with MouseLeftButtonUp.
So simple, just put this code as it is.
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
void button_MouseLeave(object sender, EventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/camBlue.png", UriKind.Relative));
Button1.Background = brush;
}
void button_MouseEnter(object sender, EventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/camRed.png", UriKind.Relative));
Button1.Background = brush;
}

caliburn winrt usercontrol and capturing custom event

I made my own slider as user control with some custom properties and one custom event. Everything works fine, but recently I start using Caliburn Micro, and I don't know how to capture my custom event.
Previously I used:
<my:RadialSlider x:Name="slider" WedgeAngle="270" ..... AngleChanged="slider_AngleChanged" />
and
public void slider_AngleChanged(object sender, ValueChangedEventArgs e)
{
.... something ....
}
Now, in Caliburn project I tried:
<my:RadialSlider x:Name="slider" WedgeAngle="270" ..... cal:Message.Attach="[Event AngleChanged] = [Action slider_AngleChanged($eventArgs)]" />
and in my ViewModel:
public void slider_AngleChanged(object sender, ValueChangedEventArgs e)
{
.... something ....
}
But, this doesn't work...
So, how to capture this event?
Slider UC code-behind:
public delegate void AngleChangedEventHandler(object sender, ValueChangedEventArgs e);
public sealed partial class RadialSlider : UserControl
{
public event AngleChangedEventHandler AngleChanged;
private void OnAngleChanged(ValueChangedEventArgs e)
{
if (AngleChanged != null)
AngleChanged(this, e);
}
public static readonly DependencyProperty WedgeAngleProperty = DependencyProperty.Register("WedgeAngle", typeof(double), typeof(RadialSlider), new PropertyMetadata((double)270, new PropertyChangedCallback(OnPropertyWedgeAngleChanged)));
public double WedgeAngle
{
get { return (double)this.GetValue(WedgeAngleProperty); }
set { this.SetValue(WedgeAngleProperty, value); }
}
private static void OnPropertyWedgeAngleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as RadialSlider).UpdateControls();
if (e.NewValue != e.OldValue)
{
(sender as RadialSlider).OnAngleChanged(new ValueChangedEventArgs((double)e.OldValue, (double)e.NewValue));
}
}
}
You need to use a routed event. This has to do with how events bubble up the visual tree and how Caliburn.Micro attaches itself to them. Standard events should be avoided on controls or UI widgets in any tech using Xaml as the loose out on some pretty funky features (bubble up / down).