Surface sdk 2.0 keyboard not showing in application - c#

I am trying to show the keyboard when I click a button, but it's not showing a keyboard at all.
The "TEST" gets printed but the keyboard isn't showing.
My code is :
private SurfaceTextBox mySurfaceTextBox = new SurfaceTextBox();
void showKeyBoard(object sender, RoutedEventArgs e)
{
//System.Windows.Input.Keyboard.Focus((IInputElement)getCanvasFromButton((SurfaceButton) sender));
System.Windows.Input.Keyboard.Focus((IInputElement)mySurfaceTextBox);
Console.Write("TEST");
SurfaceKeyboard.IsVisible = true;
SurfaceKeyboard.CenterX = (float)InteractiveSurface.PrimarySurfaceDevice.Bounds.Width - (SurfaceKeyboard.Width / 2);
SurfaceKeyboard.CenterY = (float)InteractiveSurface.PrimarySurfaceDevice.Bounds.Height - (SurfaceKeyboard.Height / 2);
SurfaceKeyboard.Layout = Microsoft.Surface.KeyboardLayout.Alphanumeric;
SurfaceKeyboard.Rotation = (float)(Math.PI / 2);
SurfaceKeyboard.ShowsFeedback = false;
}
Can someone help me please?

I don't know much about the surface framework; but usually you cannot force a keyboard to appear, the focused object needs to accept text as an input.
Because buttons generally don't accept text input, the keyboard's focus cannot be given to it, and thus
System.Windows.Input.Keyboard.Focus((IInputElement)sender);
will be ignored.
If the idea is to only make the keyboard appear, then an option is to add a SurfaceTextBox and to give focus to the textbox (this will inturn remove focus from the button)
XAML
Add this to your XAML file
<Canvas>
<s:SurfaceTextBox
Name="yourSurfaceTextBox"
Canvas.Top="200" Canvas.Left="200"
Width="100" Height="40" />
</Canvas>
Code File
void showKeyBoard(object sender, RoutedEventArgs e)
{
Console.Write("TEST");
System.Windows.Input.Keyboard.Focus((IInputElement)yourSurfaceTextBox);
// Rest of your code...
}
If the idea is to get navigation between buttons, you should consider using a SurfaceListBox since it accepts as a default behavior arrow navigation from the keyboard, then your code above should work.
Question in Comments
How I can test this on a non-surface device?
You can use a simulator which should be included in the 2.0 sdk
How I can change the cursor position in the SurfaceTextBox to the place it's touched?
I don't really understand what you mean by 'place it's touched', but you can change the cursor location in the textbox using the select method.
yourSurfaceTextBox.Select(position, 0);
To get the touch locations you can use
ReadOnlyTouchPointCollection touches = touchTarget.GetState();
Then you'll have to figure out where in relation to an object the touch was, but I this question is beyond the scope of the original question.
Have fun!

Related

UWP CustomRenderer for Checkbox: Pointer over Checkbox changes style?

