Going from Page to Window with Timer on Gives a STA Problem? - c#

I have a problem where I run my program and it gives me a STA Error on my "Test" Window. It does not have an error if I don't have a timer going from the Page I am running till the window. I'll show and Example:
public Page1()
{
InitializeComponent();
Task.Delay(2000).ContinueWith(_ =>
{
var page = new TestW();
page.Show();
}
);
}
This is from my Page1 to open up my TestW ( Test Window )
My main code looks like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Luk_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Button_Click(object sender, RoutedEventArgs e) // Login
{
Main.Content = new Page1();
Framep.Visibility = Visibility.Visible;
GridS.Visibility = Visibility.Hidden;
}
}
And my XAML Code:
<Window x:Class="date_app.MainWindow"
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:local="clr-namespace:date_app"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="400"
ResizeMode="NoResize"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
AllowsTransparency="True"
Background="Transparent"
>
<Border BorderBrush="Black"
BorderThickness="1.5"
CornerRadius="10"
>
<Border.Background>
<ImageBrush x:Name="ImageBrush" ImageSource="Images\bgapp.png" Stretch="None"/>
</Border.Background>
<Grid>
<StackPanel x:Name="Framep">
<Frame x:Name="Main" />
</StackPanel>
<StackPanel x:Name="GridS">
<!--X Luk Knappen.-->
<Grid>
<Button BorderBrush="Transparent" Name="Luk" Margin="360, 10, 10, 0" Background="Transparent" Click="Luk_Click">
<Button.Content>
<Image Source="Images\Ikke-navngivet.png" Height="20" Width="35" IsHitTestVisible="False" />
</Button.Content>
</Button>
</Grid>
<Grid>
<Image Source="Images\DateL.png" Height="200"/>
</Grid>
<!--Username.-->
<Grid>
<TextBox Style="{StaticResource WatermarkTextbox}" Name="Email" Height="30" Width="300" FontSize="20" FontFamily="Comic Sans MS" Text="Hello" Margin="0 40 0 0" TextChanged="Email_TextChanged"/>
</Grid>
<!--Password.-->
<Grid>
<TextBox Style="{StaticResource WatermarkTextbox1}" Name="Pass" Height="30" Width="300" FontSize="20" FontFamily="Comic Sans MS" Text="Hello" Margin="0 35 0 0" TextChanged="Pass_TextChanged"/>
</Grid>
<!--Login Knap.-->
<Grid>
<Button Grid.Column="0" Content="Log ind" Width="80" Height="30" FontSize="20" Margin="0 100 0 0" Click="Button_Click" />
</Grid>
<!--Opret Bruger-->
<Grid>
<Button Grid.Column="0" Content="Opret Bruger" Width="140" Height="30" FontSize="20" Margin="0 20 0 0" />
</Grid>
</StackPanel>
</Grid>
</Border>
Page1 Code:
<Page x:Class="date_app.Page1"
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:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
xmlns:local="clr-namespace:date_app"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="400"
Title="LoadingM">
<Border BorderBrush="Black"
BorderThickness="1.5"
CornerRadius="10"
>
<Border.Background>
<ImageBrush x:Name="ImageBrush" ImageSource="Images\bgapp.png" Stretch="None"/>
</Border.Background>
<Grid>
<TextBlock Text="LOADING. . ." FontSize="50" FontFamily="Comic Sans MS"/>
<Image gif:AnimationBehavior.SourceUri="Images\CatGif.gif" />
</Grid>
</Border>
The problem I have: If I run with the Task Delay My test Window gets an STA Error that I don't know how to fix.
If I run without the Task Delay It just opens it all up but no STA Error.
I am trying to do this:
When I press on the Login button It is supposed to open the Page1 and hide the main window for a " Loading " Screen and then after the delay to close Main window + Page1 down to Open up the New Window. Been sitting with this for a little while :) Thanks in advance!
-- Test Window Is plain. No code there.
This is the Background.

The problem is that the Action given to ContinueWith will not be exectued on the UiThread but on a free ThreadPool-Thread, so the new TestW object will be constructed and used by a non STA-Thread. But any WPF-UI-component must be constructed and used by an STA-Thread.
The solution is to replace your Task.Delay() with
Task.Delay(2000).ContinueWith(_ =>
{
Dispatcher.Invoke(() =>
{
var page = new TestW();
page.Show();
});
});
This will delegate your execution back to the UiThread.

