mp3 file not playing in MediaElement - windows-phone-8

i just started learning windows 8 phone development form channel 9 video tutorial
when i put Mp3 file in source property of MediaElement it is not being played by my phone
<Grid x:Name="ContentPanel" Margin="-125,110,149,51" Grid.RowSpan="2">
<Grid x:Name="InnerContentPanel" Grid.Row="5" Margin="12,0,12,0">
<Button x:Name="btnRandom"
Margin="276,203,10,264"
Click="btnRandom_Click"
Content="Get Copy
"
Background="Red"
/>
</Grid>
<MediaElement x:Name="meSound"
Source="\Assets\Sound\demo.mp3"
Volume="13"
AutoPlay="false"
></MediaElement>
</Grid>
My code behind Code
private void btnRandom_Click(object sender, RoutedEventArgs e)
{
meSound.Play();
}
can anyone tell me why my phone is not playing this mp3?

Source URI of MediaElement is wrong. It should be Assets/Sound/demo.mp3.

Related

Navigation to another windows phone page error

I am developing a windows app which allows users to order Ice Creams.
I am trying to make a button to navigate to another page called "logorder" but keep getting an error. I have uploaded a picture to show the error.
Code
Public NotInheritable Class MainPage
Inherits Page
Public Property NavigationService As Object
''' <summary>
''' Invoked when this page is about to be displayed in a Frame.
''' </summary>
''' <param name="e">Event data that describes how this page was reached.
''' This parameter is typically used to configure the page.</param>
Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
' TODO: Prepare the page for display here.
' TODO: If your application contains multiple pages, ensure that you are
' handling the hardware Back button by registering for the
' Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
' If you are using the NavigationHelper provided by some templates,
' this event is handled for you.
End Sub
Private Sub HyperlinkButton_Click(sender As Object, e As RoutedEventArgs)
End Sub
Private Sub button_Copy_Click(sender As Object, e As RoutedEventArgs) Handles button_Copy.Click
NavigationService.Navigate(New Uri("/logorder.xaml", UriKind.Relative))
End Sub
End Class
Xaml...
<Page
x:Class="mobileapps.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:mobileapps"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Button x:Name="button_Copy" Content="Summary" HorizontalAlignment="Left" Margin="96,425,0,0" VerticalAlignment="Top" Width="191"/>
<HyperlinkButton Content="HyperlinkButton" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<HyperlinkButton Content="HyperlinkButton" HorizontalAlignment="Left" Margin="-232,186,0,0" VerticalAlignment="Top"/>
<HyperlinkButton Content="HyperlinkButton" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<HyperlinkButton Content="HyperlinkButton" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<HyperlinkButton Content="Log order" HorizontalAlignment="Left" Margin="134,332,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.106,-0.496" Click="HyperlinkButton_Click" NavigateUri="logorder.xaml"/>
<HyperlinkButton NavigateUri="logorder.xaml" />
</Grid>
You never set NavigationService, but try to call a method on it, so you will get a NullReferenceException
Try this
Private Sub HyperlinkButton_Click(sender As Object, e As RoutedEventArgs)
Me.Frame.Navigate(GetType(BasicPage2))
End Sub
How to perform navigation between pages in windows phone
Similar SO Question

Save all images in ListView

I am loading a bunch of images from Vk.com in a ListView.I'd like to be able to download/save all images in any list to the phone's gallery, either in a simple event or by default. How would I go about this?
This is the XAML.
<Page.BottomAppBar>
<CommandBar>
<AppBarButton FontSize="15" FontFamily="Times New Roman" Icon="Back" Label="Albums" Click="AppBarButton_Click"/>
</CommandBar>
</Page.BottomAppBar>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"> </RowDefinition>
</Grid.RowDefinitions>
<TextBlock x:Name="MyTextBlock" FontSize="35" HorizontalAlignment="Center"></TextBlock>
<ListView Grid.Row="1"
ItemsSource="{Binding}"
SelectionMode="None"
IsItemClickEnabled="False"
>
<ListView.ItemTemplate>
<DataTemplate>
<Image Stretch="UniformToFill" Source="{Binding image_final}"
AutomationProperties.Name="{Binding Title}"
Width="Auto"
Height="Auto"
></Image>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
You've tagged this as WP8 and WP8.1. The code below is for 8, however shouldn't take too much work for 8.1.
You can download and save an image to the users gallery like this:
using Microsoft.Xna.Framework.Media;
using System.Windows.Media.Imaging;
using System.IO;
WebClient client = new WebClient();
client.OpenReadCompleted += (s, ev) =>
{
var streamResourceInfo = new StreamResourceInfo(ev.Result, null);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(streamResourceInfo.Stream);
WriteableBitmap bmp = new WriteableBitmap(bitmap);
MediaLibrary library = new MediaLibrary();
using (MemoryStream stream = new MemoryStream())
{
Extensions.SaveJpeg(bmp, stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
library.SavePicture("imagename.jpg", stream.ToArray());
}
MessageBox.Show("The picture has been saved to your Pictures Hub", "Success!", MessageBoxButton.OK);
};
client.OpenReadAsync(new Uri("http://example.com/imageurl.jpg"));
Obviously put the correct image URL in the last line. And optionally replace "imagename.jpg" with the real name of the image.
This link really explains everything: saving images, and organizing them into folders. Definitely worth a read.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/769c9a6c-961c-47ea-bddf-a381db932289/saving-an-image-from-the-web-to-the-saved-pictures-folder-in-windows-phone-81-rt-c?forum=wpdevelop
Explanation:
//Create a storage folder in the Pictures Library:
StorageFolder AppFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("XYZ", CreationCollisionOption.OpenIfExists);
//Naming and storing the file:
var uri = imageuri; //image uri
var filename = Path.GetFileName(uri.LocalPath); //designates file name
var thumbnail = RandomAccessStreamReference.CreateFromUri(uri);
var remoteFile = await StorageFile.CreateStreamedFileFromUriAsync(filename, uri, thumbnail);
await remoteFile.CopyAsync(albumname, filename, NameCollisionOption.FailIfExists); //Fail if argument filename is a string by which a file is already saved.
I hope this helps.

WinRTXAMLToolkit Treeview Crash on Windows 10

I have recently upgraded my OS to Windows 10 from Windows 8.1. I'm using VS 2013 With update 4.
My app using the Treeview control from XAMLToolkit, and it works perfectly on Windows 8.1 environment. But under Windows 10, it gives me the following error.
Please help.
This is the XAMLToolkit version I've used:
nuget.org/packages/winrtxamltoolkit.windows
Exception message:
System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.
at Windows.UI.Xaml.Controls.ItemsControl.get_ItemTemplate()
at WinRTXamlToolkit.Controls.HeaderedItemsControl.PrepareHeaderedItemsControlContainer(HeaderedItemsControl control, Object item, ItemsControl parentItemsControl, Style parentItemContainerStyle)
at WinRTXamlToolkit.Controls.HeaderedItemsControl.PrepareHeaderedItemsControlContainerForItemOverride(DependencyObject element, O
Stacktrace:
at Windows.UI.Xaml.Controls.ItemsControl.get_ItemTemplate()
at WinRTXamlToolkit.Controls.HeaderedItemsControl.PrepareHeaderedItemsControlContainer(HeaderedItemsControl control, Object item, ItemsControl parentItemsControl, Style parentItemContainerStyle)
at WinRTXamlToolkit.Controls.HeaderedItemsControl.PrepareHeaderedItemsControlContainerForItemOverride(DependencyObject element, Object item, ItemsControl parent, Style parentItemContainerStyle)
at WinRTXamlToolkit.Controls.TreeView.PrepareContainerForItemOverride(DependencyObject element, Object item)
at Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)
Inner exception is null
I encountered same issue, what i did is just removed the itemtemplate inside treeview control and added separately in page resource and i refered the itemtemplate to my treeview control. it solved my issue.
<Page.Resources>
<DataTemplate x:Name="TreeViewItemTemplate">
<Grid>
<TextBlock Text="{Binding Name}" />
</Grid>
<XCDATA:DataTemplateExtensions.Hierarchy>
<XCDATA:HierarchicalDataTemplate ItemsSource="{Binding Items}" />
</XCDATA:DataTemplateExtensions.Hierarchy>
</DataTemplate>
</Page.Resources>
<XC:TreeView
ItemTemplate="{StaticResource TreeViewItemTemplate}"
ItemsSource="{Binding ObjShopItems}">
</XC:TreeView>
Seems that you are using a HierarchicalDataTemplate in your XAML code for the TreeView. Replacing the XAML with the corresponding C# code will help. We can set the DataTemplateExtensions.Hierarchy attached property in code-behind in Loaded event for the TreeView, like this:
<controls:TreeView x:Name="treeView"
Loaded="treeView_Loaded"
...
And in code-behind:
private void treeView_Loaded(object sender, RoutedEventArgs e)
{
//don't know why, but in Windows 10 if this code is as XAML, the app falls with a ComExcpetion
//so the corresponding XAML should be commented out like this:
//...
//<controls:TreeView.ItemTemplate>
// <DataTemplate>
// <!-- <data:DataTemplateExtensions.Hierarchy>
// <data:HierarchicalDataTemplate ItemsSource="{Binding Folders}" />
// </data:DataTemplateExtensions.Hierarchy> -->
// <Grid>
//...
WinRTXamlToolkit.Controls.Data.DataTemplateExtensions.SetHierarchy(treeView.ItemTemplate, new WinRTXamlToolkit.Controls.Data.HierarchicalDataTemplate
{
ItemsSource = new Windows.UI.Xaml.Data.Binding { Path = new PropertyPath("Folders") }
});
}

Back button not working when placed in AppBar

I'm working on a Windows 8.1 app. I have added a basic page to my project, which automatically adds a back button:
<Button x:Name="backButton"
Margin="39,59,39,20"
Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
Style="{StaticResource NavigationBackButtonNormalStyle}"
VerticalAlignment="Top" />
The button works fine. However, when I move this button to an AppBar, it doesn't work. The view doesn't go back to the previous page.
What is going wrong in the latter case?
The AppBar isn't in the same namespace as the page and so the Command binding to the page's NavigationHelper property doesn't resolve. This is the case for any binding of the AppBar to the page.
You can fix this by setting the AppBar's DataContext to the page in page.Loaded
XAML
<Page.BottomAppBar>
<AppBar x:Name="bottomAppBar" IsOpen="True">
<Button x:Name="backButton"
Margin="39,59,39,20"
Command="{Binding NavigationHelper.GoBackCommand}"
Style="{StaticResource NavigationBackButtonNormalStyle}"
VerticalAlignment="Top" />
</AppBar>
</Page.BottomAppBar>
C#
public BasicPage1()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += navigationHelper_LoadState;
this.navigationHelper.SaveState += navigationHelper_SaveState;
this.Loaded += BasicPage1_Loaded;
}
async void BasicPage1_Loaded(object sender, RoutedEventArgs e)
{
bottomAppBar.DataContext = this;
}
--Rob

How to show list of items on button click?

I am developing windows phone app..
I am taking 1 button and on click of that button the dropdown list of clothes should come..
how to code for this?
Problem: How to take this list on button click
Your xaml should be :
<Button x:Name="BtnShowCloth" Content="showCloth" Margin="10,5" Click="BtnAddProduct_Click"/>
<toolkit:ListPicker ExpansionMode="FullScreenOnly" x:Name="ClothListPicker" Visibilty="Collapsed" Margin="0,0,0,10" Height="100" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource AppTextBlockStyle}" Text="{Binding}"/>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource AppTextBlockStyle}" Text="{Binding}" Margin="2,10,0,0" FontSize="31"/>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
//Another way use ListBox Control
<ListBox Grid.Row="1" Name="ListBoxCloth" Height="50">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource AppTextBlockStyle}" Text="{Binding}" Margin="2,10,0,0" FontSize="31"/>
</DataTemplate>
<ListBox.ItemTemplate>
</ListBox>
Code Behind of button click fill list of cloth names. In windows phone no dropdown list exist, you should use longlistselector instead of dropdown
private void BtnAddProduct_Click(object sender, RoutedEventArgs e)
{
List<string> clothNameList = new List<string>();
clothNameList .Add("a");
clothNameList .Add("b");
clothNameList .Add("c");
clothNameList .Add("d");
clothNameList .Add("e");
//ClothListPicker.ItemsSource = clothNameList;
// ClothListPicker.Visibility = Visibility.visible;
ListBoxCloth.ItemsSource = clothNameList;
ListBoxCloth.Height = 400;
}
This is very broad question, infact does not fall in SO standards of Questions.
You should research and try something and ask only if you got stuck somewhere or if your method fails.
And to your question, it needs clarification on many things like
What is the format of your clothes data
Are you getting it from a web service or a local db or some other resource
Is it a single List or a groups of items etc
Hence, it becomes hard to answer. Please keep these things in mind, next time when asking questions.
Based on your comment:
You should split your task into multiple small tasks
First thing, learn how to fetch the data from your db
Converting that data into List of objects
Creating your UI for the data
Bind the data to the UI in your Button_Click event handler
Happy coding !!