I'm working with Xamarin.Forms and I made a CustomRenderer for Checkbox in UWP. When I set all the Checkboxes of my items in the ListView to true by clicking the button "Alle", the Checkboxes are displayed correctly with the check inside the box:
However, if I hover my mouse over the Checkboxes, they immediately change their appearence (the check disappears but it's still selected). In the following screenshot, I moved my cursor over the 3rd - 7th Checkboxes:
This is my overridden OnElementChanged method in the CustomRenderer:
protected override void OnElementChanged(ElementChangedEventArgs<EvaCheckbox> e)
{
base.OnElementChanged(e);
var model = e.NewElement;
if (model == null)
{
return;
}
nativeCheckbox = new CheckBox();
CheckboxPropertyChanged(model, null);
model.PropertyChanged += OnElementPropertyChanged;
nativeCheckbox.Checked += (object sender, Windows.UI.Xaml.RoutedEventArgs eargs) =>
{
model.IsChecked = (bool)nativeCheckbox.IsChecked;
};
nativeCheckbox.Unchecked += (object sender, Windows.UI.Xaml.RoutedEventArgs eargs) =>
{
model.IsChecked = (bool)nativeCheckbox.IsChecked;
};
SetNativeControl(nativeCheckbox);
}
I tried to override the PointerEntered event of nativeCheckbox. It works, for example if I set the model.IsChecked to true on this event, it will be set to true:
nativeCheckbox.PointerEntered += (s, args) =>
{
model.IsChecked = true;
};
But I don't know how to (if even at this place) prevent the checkbox from changing it's appearance when moving the cursor above the Checkbox. Just leaving the triggered event with empty code like this won't change anything about the described behaviour:
nativeCheckbox.PointerEntered += (s, args) => { };
How can I prevent the Checkbox from changing it's appearance when I move my cursor over it?
Update:
I've created a sample project for this issue. You can find the repository here: https://github.com/Zure1/CustomCheckbox
It has the exact same described behavior. In the following screenshot I pressed the button "All" on the bottom of the screen and then the checkboxes look like correct with a check inside of them:
After moving the mouse cursor over the bottom 3 checkboxes, their change their appearance:
Information: I'm debugging on my desktop (Windows 10). I don't know if this issue exists on WinPhone. Just in case you're wondering why my checkboxes are red: My system color in Windows is red.
This is a tricky one as I have been struggling with this issue for a while, I'll try my best to answer this.
TL;DR: It's caused by ViewCell.
The issue comes down to Xamarin Forms ListView and ViewCell.
I haven't been able to track down the cause yet for many months and the way I get around this issue is by refreshing the ListView every time a change happens forcing a redraw of the entire ListView which can really impact performance.
My educated guess on what the cause could be is the rendering code for the ViewCell is missing something.
As for your particular issue, I have created a CheckBoxCell which you can use to display a list of checkboxes with a title. I forked your project and made the changes.
This will display something similar to what you are trying to achieve and doesn't have rendering issues so will be a good starting point. You are able to customize this to display images and the like but you'll have to do that in the platform-specific layout code.
Please note that I have only created the code for UWP and that should be enough to get you going for the other platforms.
I hope this helps somewhat.

Any Characters - Masked Text Box [duplicate]

This question already has answers here:
Masked TextBox Input Align Left
(2 answers)
Closed 9 years ago.
I'm writing an app in C# with Visual Studio 2012, and I'm needing to format some input text using MaskedTextBox. The user will type in a folder path to the text box, but since the folder path is relative to another path, I need it to start with ".\", but I do not care how long the path is.
Right now, I have the mask set for the box to \.\\CCCCCCCCCCCCCCCCCC. This works fine except for the fact that when the user clicks into the box, it places the cursor where they click instead of the beginning of the box.
Is there a way to set the mask to still put in the ".\" but not to set any limit on the characters that come after it?
Or is there a way I'm overlooking?
EDIT: More info
So I've tried a couple recommended things, but they don't seem to work. The answer linked here doesn't work well. While I can set it to go to that selection point when I click on the box, it will go there every time you need to click on the box. So you can't select the whole box or edit part of what you typed, which is even worse for usability.
I also tried the method suggested by Adelmo. I made an even handler like so:
public Form1()
{
InitializeComponent();
refreshList();
this.textBoxPrintFolder.GotFocus += new EventHandler(textBoxPrintFolder_GotFocus);
}
private void textBoxPrintFolder_GotFocus(object sender, EventArgs e)
{
this.textBoxPrintFolder.Select(2, 0);
}
This works when tabbing to the box, but apparently clicking on the box doesn't go into the GotFocus event.
I've also tried using the MouseEnter event. While it does work, it takes a few seconds before it will move. Not ideal.
Any help would be greatly appreciated.
Maybe using onFocus event:
You can control cursor position (and selection) by TextBox.SelectionStart and TextBox.SelectionLength properties.
Example if you want move cursor to before 3th character set SelectionStart = 2 and SelectionLength = 0.
http://social.msdn.microsoft.com/Forums/en-US/04362a62-8cbf-4d86-a1bc-2aba8e4978ca/cursor-position-in-textbox
Hope it help you

How I will get dropping media on mediaelement in wpf?

I am working with mediaelement in wpf, but the problem is I can't drop media on medaiaelement.
can anyone tell me the solution.the following code is .cs file code. I set allow drop property= true
private void mediaElement1_Drop(object sender, System.Windows.DragEventArgs e)
{
String[] FileName = (String[])e.Data.GetData(System.Windows.Forms.DataFormats.FileDrop, true);
if (FileName.Length > 0)
{
String VideoPath = FileName[0].ToString();
mediaElement1.Source = new Uri(VideoPath);
mediaElement1.Play();
}
e.Handled = true;
}
Tried it myself. Actually it will work after you play something there.
Here's the point: assume we have a Grid:
<Grid AllowDrop="True"></Grid>
It won't allow drop.
Now the following
<Grid Background="Transparent" AllowDrop="True"></Grid>
Will allow drop.
The first Grid doesn't have background at all, so actually there's no way to drop anything on it - there's no grid. And in second case there is grid's background even though we can't see it.
The same thing applies to MediaElement. Unfortunately it doesn't have any Background or Content property, so it won't allow drop until you start playing something there.
Solution is to handle drop on MediaElement parent container.
By the way, don't forget to set LoadedBehavior="Manual" for MediaElement so that it will play dropped file.
EDIT.
Here is explanation why MediaElement doesn't allow drop till any content was loaded in it.
Every WPF component is in fact composed of some other basic elements: Borders, Grids, ContentPresenters etc. So something inside the MediaElement handles drop. I cannot tell you what element it is because MediaElement's Template is not accessible. But it really doesn't matter what exactly is the element that handles drag and drop there. What does matter is that there's is nothing material in MediaElement's area until you load content on it - just like in case of my example with Grid at the beginning of this post. I mean that when you move mouse cursor over it's area there is nothing between cursor and MediaElement's container. Try to handle MouseDown event: result will be the same - it won't fire until you load any video. Why? Because there is nothing to raise event. Nothing cannot raise anything.
As I mentioned before there is great difference between Background="{x:Null}" and Background="Transparent": in first case there's no background brush, no background, but in second case there is one. Feel the difference.

Kinect Custom Cursor

I need to make a cursor in Kinect, but I don't know where to start. I need to make it using WPF and C#. Cursor should be in shape of hand and when I hover over element the "loading" circle should appear, and when it "loads" it should fire click event on hovered element.
I'm sure that your're all familiar with this.
It would be of great help if someone could write me some directions oh how to accomplish this.
Here is a code snippet that might help you:
using Microsoft.Research.Kinect.Nui;
Runtime nui = Runtime.Kinects[0];
nui.Initialize(RuntimeOptions.UseSkeletalTracking);
nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);
void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
SkeletonFrame sf = e.SkeletonFrame;
SkeletonData d = (from s in sf.Skeletons
where s.TrackingState == SkeletonTrackingState.Tracked
select s).FirstOrDefault();
if (d != null)
{
SetHandPosition(imageCursor, d.Joints[JointID.HandLeft]);
}
}
void SetHandPosition(FrameworkElement e, Joint joint)
{
Joint scaledJoint = Coding4Fun.Kinect.Wpf.SkeletalExtensions.ScaleTo(joint, 600, 400, 0.75f, 0.75f);
Canvas.SetLeft(e, scaledJoint.Position.X);
Canvas.SetTop(e, scaledJoint.Position.Y);
}
If you want your cursor to be different hovering on an element then just go to the elements properties and set a cursor for that element. In Visual Studio you can choose a cursor in the elements properties.
To make a click on a hover over an element you have to implement the MouseEnter event
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
....
}
Here are some infos about it:
button1.PerformClick() in wpf
Here is a nice solution using the official SDK, but even if you're not, it can be very (!) helpful (it was for me):
You should take a look at the (free) code available here Beginning Kinect Programming with the Microsoft Kinect SDK sample code you click on "Source Code/Downloads" and what's is going to interest you in the sample is the Chapter 6 (name of the folder).
Basically they're using a static class KinectInput that allow to raise new event like KinectCursorEnterEvent, there is a cursor manager KinectCursorManager which does almost everything get the hand position/update the cursor ... and They use an adorner to put the cursor, with the FrameworkElement you want as a cursor.
They implement the HoverButton you're talking about. It fires the click event after a timer elapsed (timer launched when the KinectCursorEnterEvent occured). The solution they propose is elegant, and allow an easy implementation of nice controls.
You can easily modify the code to handle the two cursor (that's the value-added of the Kinect, isn't it?)
having an Enumeration CursorSide:
public enum CursorSide
{
Left,
Right
}
and modifyong only the KinectCursorManager having a Dictionary of capacity 2, and the enumeration being the Key, and having a pointer on the elemtn under the cursor for each hand:
private Dictionary<CursorSide, CursorAdorner> _cursorAdorner;
private UIElement _lastElementOverRightHand;
private UIElement _lastElementOverLeftHand;
But before you have to remove the part of the code that does the selection of the primaryHand (basically the hand closest to the Kinect).
I hope this can help somebody :-]
The book is very interesting, you can buy it for a few bucks.

