Geolocator.PositionChanged event gets raises when I try GeoLocator.GetPositionAsync() - c#

So, I'm getting some weird behavior when getting the current position using the GetPositionAsync method in my app. It seems to be firing the PositionChanged event when I query for the position. I'm not sure exactly what's going on and it's hard to debug since the call is asynchronous. Here's some sample code that illustrates the problem, (you'll need a Bing Maps key to test it):
MainPage.xaml.cs
using System;
using Bing.Maps;
using Windows.UI.Xaml.Input;
using Windows.Devices.Geolocation;
using Windows.UI.Core;
using GeoPositionExercise.Model;
namespace GeoPositionExercise
{
public sealed partial class MainPage
{
private Geolocator _geolocator;
private LocationIcon _locationIcon = new LocationIcon();
public MainPage()
{
InitializeComponent();
MyMap.Children.Add(_locationIcon);
_geolocator = new Geolocator();
//LoadPoly(PolyLayer, DefaultStyle, GetLocation);
_geolocator.PositionChanged += this._geolocator_PositionChanged;
}
private void _geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, new DispatchedHandler(
() =>
{
displayPosition(this, args.Position.Coordinate);
}));
}
private void displayPosition(object sender, Geocoordinate coordinate)
{
Location location = new Location(coordinate.Latitude, coordinate.Longitude);
MapLayer.SetPosition(_locationIcon, location);
MyMap.SetView(location, 15.0f);
}
private async void GetLocation(object sender, TappedRoutedEventArgs e)
{
// This raises a PositionChanged event for some reason
var position = await _geolocator.GetGeopositionAsync();
}
}
}
MainPage.xaml
<common:PageBase xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:common="using:GeoPositionExercise.Common"
xmlns:local="using:GeoPositionExercise"
xmlns:ignore="http://www.ignore.com"
x:Name="PageRoot"
x:Class="GeoPositionExercise.MainPage"
xmlns:m="using:Bing.Maps"
mc:Ignorable="d ignore"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
<Page.BottomAppBar>
<AppBar>
<AppBarButton Label="Get Location" Tapped="GetLocation">
<AppBarButton.Icon>
<FontIcon Glyph="" />
</AppBarButton.Icon>
</AppBarButton>
</AppBar>
</Page.BottomAppBar>
<Grid x:Name="MainGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,53">
<m:Map x:Name="MyMap" Credentials="YOUR BING MAP KEY HERE" >
<m:MapLayer x:Name="PinLayer"/>
<m:MapLayer x:Name="InfoboxLayer" Visibility="Collapsed">
<Grid x:Name="Infobox" Width="350">
<Border Background="Black" Opacity="0.8" BorderBrush="White" BorderThickness="2" CornerRadius="5"/>
<Grid Margin="5">
<StackPanel HorizontalAlignment="Left" Margin="10">
<TextBlock x:Name="InfoboxTitle" FontSize="18" Width="290" HorizontalAlignment="Left" TextWrapping="Wrap" />
<WebView x:Name="InfoboxDescription" Width="330" Height="100" Margin="0,10,0,0"/>
</StackPanel>
</Grid>
</Grid>
</m:MapLayer>
</m:Map>
</Grid>
</common:PageBase>
And finally a user control LocationIcon:
<UserControl
x:Class="GeoPositionExercise.Model.LocationIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GeoPositionExercise.Model"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Canvas>
<Ellipse Height="25" Width="25" Fill="#FFF0F0F0" Margin="-12.5,-12.5,0,0"></Ellipse>
<Ellipse Height="20" Width="20" Fill="Black" Margin="-10,-10,0,0"></Ellipse>
<Ellipse Height="10" Width="10" Fill="White" Margin="-5,-5,0,0"></Ellipse>
</Canvas>
</UserControl>
Make sure to have the right references and target a X64 or x82 platform before compiling. You can test this by clicking the "Get Location" app bar button, it'll always center the map at the current location. I'm not really sure why this is occuring, since the only place I call displayPosition is in the _geolocator_PostionChanged method.

Related

UserControl resizing at runtime

