Get Geolocation Bing Map W10 Universal App When Right Clicked/Long Press - c#

I'm developing a universal map for windows 10 (VS 2015) and I have a map control. I want to capture a long press / right click event and determine the geolocation (lat/lng) where the user presses. I've tried capturing the right click event, but it never fires whether I use the mouse or a long press on the map:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Maps="using:Windows.UI.Xaml.Controls.Maps"
x:Class="App1.MainPage"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Maps:MapControl
x:Name="Map"
MapServiceToken="<MY_TOKEN>"
ZoomInteractionMode="GestureAndControl"
LandmarksVisible="True"
IsRightTapEnabled="true"
RightTapped="Map_RightTapped"
/>
</Grid>
</Page>
C#:
using System.Diagnostics;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Map_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
Debug.WriteLine("Here");
}
}
}
Evidently I'm missing something. Thanx,
Update: I have a work around of sorts. I can capture the data I need from a long press using the MapHolding event, but there does not seem to be support for a right click. The class documentation indicates there is a MapRightTapped event (https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.maps.mapcontrol.maprighttapped.aspx) but it isn't available either in in the XAML editor or in the .cs file using intellisense. Note that the event is MapHolding, not Holding (e.g., use myMap.MapHolding += EventHandlerMethod, not myMap.Holding += EventHandlerMethod).

I believe the right click event is not currently supported on the map however, this is in the next Windows update later this month.

Related

WPF C# user control with 2 or more buttons

Sorry if this has been asked before and I have spent about a week trying to find a similar question to point me in the right direction. I am teaching myself C# with WPF, XAML etc and am playing around with user controls. I made a simple app with a user control to load on top of other windows or user controls. The UC in question has two buttons and I need to get to the click events for each button in main window once the control is loaded. The main window has a button that loads the control.
Through some research I was able to find a solution from user SWilko (https://stackoverflow.com/a/28949666/10659981) but I can't figure it out for each button separately (click button a and show "clicked btn a", click button b and show "clicked button b"). I did try calling by sender using name and that will not work either. I feel like I am close with the help from the answer by SWilko but stuck.
Here is the code so far:
Basic main screen loading user control
<Window x:Class="UCBTN_TEST.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:UCBTN_TEST"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="435">
<Grid>
<Button Content="Load Button" HorizontalAlignment="Left" Margin="18,23,0,0" VerticalAlignment="Top" Width="74" Click="Button_Click"/>
<Grid x:Name="GridLoad" HorizontalAlignment="Left" Height="300" Margin="120,23,0,0" VerticalAlignment="Top" Width="300" Background="#FFF1CBCB"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UCBTN_TEST
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
GridLoad.Children.Clear();
GridLoad.Children.Add(new WindowControl());
}
}
}
The button user control
<UserControl x:Name="UCMain" x:Class="UCBTN_TEST.Controls.ButtonControl"
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:local="clr-namespace:UCBTN_TEST.Controls"
mc:Ignorable="d" d:DesignWidth="300" Height="40.333">
<Grid Background="#FFE7EEA7">
<Button x:Name="ButtonA" Content="Button A" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonA_Click" Background="Red"/>
<Button x:Name="ButtonB" Content="Button B" HorizontalAlignment="Left" Margin="215,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonA_Click" Background="Green"/>
</Grid>
</UserControl>
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UCBTN_TEST.Controls
{
/// <summary>
/// Interaction logic for ButtonControl.xaml
/// </summary>
public partial class ButtonControl : UserControl
{
public ButtonControl()
{
InitializeComponent();
}
private void ButtonA_Click(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(ClickEvent1, this));
}
public static readonly RoutedEvent ClickEvent1 = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ButtonControl));
public event RoutedEventHandler Click
{
add { AddHandler(ClickEvent1, value); }
remove { RemoveHandler(ClickEvent1, value); }
}
}
}
A second user control which would ultimately have some other controls once the buttons work correctly. But the button UC will load on top, simple button features related to WindowControl.
<UserControl
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:local="clr-namespace:UCBTN_TEST"
xmlns:Controls="clr-namespace:UCBTN_TEST.Controls" x:Class="UCBTN_TEST.WindowControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="#FFE7CFEE">
<Controls:ButtonControl HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="300" Click="Click1"/>
</Grid>
</UserControl>
I understand the behind code and why this is happening. My problem is that I need to have the buttons be unique in their events. I have tried calling by sender and name and that just kills the event all together.
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCBTN_TEST.Controls;
namespace UCBTN_TEST
{
/// <summary>
/// Interaction logic for WindowControl.xaml
/// </summary>
public partial class WindowControl : UserControl
{
public WindowControl()
{
InitializeComponent();
}
private void Click1(object sender, RoutedEventArgs e)
{
MessageBox.Show("This triggers both");
}
}
}
I was going to add a bunch of comments but really this is kind of answering the question and there's a lot to explain.
You should look into MVVM and mostly be thinking in terms of binding commands rather than which button was clicked. There are exceptions to this. For example, if you were building an on screen keyboard. The reason this is different because it's purpose can be encapsulated. The user presses a button which has "A" in it. Whatever textbox is focussed should be sent the character "A". They press a button showing "B" and similarly "B" should be sent. That functionality can be encapsulated in the control.
As it is, you have two buttons.
You put them in a usercontrol and encapsulate them.
By doing this you created a boundary.
This then creates a complication - which was clicked?
The usercontrol is also not particularly re-use friendly. If you add two then there are two buttonA and two button B. You could potentially improve that with a custom event args on your custom routed event and a dependency property on your usercontrol. Pass some usercontrol identifier along with which button was pressed.
This would be an unusual way to work though. I've rarely seen Custom routed events used in commercial apps.
All in all I would suggest the usercontrol mainly adds complexity.
Say you wanted to have 20 sets of 2 buttons.
Or 20 sets of 5 radiobuttons for a set of multiple choice questions.
The way to do that sort of thing is to use an itemscontrol and template out the multiple controls. One template giving 2 buttons ( or a textblock question and 5 radiobuttons for answers ) per row.
A click event is already a routed event and would bubble to the window. You may as well remove your custom routed event and the handler out the usercontrol... and the usercontrol. Just handle click in the window.
Code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var btn = e.OriginalSource as Button;
if(btn == null)
{
return;
}
MessageBox.Show($"Button clicked was {btn.Tag}");
}
Markup:
ButtonBase.Click="Button_Click"
Title="MainWindow"
>
<Grid>
<StackPanel>
<Button x:Name="ButtonA" Content="Button A" Tag="A" Background="Red"/>
<Button x:Name="ButtonB" Content="Button B" Tag="B" Background="Green"/>
</StackPanel>
</Grid>
</Window>