Related

WPF minimizing window into bottom of parrent window

I try to make a "email messenger". I have two windows. Main window, there is ribbon with bookmarks and one button (New message). This button invoke second window NewMessage. The Mainwindow is owner of the NewMessage window.
MainWindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NewMessage newmessage = new NewMessage();
newmessage.Show();
}
}
}
MainWindow XAML:
<Window x:Class="Messenger.MainWindow"
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:local="clr-namespace:Messenger"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="New Message" Height="20" Width="80
" HorizontalAlignment="Left" Margin="35,46,0,0" VerticalAlignment="Top" Click="Button_Click" />
<TabControl HorizontalAlignment="Left" Height="254" Margin="34,44,0,0" VerticalAlignment="Top" Width="461" Background="{x:Null}" SelectedIndex="1">
<TabItem Header="Delivered" Visibility="Hidden" Width="80">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="Delivered" Visibility="Visible" Width="80">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="Sent" Width="80">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="Trash" Width="80">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
NewMessage:
public partial class NewMessage : Window
{
public NewMessage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
NewMessage XAML:
<Window x:Class="Messenger.NewMessage"
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:local="clr-namespace:Messenger"
mc:Ignorable="d"
Title="NewMessage" Height="351.047" Width="814.398">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="27*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="23*"/>
<RowDefinition Height="24*"/>
<RowDefinition Height="243*"/>
<RowDefinition Height="31*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="Komu" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
<Label Grid.Row="1" Content="Předmět" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
<RichTextBox HorizontalAlignment="Left" Height="243" Grid.Row="2" VerticalAlignment="Top" Width="806" Grid.ColumnSpan="2">
<FlowDocument>
<Paragraph>
<Run Text="Sem zadejte text zprávy"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button Grid.Row="3" Content="Odeslat" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Width="75" Margin="0,5,90,0"/>
<Button Grid.Row="3" Content="Storno" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Width="75" Margin="0,5,10,0" Click="Button_Click"/>
</Grid>
I would like to, when I minimize NewMessage window, this window minimize to the bottom of MainWindow "taskbar". If you know gmail, there is the same principle. Any idea?

C# mediaelement not working after setup on other computer

<Window x:Class="ErasmusWPF.Marta"
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:local="clr-namespace:ErasmusWPF"
mc:Ignorable="d"
Title="Marta" Height="768" Width="1366" ResizeMode="NoResize" WindowState="Maximized" AllowsTransparency="True" WindowStyle="None" Background="Transparent">
<Grid>
<Image Source="Images\grass.jpg" Stretch="UniformToFill" Grid.ColumnSpan="2"/>
<Image Source="Images\rail_luxury_shift-a.png" Height="833" VerticalAlignment="Bottom" OpacityMask="Black" Margin="-717,-38,-862,-27" Grid.ColumnSpan="2"/>
<Image Source="Images\Untitled.png" Margin="234,0,-369,-125" Width="1400" Height="767.8"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="636,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="566" LineHeight="18" TextAlignment="Center" FontSize="30"><Run Text="Klausīties Valka mūziku "/><LineBreak/><Run Text="(""/><Run Text="Dis"/><Run Text="t"/><Run Text="a"/><Run Text="nt Light")"/><LineBreak/><Run Text=" "/><InlineUIContainer/></TextBlock>
<Button x:Name="button5" Content="" Margin="636,27,624,682" Click="button_Click">
<Button.Background>
<ImageBrush ImageSource="Images/bcyEgxxzi.png"/>
</Button.Background>
</Button>
<Button x:Name="play" HorizontalAlignment="Left" Margin="484,322,0,0" VerticalAlignment="Top" Width="140" Height="84" Click="play_Click" >
<Button.Background>
<ImageBrush ImageSource="Images/playbutton.png"/>
</Button.Background>
<MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="84" VerticalAlignment="Top" Width="140" LoadedBehavior="Manual" UnloadedBehavior="Stop" Source="C:\Users\Leonards\Documents\Visual Studio 2015\Projects\ErasmusWPF\ErasmusWPF\ayylmao\crysis3.wmv" RenderTransformOrigin="0.5,0.5" Stretch="Fill" >
</MediaElement>
</Button>
<Button x:Name="play2" HorizontalAlignment="Left" Margin="669,322,0,0" VerticalAlignment="Top" Width="140" Height="84" Click="play2_Click" >
<Button.Background>
<ImageBrush ImageSource="Images/playbutton.png"/>
</Button.Background>
</Button>
<MediaElement x:Name="mediaElement2" HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="1368" LoadedBehavior="Manual" UnloadedBehavior="Stop" Source="C:\Users\Leonards\Documents\Visual Studio 2015\Projects\ErasmusWPF\ErasmusWPF\ayylmao\crysis3.wmv" Margin="0,0,-2,0" Stretch="Fill" />
</Grid>
</Window>
This is my xaml code
public Marta()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
System.Media.SoundPlayer player =
new System.Media.SoundPlayer();
player.SoundLocation = #"C:\Users\Leonards\Documents\visual studio 2015\Projects\ErasmusWPF\ErasmusWPF\Music\DistantLight.wav";
player.Load();
player.Play();
}
private async void play_Click(object sender, RoutedEventArgs e)
{
mediaElement.Play();
await Task.Delay (183000);
mediaElement.Source = null;
}
private async void play2_Click(object sender, RoutedEventArgs e)
{
mediaElement2.Play();
await Task.Delay(183000);
mediaElement2.Source = null;
}
}
The problem begins when i try to run application on any other computer. Mediaelements are programmed for my computers C directory. Whenever louched on any other pc, the content is lost and missing.