Hi fellow programmers,
I'm working on a WPF software that uses a Canvas to display and move graphic objects.
These graphic objects are UserControls containing labels or rectangles :
<UserControl x:Class="DashEditor.Views.MovableObject"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" >
<Grid Name="ControlLayout">
<StackPanel x:Name="DisplayPanel" >
<Canvas x:Name="graphicObjectCanvas" Width="100" Height="50">
<Viewbox x:Name="graphicObjectViewBox" Width="100" Height="50" IsEnabled="False" Canvas.Left="0" Canvas.Top="0" Stretch="Fill"/>
</Canvas>
</StackPanel>
</Grid>
I need to resize these UserControls, I saw examples with thumbs but I can't figure out how to use it in a UserControl.
Thank you for your help !
OK, I found a working solution to resize my canvas graphic objects.
I added a thumb into my usercontrol which become visible when a graphic object is selected :
<Grid Name="ControlLayout">
<StackPanel x:Name="DisplayPanel">
<Border x:Name="CanvasBorder" BorderThickness="1">
<Canvas x:Name="graphicObjectCanvas" Width="100" Height="50" Background="Aquamarine">
<Viewbox x:Name="graphicObjectViewBox" Width="100" Height="50" IsEnabled="False" Stretch="Fill"/>
<Thumb x:Name="myThumb" Canvas.Left="80" Canvas.Top="30" Width="20" Height="20" DragDelta="myThumb_DragDelta" Visibility="Hidden"
PreviewMouseLeftButtonDown="myThumb_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="myThumb_PreviewMouseLeftButtonUp" BorderBrush="Blue" BorderThickness="2"/>
</Canvas>
</Border>
</StackPanel>
</Grid>
And this is the code behind that changes my graphic object's properties :
private void myThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
double yadjust = graphicObjectViewBox.Height + e.VerticalChange;
double xadjust = graphicObjectViewBox.Width + e.HorizontalChange;
if ((xadjust >= 0) && (yadjust >= 0))
{
graphicObjectViewBox.Width = xadjust;
graphicObjectViewBox.Height = yadjust;
graphicObjectCanvas.Width = xadjust;
graphicObjectCanvas.Height = yadjust;
Width = (int)xadjust;
Height = (int)yadjust;
XapParent.Width = (int)xadjust;
XapParent.Height = (int)yadjust;
Canvas.SetLeft(myThumb, Canvas.GetLeft(myThumb) + e.HorizontalChange);
Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) + e.VerticalChange);
}
}

RDLC report not showing up inside a user control

I am trying to show a rdlc report inside a user control. The problem is that it does not show when I run the project.
Before running the project I can see it in xaml designer.
After Running project:
Here is my code:
Report User Control:
<UserControl x:Class="CPOSApplication.UserControls.Reports.ReportViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="#FFFFFFFF"
xmlns:repview="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms">
<Grid>
<WindowsFormsHost x:Name="winform" Background="White" Visibility="Visible">
<repview:ReportViewer BackColor="white" x:Name="salesReportViewer"/>
</WindowsFormsHost>
</Grid>
Another UserControl where it is being used:
<UserControl x:Class="CPOSApplication.UserControls.Reports.SalesReport"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:uc="clr-namespace:CPOSApplication.UserControls.Reports"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="900">
<Grid Style="{DynamicResource GridsCustomStyle}" x:Name="SearchShelfGri">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition MinWidth="100" />
</Grid.ColumnDefinitions>
<Border Grid.ColumnSpan="2" Style="{DynamicResource SearchGridHeaderBorder}">
<DockPanel Grid.Row="0">
<Label x:Name="GridLabel" Style="{DynamicResource HeadingLabelsCustomStyle}" Content="Sales Report-:"/>
</DockPanel>
</Border>
<uc:ReportViewer x:Name="salesReport" Grid.Column="0" Background="White" Grid.Row="1" Margin="0,0"/>
<uc:FilterSalesReport x:Name="filterShelf" Grid.Column="1" Grid.Row="1" Background="AliceBlue" Width="300" Padding="2,0,0,0" Margin="0,0"/>
</Grid>
It's .cs File
public partial class SalesReport : UserControl
{
public SalesReport()
{
InitializeComponent();
salesReport.salesReportViewer.ProcessingMode = ProcessingMode.Local;
salesReport.salesReportViewer.LocalReport.ReportPath = #"C:\Users\Safi\Documents\Visual Studio 2012\Projects\CPOS\CPOS\CPOSApplication\Reports\QuarterSaleInvoiceReport.rdlc";
salesReport.salesReportViewer.RefreshReport();
salesReport.salesReportViewer.LocalReport.ReportEmbeddedResource = "CPOSApplication.Reports." + ReportType.QuarterSaleInvoice.ToString() + "Report.rdlc";
salesReport.salesReportViewer.RefreshReport();
}
}
//in your CS file, create this method
public void GenReport()
{
YourReportViewfname.LocalReport.ReportPath = #"paste Your RDLC Location";
YourClass rptclsProp01 = new YourClass();
var data = rptclsProp01.YourList();
ReportDataSource dataSource = new ReportDataSource("YourDataSet", data);
YourReportViewfname.LocalReport.DataSources.Clear();
YourReportViewfname.LocalReport.DataSources.Add(dataSource);
YourReportViewfname.RefreshReport();
YourReportViewfname.Show();
}
//then Button Click event, just call the method
this.YourUCfnam.salesReport.GenReport();
//hope that report will show

