Windows phone Text Boundaries - c#

I'm starting the development of a Windows Phone application and I can't find out how to get a rectangle selection on a textblock.
I'm looking for the exact same information as the getTextBounds(...) method gives in Android (getTextBounds)
Thanks for your help

Windows Phone 8 doesn't have any direct way to measure text. The closest you can get is to create the TextBlock and then check its ActualWidth.
TextBlock t = new TextBlock();
t.Text = "Lorum ipsum";
Debug.WriteLine("Text {0} {1},{2}", t.Text, t.ActualWidth, t.ActualHeight);
If you update to Windows Phone 8.1 then you can interop to DirectWrite to measure text, but DirectWrite is not available on Windows Phone 8.0. Win2D has measuring text on the backlog, but it's not there yet (and won't support WP8).

Did you by any chance, asking about focusing the Textbox:
//For your textbox
private void yourTextBox_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).Text = string.Empty;
}
Reference
Or else try using the Control.Focus.

Related

Windows Phone Universal App MediaCapture alternatives

I have a big problem with Universal App's MediaCapture. In this link I found that the problem might be the update installed on the phone which goes in conflict with this class.
I tryed some phones and all phones with Windows phone 8.1 Update crushes on MediaCapture initializing. No errors, just the phone quits the app.
In this article they said that it is due to a bug which closes the camera stopping the application.
Now, my problem is to find an alternative to MediaCapture, because half phones I need are with update 1 and half with update2 and I can't develop the app only for half customers.
Does any of you knows an alternative class?
PS: phones where app crushes have this update: 8.10.14219.341
Thanks all and sorry for my not perfect english.
Have you looked into using CameraCaptureTask instead? It can be as simple as this:
CameraCaptureTask cameraCaptureTask;
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
cameraCaptureTask.Show();
And then this is what you can do when the user is done capturing:
void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show(e.ChosenPhoto.Length.ToString());
//Code to display the photo on the page in an image control named myImage.
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp;
}
}

WinRT how to not have page go up when virtual key board appears Windows Phone 8.1

I have an AutoSuggestBox in a XAML Page for a Windows Phone 8.1 app and when i click on the AutoSuggestBox the whole page goes up. Does anyone know how to disable this feature?
Thanks
Edit:
Here is the code fix, thank you Peter Torr - MSFT.
Windows.UI.ViewManagement.InputPane.GetForCurrentView().Showing += (s, args) =>
{
args.EnsuredFocusedElementInView = true;
};
You need to handle the InputPane.Showing event, and set the EnsuredFocusedElementInView property to true. This will stop Windows from trying to make the item come into view.

Error when trying to set Grid.Background to accent colour

In my application the user can choose what colour they would like a particular feature of the app to be.
They can choose from a variety of colours which works fine, however when trying to set the Grid's background to the accent colours; when chosen, a NullReferenceException was unhandled error appears.
The code I am using is:
Color accentColour = (Color)Application.Current.Resources["PhoneAccentColor"];
gridColour.Background = new SolidColorBrush(accentColour);
Anyone know what I am doing wrong? (I've also tried using a Rectangle and it's .Fill property).
Thanks.
[SOLVED: Post solved in comments.]
You can try that code in OnNavigatedTo Event. Its Working.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Color accentColour = (Color)Application.Current.Resources["PhoneAccentColor"];
ContentPanel.Background = new SolidColorBrush(accentColour);
}
I guess you can easily do this by binding the PhoneAccent color within your XAML for your Grid.
These threads would be helpful:
Windows Phone 8 Change Accent and Theme Colour
windows phone 8 xaml set the color of a button on click
Hope it helps!

listbox.SetSelected(int, true) is not working in Windows Phone 7

In my Windows Phone 7 app, I have a listbox in the Xaml with some stuff in it.
Then in the C# code, I tried
listbox.SetSelected( anInteger, true);
Only it didn't work.
So am I doing something wrong? How do I select an item from the listbox with an integer in Windows Phone 7 with C#?
If you look at the ListBox Class Methods, you will notice that the SetSelected method is not supported for WP7.
Try using the SelectedIndex property instead:
private void btnSetSelected_Click(object sender, RoutedEventArgs e)
{
this.listBox.SelectedIndex = anInteger;
}
This article could be pretty useful: Working with WP7 ListBox SelectedItem

Set source and play a single mp3 file on each button on windows phone 7

i'm trying to make an application for windows phone 7.1 for schoool. I want to play a sound for each pressed button on my application (just like istantfun button for android) but i have some problem. If i declare on my xaml all the 120 Media Elements my applcation will play only 3/4 sound randomly. I want to make something like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
prova.Source = new Uri("/mp3/call.mp3", UriKind.Relative);
prova.Play();
}
where prova is a single MediaElement declared on the first page of xaml file.
How can i do? Thanks advice
first of all if you want play sound in multi-page app than media element is no good solution.
Besides of it's limitation (single sound at a time) and after considering amount of sound effects maybe you should try using XNA as described here: http://www.dotnetscraps.com/dotnetscraps/post/Play-multiple-sound-files-in-Silverlight-for-Windows-Phone-7.aspx
public void PlaySound(string soundFile)
{
using (var stream = TitleContainer.OpenStream(soundFile))
{
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
}
}
With this solution you don't need any XAML elements and thus you can play it app-wide.

Categories