WPF page navigation in c#

I am trying to learn c# and WPF application. Here I am trying to redirect from one WPF page(MainWindow.xaml) to another(HandWash.xaml) on a button click event. But the following code is throwing NULLReferenceException.
This is the MainWindow.xaml file.
<Window x:Class="MyApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
d:DesignHeight="720" d:DesignWidth="1284"
Title="StartPage" WindowStartupLocation="CenterScreen" WindowStyle="None" WindowState="Maximized" Closed="Window_Closed">
<Window.Background>
<ImageBrush ImageSource="/Images/StartPage.png"></ImageBrush>
</Window.Background>
<Grid>
<Button Content="Hand Wash" Height="794" HorizontalAlignment="Left" Name="HandWash" VerticalAlignment="Top" Width="353" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="HandWash_Click"/>
<Button Content="Bathing" Height="794" HorizontalAlignment="Left" Margin="390,0,0,0" Name="Bathing" VerticalAlignment="Top" Width="301" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="Bathing_Click"/>
<Button Content="Nail-Clip" Height="794" HorizontalAlignment="Left" Margin="730,0,0,0" Name="NailClip" VerticalAlignment="Top" Width="295" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="NailClip_Click"/>
<Button Content="Teeth Brush" Height="794" HorizontalAlignment="Left" Margin="1067,0,0,0" Name="TeethBrush" VerticalAlignment="Top" Width="310" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="TeethBrush_Click"/>
</Grid>
</Window>
Background code for this:
private void TeethBrush_Click(object sender, RoutedEventArgs e)
{
try
{
TeethBrush teeth = new TeethBrush(myarg);
NavigationService navService = NavigationService.GetNavigationService(this);
navService.Navigate(teeth); // NULL REFERENCE EXCEPTION at this line
}
catch (NullReferenceException ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
This is the code for TeethBrush.xaml :
<Page x:Class="MyApplication.TeethBrush"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
d:DesignHeight="720" d:DesignWidth="1284"
Title="TeethBrush">
<Grid>
</Grid>
<Page.Background>
<ImageBrush ImageSource="C:\Users\Tonmoy\Documents\Visual Studio 2010\Projects\MyKinectApp\MyKinectApp\Images\StartPage.png"></ImageBrush>
</Page.Background>
</Page>
and the background code is:
public TeethBrush(Myargs arg)
{
InitializeComponent();
//Rest of the code
}
Please help....
You need to have a frame in main window where contents of Pages will be hosted.
If you add the following namespace to MainWindow:
xmlns:local="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
you can define a frame somewhere, e.g. in your grid:
<Grid>
<local:Frame x:Name="mainFrame">
</local:Frame>
....
Then you can navigate from your event handler like so:
TeethBrush teeth = new TeethBrush(myarg);
this.mainFrame.Navigate(teeth);

Image as pushpin on maps - Windows phone 8

im trying to add a image to a windows phone 8 map, to serve as a pushpin
i have the following code on my XAML
<phone:PhoneApplicationPage
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"
xmlns:maps="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
x:Class="Cartuxa.Contactos"
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">
<Grid.Background>
<ImageBrush ImageSource="/Assets/Images/bkContacts#2x.png"/>
</Grid.Background>
<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"/>
<Grid x:Name="ContentPanel" Grid.RowSpan="2" Margin="0,-6,0,6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="127*"/>
<ColumnDefinition Width="329*"/>
</Grid.ColumnDefinitions>
<Image HorizontalAlignment="Left" Margin="18.75,57.5,0,0" VerticalAlignment="Top" Source="/Assets/Images/logo#2x.png" Stretch="Fill" Grid.ColumnSpan="2" Width="221" Height="72"/>
<TextBlock HorizontalAlignment="Left" Margin="30,152,0,0" TextWrapping="Wrap" Text="Contactos" VerticalAlignment="Top" Foreground="#FFCD3333" FontFamily="Helvetica" FontSize="36" Grid.ColumnSpan="2" Width="174"/>
<Button HorizontalAlignment="Right" Margin="0,201,-13,0" VerticalAlignment="Top" Width="255" Background="White" Height="66" BorderThickness="0" Grid.Column="1" BorderBrush="#FFFF0909">
<Grid Grid.ColumnSpan="2">
<Image Name="POI_Left" Source="/Assets/Images/POI_Gray#2x.png" RenderTransformOrigin="0.4,0.274" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="-180,0,0,0" Stretch="None"/>
<TextBlock HorizontalAlignment="Left"
Margin="13,6,-22,0"
TextWrapping="Wrap"
Text="Paços dos Condes de Basto"
VerticalAlignment="Top"
FontFamily="Helvetica"
Foreground="Black"
FontSize="14" Width="182"/>
</Grid>
</Button>
<maps:Map Name="contactosMaps" HorizontalAlignment="Left" Margin="0,271,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.737,-0.175" Grid.ColumnSpan="2" Height="290" Width="480"/>
<Button Name="Contactos_Btn1"
Click="Contactos_Btn1_Click"
HorizontalAlignment="Right"
Margin="-17,201,242,0"
VerticalAlignment="Top"
Width="255"
Background="White"
Height="65"
BorderThickness="0"
Grid.ColumnSpan="2">
<Grid Name="pushRed" Grid.ColumnSpan="2">
<Image x:Name="POI_Right" Source="/Assets/Images/POI_Red#2x.png" RenderTransformOrigin="0.4,0.274" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="-190,0,0,0" Stretch="None"/>
<TextBlock HorizontalAlignment="Left" Margin="14,6,-14,0" TextWrapping="Wrap" Text="Fundação Eugénio De Almeida" VerticalAlignment="Top" FontFamily="Helvetica" Foreground="Black" FontSize="14"/>
</Grid>
</Button>
<TextBlock HorizontalAlignment="Left" Margin="26,606,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="406" Height="22">
<Run FontFamily="Helvetica" Text="Fundação Eugénio de Almeida"/>
<LineBreak/>
<Run/>
</TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="30,655,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="402" Height="85" FontFamily="Helvetica">
<Run Text="Páteo de São Miguel "/>
<LineBreak/>
<Run Text="Apartado 2001, 7001-901 Évora "/>
<LineBreak/>
<Run Text="Portugal"/>
<LineBreak/>
<Run/>
</TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="30,745,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="406" Height="22" Text="Tel. (+351) 266 748 300 "/>
<TextBlock HorizontalAlignment="Left" Margin="30,773,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="406" Height="22">
<Run FontFamily="Helvetica" Text="Fax. (+ 351) 266 705 149"/>
<Run FontFamily="Helvetica"/>
<Run/>
</TextBlock>
</Grid>
</Grid>
and my .cs code is the following
public partial class Contactos : PhoneApplicationPage
{
public Contactos()
{
InitializeComponent();
Loaded +=Contactos_Loaded;
}
private void Contactos_Loaded(object sender, RoutedEventArgs e)
{
UpdateMap();
}
private void UpdateMap()
{
MapOverlay pin = new MapOverlay();
pin.Content = pushRed;
pin.GeoCoordinate = new GeoCoordinate(38.57325D, -7.90546);
pin.PositionOrigin = new Point(0, 0.5);
MapLayer pinLayer = new MapLayer();
pinLayer.Add(pin);
contactosMaps.Layers.Add(pinLayer);
contactosMaps.SetView(new GeoCoordinate(38.57325D, -7.90546), 14D);
}
private void Contactos_Btn1_Click(object sender, RoutedEventArgs e)
{
}
}
when i try to run my project, it loads the map but a few moments later i get this exception
"Additional information: Value does not fall within the expected range."
when i do the "contactosMaps.Layers.Add(pinLayer);"
can't i have that Grid with the image as a pushpin?
Use the Phone.Controls.Toolkit as described here:-
http://wp.qmatteoq.com/maps-in-windows-phone-8-and-phone-toolkit-a-winning-team-part-1/
The toolkit can be found at either of
http://phone.codeplex.com/
or
https://www.nuget.org/packages/WPtoolkit
You can then either add the image directly in your XAML as follows:-
<maps:Map Loaded="MapControl_Loaded" ZoomLevel="18" Name="MapControl">
<toolkit:MapExtensions.Children>
<toolkit:Pushpin x:Name="MyPushpin">
<toolkit:Pushpin.Template>
<ControlTemplate TargetType="toolkit:Pushpin">
<StackPanel>
<Image Source="/Images/MapScreen/MapScreenCurrentLocationPin.png" Stretch="Uniform" Width="50" Height="50" HorizontalAlignment="Center"/>
</StackPanel>
</ControlTemplate>
</toolkit:Pushpin.Template>
</toolkit:Pushpin>
</toolkit:MapExtensions.Children>
</maps:Map>
or you can add it in C# as follows:-
MapOverlay overlay = new MapOverlay
{
GeoCoordinate = myMap.Center,
Content = new Ellipse
{
Fill = new SolidColorBrush(Colors.Red),
Width = 40,
Height = 40
}
};
MapLayer layer = new MapLayer();
layer.Add(overlay);
myMap.Layers.Add(layer);
You should be able to add a Grid with an image instead of an ellipse as shown above.
Let me know if this worked for you.

XamlParseException in windows Phone Application

I am trying to create my first Windows phone application but I keep getting this "XamlParseException" thrown at me.It looks like the error is coming from the InitializeComponent() method which is a shock because it was automatically created by VS.
Here is the Exception :
XamlParseException
Failed to assign to property 'System.Windows.FrameworkElement.Loaded'. [Line: 14 Position: 39]
Here is the code behind :
namespace WinHomeWork1
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, TextChangedEventArgs e)
{
myTextBox.Focus();
}
private void ClickMe_Click(object sender, RoutedEventArgs e)
{
if (myTextBox.Text.CompareTo("1 2 3 4") == 0)
{
myTextBlock.Text = "You have won!";
}
else
myTextBlock.Text = "You have Lost";
}
}
}
Here is the Xaml Code :
<phone:PhoneApplicationPage
x:Class="WinHomeWork1.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:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">
<!--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 x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="Brahman Initiative" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="108 mintes" 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">
<TextBox Height="72" HorizontalAlignment="Left" Margin="0,6,0,0" Name="myTextBox" Text="" VerticalAlignment="Top" Width="460" />
<Button Content="Go" Height="74" HorizontalAlignment="Left" Margin="234,88,0,0" Name="ClickMe" VerticalAlignment="Top" Width="222" Click="ClickMe_Click" />
<TextBlock Height="68" HorizontalAlignment="Left" Margin="12,88,0,0" Name="myTextBlock" Text="" VerticalAlignment="Top" Width="200" />
</Grid>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
The program has only 3 elements, 1) A TextBox 2) A TextBlock and a 3) Button. The user inputs data into the textBox then clicks a "Go" button, if the input is "1 2 3 4" the app will display "You have Won" in the textBlock, if any other input has been entered it will display " You have lost".Please Help, I do not know any Xaml so I can't play around with the Xaml code.
Change the parameter e in your PhoneApplicationPage_Loaded to an instance of the RoutedEventArgs class:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Focus();
}
Your error is due to PhoneApplicationPage_Loaded event handler method. It should be like this:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Focus();
}

Categories