Errors when editting existing WP8 project

So I am just starting out with this stuff and have run into some strife. There was a pivot soundboard application that I needed to change into a series of panorama pages but have the same functionality.
I have finished all the xaml and c# appropriately but I think because I had to create a new MainPage and delete the older one I have created several errors. I suspect they all have a similar cause so I will just provide three.
Error 1 The type 'SoundBoard.MainPage' already contains a definition
for 'LayoutRoot' \SoundBoard\obj\Debug\Cartoons.g.cs
Error 30 The name 'AudioPlayer' does not exist in the current
context \SoundBoard\Cartoons.xaml.cs
Error 7 Type 'SoundBoard.MainPage' already defines a member called
'InitializeComponent' with the same parameter types
\SoundBoard\obj\Debug\Cartoons.g.cs
I'll link my current MainPage and Cartoons and hopefully you guys can help me out.
MainPage.xaml:
<phone:PhoneApplicationPage
x:Class="SoundBoard.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignData SampleData/SampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="SoundBoard" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="Categories" 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="Warnings" HorizontalAlignment="Left" Margin="244,304,0,0" VerticalAlignment="Top" Height="160" Width="182" Background="Red" Click="Button_Click_3"/>
<Button Content="Taunts" HorizontalAlignment="Left" Margin="27,304,0,0" VerticalAlignment="Top" Height="160" Width="182" Background="Red" Click="Button_Click_2"/>
<Button Content="Cartoons" HorizontalAlignment="Left" Margin="244,123,0,0" VerticalAlignment="Top" Height="160" Width="182" Background="Red" Click="Button_Click_1"/>
<Button Content="Animals" HorizontalAlignment="Left" Margin="27,123,0,0" VerticalAlignment="Top" Height="160" Width="182" Background="Red" Click="Button_Click"/>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
MainPage.xaml.cs:
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;
namespace SoundBoard
{
public partial class FirstPage : PhoneApplicationPage
{
public FirstPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Animals.xaml", UriKind.Relative));
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Cartoons.xaml", UriKind.Relative));
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Taunts.xaml", UriKind.Relative));
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Warnings.xaml", UriKind.Relative));
}
}
}
Cartoons.xaml:
<phone:PhoneApplicationPage
x:Class="SoundBoard.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignData SampleData/SampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot contains the root grid where all other page content is placed-->
<Grid x:Name="LayoutRoot">
<phone:Panorama Title="{Binding Path=LocalizedResources.ApplicationTitle,
Source={StaticResource LocalizedStrings}}">
<!--Panorama item one-->
<phone:PanoramaItem Header="Cartoons">
<Grid>
<Image HorizontalAlignment="Center" Height="431" Margin="10,10,0,0" VerticalAlignment="Top" Width="400" Source="/Assets/cartoon.jpg"/>
</Grid>
</phone:PanoramaItem>
<!--Panorama item two-->
<phone:PanoramaItem Header="Sounds">
<phone:LongListSelector
Margin="0,0,-12,0"
ItemsSource="{Binding Cartoons.Items}"
LayoutMode="Grid"
GridCellSize="150,150"
>
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid Background="{StaticResource PhoneAccentBrush}">
<Grid VerticalAlignment="Top" HorizontalAlignment="Right"
Width="40" Height="40" Margin="0,6,6,0">
<Ellipse Stroke="{StaticResource PhoneForegroundBrush}"
StrokeThickness="3" />
<Image Source="Assets/AppBar/Play.png" />
</Grid>
<StackPanel VerticalAlignment="Bottom">
<TextBlock Text="{Binding Title}"
Margin="6,0,0,6"/>
</StackPanel>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</phone:PanoramaItem>
<!--Panorama item three-->
<phone:PanoramaItem Header="Record">
<Grid>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<MediaElement x:Name="AudioPlayer" AutoPlay="False" />
<StackPanel>
<ToggleButton Content="Record"
Checked="RecordAudioChecked"
Unchecked="RecordAudioUnchecked"/>
<Grid Width="200"
Height="200"
Name="ReelGrid"
RenderTransformOrigin=".5,.5">
<Grid.RenderTransform>
<CompositeTransform />
</Grid.RenderTransform>
<Ellipse Fill="{StaticResource PhoneAccentBrush}" />
<Ellipse Height="20"
Width="20"
Fill="{StaticResource PhoneForegroundBrush}" />
<Rectangle Height="20"
Width="20"
Margin="0,20,0,20"
VerticalAlignment="Top"
Fill="{StaticResource PhoneForegroundBrush}" />
<Rectangle Height="20"
Width="20"
Margin="0,20,0,20"
VerticalAlignment="Bottom"
Fill="{StaticResource PhoneForegroundBrush}" />
<Rectangle Height="20"
Width="20"
Margin="0,0,20,0"
HorizontalAlignment="Right"
Fill="{StaticResource PhoneForegroundBrush}" />
<Rectangle Height="20"
Width="20"
Margin="20,0,0,0"
HorizontalAlignment="Left"
Fill="{StaticResource PhoneForegroundBrush}" />
</Grid>
<Button Name="PlayAudio" Content="Play" Click="PlayAudioClick" />
</StackPanel>
</Grid>
</Grid>
</phone:PanoramaItem>
</phone:Panorama>
</Grid>
</phone:PhoneApplicationPage>
Cartoons.xaml.cs:
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 SoundBoard.Resources;
using Coding4Fun.Toolkit.Audio;
using Coding4Fun.Toolkit.Audio.Helpers;
using System.IO;
using System.IO.IsolatedStorage;
using Coding4Fun.Toolkit.Controls;
using SoundBoard.ViewModels;
using Newtonsoft.Json;
namespace SoundBoard
{
public partial class Cartoons : PhoneApplicationPage
{
private MicrophoneRecorder _recorder = new MicrophoneRecorder();
private IsolatedStorageFileStream _audioStream;
private string _tempFileName = "tempWav.wav";
public Cartoons()
{
InitializeComponent();
DataContext = App.ViewModel;
BuildLocalizedApplicationBar();
}
// Load data for the ViewModel Items
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
// verifying our sender is actually a LongListSelector
if (selector == null)
return;
SoundData data = selector.SelectedItem as SoundData;
// verifying our sender is actually SoundData
if (data == null)
return;
if (File.Exists(data.FilePath))
{
AudioPlayer.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute);
}
else
{
using (var storageFolder = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = new IsolatedStorageFileStream(data.FilePath, FileMode.Open, storageFolder))
{
AudioPlayer.SetSource(stream);
}
}
}
selector.SelectedItem = null;
}
private void BuildLocalizedApplicationBar()
{
ApplicationBar = new ApplicationBar();
ApplicationBarIconButton recordAudioAppBar =
new ApplicationBarIconButton();
recordAudioAppBar.IconUri = new Uri("/Assets/AppBar/Save.png", UriKind.Relative);
recordAudioAppBar.Text = AppResources.AppBarSave;
recordAudioAppBar.Click += recordAudioAppBar_Click;
ApplicationBar.Buttons.Add(recordAudioAppBar);
ApplicationBar.IsVisible = false;
}
void recordAudioAppBar_Click(object sender, EventArgs e)
{
InputPrompt fileName = new InputPrompt();
fileName.Message = "What should we call the sound?";
fileName.Completed += FileNameCompleted;
fileName.Show();
}
private void FileNameCompleted(object sender, PopUpEventArgs<string, PopUpResult> e)
{
if (e.PopUpResult == PopUpResult.Ok)
{
// Create a sound data object
SoundData soundData = new SoundData();
soundData.FilePath = string.Format("/customAudio/{0}.wav", DateTime.Now.ToFileTime());
soundData.Title = e.Result;
// Save the wav file into the DIR /customAudio/
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
if (!isoStore.DirectoryExists("/customAudio/"))
isoStore.CreateDirectory("/customAudio/");
isoStore.MoveFile(_tempFileName, soundData.FilePath);
}
// Add the SoundData to App.ViewModel.CustomSounds
App.ViewModel.CustomSounds.Items.Add(soundData);
// Save the list of CustomSounds to isolatedStorage.ApplicationSettings
var data = JsonConvert.SerializeObject(App.ViewModel.CustomSounds);
IsolatedStorageSettings.ApplicationSettings[SoundModel.CustomSoundKey] = data;
IsolatedStorageSettings.ApplicationSettings.Save();
// We'll need to modify sound model to retrieve CustomSounds
//from isolatedStorage.ApplicationSettings
NavigationService.Navigate(new Uri("", UriKind.RelativeOrAbsolute));
}
}
private void RecordAudioChecked(object sender, RoutedEventArgs e)
{
PlayAudio.IsEnabled = false;
ApplicationBar.IsVisible = false;
RotateCirlce.Begin();
_recorder.Start();
}
private void RecordAudioUnchecked(object sender, RoutedEventArgs e)
{
_recorder.Stop();
SaveTempAudio(_recorder.Buffer);
PlayAudio.IsEnabled = true;
ApplicationBar.IsVisible = true;
RotateCircle.Stop();
}
private void SaveTempAudio(MemoryStream buffer)
{
//defensive...
if (buffer == null)
throw new ArgumentNullException("Attempting a save on empty sound buffer.");
//Clean out hold on audioStream
if (_audioStream != null)
{
AudioPlayer.Stop();
AudioPlayer.Source = null;
_audioStream.Dispose();
}
var bytes = buffer.GetWavAsByteArray(_recorder.SampleRate);
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
if (isoStore.FileExists(_tempFileName))
isoStore.DeleteFile(_tempFileName);
_tempFileName = string.Format("{0}.wav", DateTime.Now.ToFileTime());
_audioStream = isoStore.CreateFile(_tempFileName);
_audioStream.Write(bytes, 0, bytes.Length);
//Play ... SetSource of a mediaElement
AudioPlayer.SetSource(_audioStream);
}
}
private void PlayAudioClick(object sender, RoutedEventArgs e)
{
AudioPlayer.Play();
}
}
}
As I said it is probably something dumb but I am really stuck so would really appreciate any advice.
Thanks in advance.
It seems that all the problems reside in auto-generated files Cartoons.g.cs. The only error that points to non fully auto-generated file is this :
The name 'AudioPlayer' does not exist in the current context \SoundBoard\Cartoons.xaml.cs
Which complaining about AudioPlayer doesn't exists when it does exist, declared in Cartoons.xaml. So that's all weird errors.
First thing I will do is clean up (from visual studio, or manually delete bin and debug folders) and rebuild the project, and or even restart Visual Studio. Those steps can fix weird errors most of the time.
The problem is that both Cartoons.xaml and MainPage.xaml have the same line x:Class="SoundBoard.MainPage". I'm not sure but think that Cartoons.xaml should have different value for x:Class attribute

