In this article we are going to see how to setup a Radio element in the Windows Phone application. To access the Radio element we need to create an instance of the FMRadio class which has a list of properties of which we can use it across to setup as per our requirement. Let us see the steps on how to achieve this task in our Windows Phone application development. Open Visual Studio 2012 IDE and create a new Windows Phone project with a valid project name as shown in the screen below. [more]
Note: This will work only with Windows phone 7.1 version and will not work with Windows Phone 8
Clicking on OK will create the project and the solution with the list of default files and folders that are required to run the application. It will take some time to create these files based on your system configuration, so once everything is ready we can see the Visual Studio IDE with the project as shown in the screen below.
Let us add some control to trigger the Radio on click of an event, we will add a button which triggers the FMRadio class and we will set up some properties which are used to set the FM Radio based on the requirement.
XAML Code:
[code:c#]<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="FM Radio" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!–ContentPanel – place additional content here–>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Start FM Radio" HorizontalAlignment="Left" Margin="10,22,0,0" VerticalAlignment="Top" Width="436" Height="99" Click="Button_Click"/>
</Grid>
FMRadio class has a list of properties which we can use to setup as per our requirement, we need to create an instance of the FMRadio class and assign to the properties as shown in the code below.
C# Code:
[code:c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using F5debugHowto80.Resources;
using Microsoft.Devices.Radio;
namespace F5debugHowto80
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
FMRadio Radio = FMRadio.Instance;
Radio.PowerMode = RadioPowerMode.On;
}
}
}
Now we are done with the code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected output.