C# MVVM Prism questions

I try to understand C# prism framework, and make a little project. I meet some problem with misunderstanding Prism concept. I will explain what i want to do, what i try, my code:
I want to make a simple application like a test with 5 random question. You select difficult of test after you press start. On the same window your page is change with first question. Select right answer , next and you go on the second page with second question. From the second question you have 2 buttons(prev and next) , next next next, finish and you have a message like "you got x/5 points".
I have a similar aplication but the framework use is Caliburn.
First Application
I want to put all questions in xaml file now.
What i try:
I follow documentation, youtube tutorials for 2 days.
I create 2 folders: View and ViewModel. Create MainPage.xaml like a window in View and in ViewModel i create MainPageViewModel, in file MainPage.cs i create one instance like:
MainPageViewModel main = new MainPageViewModel();
After this i create a button i test the button and work.
Now I want to create a Page to connect with MainPageViewModel window. In the page i want to create another button and i want to check if is work. But this is the step how can't create.
After follow few tutorials, i can't make this. Not work to connect on the xaml file MainPageViewModel.
MainPage:
<Window x:Class="Aplicatie2._0.View.MainPage"
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:prism="http://prismlibrary.com/"
xmlns:local="clr-namespace:Aplicatie2._0.View"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="{Binding Title}" Height="450" Width="800">
<Window.DataContext>
</Window.DataContext>
<Grid>
</Grid>
</Window>
Error MC3063: Property 'DataContext' does not have a value. Line 14 Position 30.
MainPageViewModel:
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Commands;
namespace Aplicatie2._0.ViewModel
{
class MainPageViewModel : BindableBase
{
private string _Title = "Test";
public string Title
{
get
{
return _Title;
}
set
{
SetProperty(ref _Title, value);
}
}
}
}
App.xaml:
<prism:PrismApplication x:Class="Aplicatie2._0.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
xmlns:local="clr-namespace:Aplicatie2._0">
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
App.xaml.cs:
using Prism.Modularity;
using Prism.Ioc;
using System.Windows;
using System.ComponentModel;
using Prism.Unity;
using Aplicatie2._0.View;
namespace Aplicatie2._0
{
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainPage>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<MainPage>("MainPage");
}
}
}
1)How can i create multiple page and how can connect with main window?(If i call page1 i want to be clear, to create button and Radiobutton on the page)
2)How can i connect ViewModel file with View file on xaml, without create instance?
3)How can i create one question in xaml like:
Question: This is first question
[] Answer1
[] Answer2
[] Answer3
If you can to give me an example it's perfect, but make it simple.