Shapes doesn't show on canvas when drawn programmatically

I'm trying to crate simple app for my Windows Phone device where I can programmatically draw some shapes on canvas. When I write code to define shape in xaml everything looks great but when I try to draw same shape in code, nothing shows on canvas.
Xaml code
<phone:PhoneApplicationPage
x:Class="TestApp.ResultPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="TestApp" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Margin="14,10,10,86" Grid.Row="1">
<Canvas x:Name="resultCanvas" Margin="10,10,10,0" Background="White" MaxHeight="590" VerticalAlignment="Center" Height="590" Width="436" MaxWidth="436" HorizontalAlignment="Center">
<Canvas.Clip>
<RectangleGeometry Rect="0, 0, 436, 590"/>
</Canvas.Clip>
</Canvas>
</Grid>
<Button x:Name="drawButton" Content="Return" HorizontalAlignment="Left" Margin="14,615,0,0" Grid.Row="1" VerticalAlignment="Top" Height="81" Width="456" Click="drawButton_Click"/>
</Grid>
C# code for drawing line
public void Draw()
{
Line line = new Line()
{
StrokeThickness = 2,
Stroke = new SolidColorBrush(Colors.Blue),
X1 = 100,
X2 = 200,
Y1 = 100,
Y2 = 200,
};
resultCanvas.Children.Add(line);
}
private void drawButton_Click(object sender, RoutedEventArgs e)
{
Draw();
}