c# sending control shortcuts

I am making a program in c sharp/xaml. I have a save button, and figured the easiest way to make it effective was when pressed if I could send the "control s" signal. What is the command (and any includes VS wouldn't add as standard) to do so?
A completely different question to cut down on thread count, how would I make a textblock (or textbox if easier) automatically newline when you reach the end rather than continuing to send text offscreen.
There are better patterns that I would suggest looking into before coupling your UI to keypresses (such as Commands) but if you really want to do this, you can use the SendKeys class from windows forms. This will allow you to send key presses to the application as if the user pressed those keys.
As for the second question, if you're using WPF just create a textbox element and set AcceptsReturn="True" and TextWrapping="Wrap". Here's an example:
<TextBox
Name="tbMultiLine"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"
>
This TextBox will allow the user to enter multiple lines of text. When the RETURN key is pressed,
or when typed text reaches the edge of the text box, a new line is automatically inserted.
</TextBox>
You can use the SaveFileDialog() .
private void button1_Click(object sender, EventArgs e)
{
// When user clicks button, show the dialog.
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
// Get file name.
string name = saveFileDialog1.FileName;
// Write to the file name selected.
// ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test");
}
If you're using XAML I'll assume you're using WPF. So you should be able to bind the Command property of the Button to ApplicationCommands.Save.

Categories