Why isn't any sound playing when a button is clicked in my C#/XAML UWP application?

I am trying to use C# and XAML to create a simple app for the Universal Windows Platform (UWP). When I run it, my program is supposed to display a button that will play a sound using speech synthesis when clicked. The button shows up when I run the program, but does nothing instead of following the command.
Here is the code for "mainpage.xaml", the UI design code with some of the button's parameters:
<Page
x:Class="App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App"
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 Content="Hello world!" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0"/>
</Grid>
Here is the code for "MainPage.xaml.cs" that contains the event handler for the button:
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
//The event handler
private async void Button_Click(object sender, RoutedEventArgs e)
{
MediaElement mediaElement = new MediaElement();
var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello, World!");
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
}
}
}
I would like to know:
Am I missing anything in my program? If so, what?
Does the current playback device selected for my computer affect whether the sound plays?
Is the problem caused by the event handler in general or the command itself?
Information that might be useful:
There are no build errors that keep the program from running
My operating system is Windows 10, version 1803 (Build 17134.286)
The version of C# I am using is 7.3
I am using Visual Studio 2017
This is the tutorial I followed to create my app
You need to wire up the Click event and its handler. Either in XAML or in code behind.
In XAML,
<Button Click=“Button_Click” ...
In code behind (you need to give the button a name like btn)
btn.Click += Button_Click;

Initializing XAML user control

I am very new to C# and I'm trying to initialize a XAML user control window when I start my scripting program. It's just a basic window with textboxes, and comboboxes. The XAML code and C# code are listed below respectively. Since I have Express, I am unable to use the MVVM light toolkit. I am also using VS2010 because that is what the original code for this program was. The VMS.TPS.Common.Model.API and Types are dll's used for this particular program. Keep in mind that the C# code has to have this basic skeleton, otherwise it won't work. The 'public void Execute' portion is where I need to code.
<UserControl x:Class="WpfApplication1.UserControl1"
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="426" d:DesignWidth="736">
<Grid Margin="10" Width="702" HorizontalAlignment="Center">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
using System.Windows;
using System.Windows.Forms;
using WpfApplication1;
namespace VMS.TPS
{
public class Script
{
public Script()
{
}
public void Execute(ScriptContext context, System.Windows.Window window)
{
}
}
}
It should be very simple if i understood it right :
UserControl1 testUsrCtrl = new UserControl1();
And then set required properties.
Make sure you have all the references in place.

How to enable window chrome by using C# code and not XAML?

So I am using the WPFShell to apply chrome to a custom window. I have learned from this article that in order to use it, I have to reference the Microsoft.Windows.Shell library and use this XAML code:
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome
ResizeBorderThickness="6"
CaptionHeight="43"
CornerRadius="25,25,10,10"
GlassFrameThickness="0">
</shell:WindowChrome>
</shell:WindowChrome.WindowChrome>
My question is, how do I enable chrome by using C# code and not XAML? (i.e. How do I apply chrome in code-behind?)
Ah, stupid me. It was easy:
WindowChrome.SetWindowChrome(this, new WindowChrome());
I know that this is a older question, But I noticed that I couldn't get WindowChrome.GetWindowChrome() to work in .NET 4.5. I'm not sure if this has to do with System.Windows.Shell being included within the PresentationFramework assembly. But since it kept returning null there would be no way to update the chrome.
So my solution was to add a 'Name' to the WindowChrome which made it accessible in Code Behind.
XAML:
<Window x:Class="SomeProject.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"
mc:Ignorable="d"Title="Some Window" WindowStyle="None" ResizeMode="CanResize"
AllowsTransparency="True">
<WindowChrome.WindowChrome>
<WindowChrome x:Name="chrome" ResizeBorderThickness="6" CaptionHeight="0"
GlassFrameThickness="0" CornerRadius="0" UseAeroCaptionButtons="False"/>
</WindowChrome.WindowChrome>
</window>
Code Behind:
using System;
using System.Window;
namespace SomeProject
{
public partial class MainWindow: Window
{
public MainWindow()
{
//Get Existing 'WindowChrome' Properties.
var captionHeight = chrome.CaptionHeight;
//Set Existing 'WindowChrome' Properties.
chrome.GlassFrameThickness = new Thickness(2d);
//Assign a New 'WindowChrome'.
chrome = new System.Windows.Shell.WindowChrome();
}
}
}
I hope this helps someone who needs it.

Categories

Resources