how to show different windows in main window?

In my c# Wpf standalone application i created Main Window and added to my project another 5 windows
I put in Main Window a button by name of every window from these 5 windows.
my question is: when i press any button in Main Window..how to show its related window in (Windows Area) in Main Window?? e.g. if i press history button ..how to show History window in that (Windows Area) of Main Window??
Note: i don't want to use pages or navigation windows with navigation controls in my application.
Just use UserControl's and add them to your Container Control, when you click your buttons.
Here is a simplistic example:
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="History" Height="53" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Precaution" Height="53" HorizontalAlignment="Left" Margin="109,12,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
<Button Content="Uses" Height="53" HorizontalAlignment="Left" Margin="208,12,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" />
<Button Content="Side Effects" Height="53" HorizontalAlignment="Left" Margin="314,12,0,0" Name="button4" VerticalAlignment="Top" Width="75" Click="button4_Click" />
<Button Content="New Item" Height="53" HorizontalAlignment="Left" Margin="405,12,0,0" Name="button5" VerticalAlignment="Top" Width="75" Click="button5_Click" />
<Viewbox Height="209" HorizontalAlignment="Left" Margin="12,90,0,0" Name="Container" VerticalAlignment="Top" Width="479" />
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
History use1 = new History();
Precaution use2 = new Precaution();
Uses use3 = new Uses();
SideEffect use4 = new SideEffect();
NewItem use5 = new NewItem();
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Container.Child = use1;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Container.Child = use2;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
Container.Child = use3;
}
private void button4_Click(object sender, RoutedEventArgs e)
{
Container.Child = use4;
}
private void button5_Click(object sender, RoutedEventArgs e)
{
Container.Child = use5;
}
}
Dummy UserControls
<UserControl x:Class="WpfApplication1.History"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="AliceBlue">
<Grid Width="479" Height="209">
</Grid>
</UserControl>
<UserControl x:Class="WpfApplication1.Precaution"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="LightCoral" >
<Grid Width="479" Height="209">
</Grid>
</UserControl>
<UserControl x:Class="WpfApplication1.Uses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="LightGreen" >
<Grid Width="479" Height="209">
</Grid>
</UserControl>
<UserControl x:Class="WpfApplication1.SideEffect"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="PapayaWhip" >
<Grid Height="209" Width="479">
</Grid>
</UserControl>
<UserControl x:Class="WpfApplication1.NewItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="LightGoldenrodYellow">
<Grid Height="209" Width="479">
</Grid>
</UserControl>

Categories

Resources