本文整理汇总了C#中Windows.UI.Xaml.Controls.TextBox.Focus方法的典型用法代码示例。如果您正苦于以下问题:C# TextBox.Focus方法的具体用法?C# TextBox.Focus怎么用?C# TextBox.Focus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.UI.Xaml.Controls.TextBox
的用法示例。
在下文中一共展示了TextBox.Focus方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateNewToDoItem
private void CreateNewToDoItem(TextBox textBox)
{
var list = textBox.DataContext as ViewModels.TodoListViewModel;
list.AddCommand.Execute(textBox.Text);
textBox.Text = string.Empty;
textBox.Focus(Windows.UI.Xaml.FocusState.Programmatic);
if (AddNewItemConfirmButton != null)
AddNewItemConfirmButton.IsEnabled = false;
}
示例2: DialogTextBox
/// <summary>
/// Visualizza una finestra di dialogo con TextBox
/// </summary>
public static async Task<DialogTextBoxResult> DialogTextBox(string content, string title, string box="", string ok="ok", string cancel="annulla", string sub=null, string header=null, InputScopeNameValue scopename=InputScopeNameValue.Default)
{
DialogTextBoxResult risposta = new DialogTextBoxResult();
risposta.result=false;
risposta.output=null;
StackPanel stack = new StackPanel();
if(content != null)
{
TextBlock contenuto = new TextBlock();
contenuto.Text = content;
stack.Children.Add(contenuto);
}
InputScope scope = new InputScope();
scope.Names.Add(new InputScopeName(scopename));
TextBox tb = new TextBox();
tb.Text=box;
tb.InputScope = scope;
tb.Header = header;
tb.Margin = new Thickness(0,5,0,5);
stack.Children.Add(tb);
if(sub!=null)
{
TextBlock subba = new TextBlock();
subba.Text=sub;
subba.TextWrapping = TextWrapping.Wrap;
stack.Children.Add(subba);
}
ContentDialog cd = new ContentDialog();
cd.Title = title;
cd.Content = stack;
cd.PrimaryButtonText = ok;
cd.PrimaryButtonClick+= (s, ev) =>
{
risposta.result=true;
risposta.output=tb.Text;
};
cd.SecondaryButtonText=cancel;
cd.SecondaryButtonClick+= (s, ev) =>
{
risposta.result=false;
risposta.output=null;
};
cd.Opened+=(s, ev) =>
{
tb.Focus(FocusState.Keyboard);
tb.SelectAll();
};
await cd.ShowAsync();
return risposta;
}
示例3: BeginShowKeyboardInput_Win8
//.........这里部分代码省略.........
// Create a grid to add to the page. The grid will fill the entire page and has a
// semi-transparent black background to "mark" the graphics behind.
_gridBackground = new Grid();
_gridBackground.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
_gridBackground.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch;
_gridBackground.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
_gridBackground.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
_gridBackground.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
_gridBackground.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(100, 0, 0, 0));
// Create another grid to appear as a strip across the center of the screen, inside which
// the popup text, input box and buttons will be displayed
gridInputArea = new Grid();
gridInputArea.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 220, 220, 220));
gridInputArea.HorizontalAlignment = HorizontalAlignment.Stretch;
gridInputArea.VerticalAlignment = VerticalAlignment.Stretch;
gridInputArea.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
gridInputArea.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
gridInputArea.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
gridInputArea.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
gridInputArea.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20, GridUnitType.Star) });
gridInputArea.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(60, GridUnitType.Star) });
gridInputArea.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20, GridUnitType.Star) });
_gridBackground.Children.Add(gridInputArea);
Grid.SetRow(gridInputArea, 1);
// If we have some text, add it to the grid
if (!string.IsNullOrEmpty(title))
{
textTitle = new TextBlock();
textTitle.Text = title;
textTitle.TextWrapping = TextWrapping.Wrap;
textTitle.Foreground = new SolidColorBrush(Colors.Black);
textTitle.FontSize = 25;
textTitle.Margin = new Thickness(0, 20, 0, 0);
gridInputArea.Children.Add(textTitle);
Grid.SetRow(textTitle, 0);
Grid.SetColumn(textTitle, 1);
}
// Add the body text
textBody = new TextBlock();
textBody.Text = body;
textBody.TextWrapping = TextWrapping.Wrap;
textBody.Foreground = new SolidColorBrush(Colors.Black);
textBody.FontSize = 20;
textBody.Margin = new Thickness(0, 20, 0, 20);
gridInputArea.Children.Add(textBody);
Grid.SetRow(textBody, 1);
Grid.SetColumn(textBody, 1);
// Create a textbox for the user to enter text into
_textboxInput = new TextBox();
_textboxInput.HorizontalAlignment = HorizontalAlignment.Stretch;
_textboxInput.VerticalAlignment = VerticalAlignment.Stretch;
_textboxInput.Height = 20;
_textboxInput.Background = new SolidColorBrush(Colors.White);
_textboxInput.Text = initialValue;
_textboxInput.KeyDown += textboxInput_KeyDown;
gridInputArea.Children.Add(_textboxInput);
Grid.SetRow(_textboxInput, 2);
Grid.SetColumn(_textboxInput, 1);
// The final row of the popup is occupied by a StackPanel inside which the buttons are displayed
stackButtons = new StackPanel();
stackButtons.Orientation = Orientation.Horizontal;
stackButtons.HorizontalAlignment = HorizontalAlignment.Right;
stackButtons.Margin = new Thickness(0, 20, 0, 20);
gridInputArea.Children.Add(stackButtons);
Grid.SetRow(stackButtons, 3);
Grid.SetColumn(stackButtons, 1);
// Add the OK button...
_buttonOK = new Button();
_buttonOK.Content = "OK";
_buttonOK.Padding = new Thickness(0, 5, 0, 5);
_buttonOK.Width = 110;
_buttonOK.Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 30, 30, 30));
_buttonOK.Click += buttonOption_Click;
stackButtons.Children.Add(_buttonOK);
// ...and the Cancel button
_buttonCancel = new Button();
_buttonCancel.Content = "Cancel";
_buttonCancel.Padding = new Thickness(0, 5, 0, 5);
_buttonCancel.Width = 110;
_buttonCancel.Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 30, 30, 30));
_buttonCancel.Click += buttonOption_Click;
stackButtons.Children.Add(_buttonCancel);
// Add the whole grid to the game page
_parent.Children.Add(_gridBackground);
// Set focus into the input field
_textboxInput.Focus(FocusState.Programmatic);
_textboxInput.SelectionStart = _textboxInput.Text.Length;
// Indicate that the input panel is now visible
KeyboardInputIsVisible = true;
}
示例4: ShowKeyboardInput
public bool ShowKeyboardInput()
{
if (IsXAML)
{
// RunAsync all of the UI info.
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// Create a TextBox that will be added to our view but hidden
hiddenKeyInput = new Windows.UI.Xaml.Controls.TextBox();
hiddenKeyInput.Opacity = 0;
// We will only be generating one character at a time and does not
// matter about the font. All we need is the text.
hiddenKeyInput.Width = 1;
hiddenKeyInput.Height = 1;
hiddenKeyInput.MaxWidth = 1;
// Create an input scope for us. Probably not needed
var scope = new Windows.UI.Xaml.Input.InputScope();
var name = new Windows.UI.Xaml.Input.InputScopeName();
name.NameValue = Windows.UI.Xaml.Input.InputScopeNameValue.Default;
scope.Names.Add(name);
hiddenKeyInput.InputScope = scope;
// Set spell checking off.
hiddenKeyInput.IsSpellCheckEnabled = false;
// Add our hidden TextBox to our panel
content.Children.Add(hiddenKeyInput);
// InputPane does not work in this scenario
// We need to listen to GotFocus and LostFocus Events
hiddenKeyInput.GotFocus += OnGotFocus;
hiddenKeyInput.LostFocus += OnLostFocuc;
// Hook up our key delegates
hiddenKeyInput.KeyDown += OnKeyDown;
hiddenKeyInput.KeyUp += OnKeyUp;
// enable us and set focus
hiddenKeyInput.IsEnabled = true;
hiddenKeyInput.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
);
}
else
{
}
return true;
}
示例5: DoEdit
/// <summary>
/// Preforms the selected edit type on the text box given.
/// </summary>
/// <param name="textBox"></param>
/// <param name="editType"></param>
public static void DoEdit(TextBox textBox, VisualHelperTypes editType)
{
// First refocus the text box since the user clicked a button.
// We need to do this quickly so the virt keyboard doesn't move.
textBox.Focus(FocusState.Programmatic);
// Get some vars
int selStart = textBox.SelectionStart;
int selLength = textBox.SelectionLength;
string curText = textBox.Text;
string insertNewLine = null;
string insertAtEnd = null;
bool isLink = false;
bool hasExampleText = false;
// Depending on the type see what we can do.
switch (editType)
{
case HelperControls.VisualHelperTypes.Bold:
if (selLength != 0)
{
// Wrap the selected text
textBox.Text = curText.Insert(selStart + selLength, "**").Insert(selStart, "**");
}
else
{
// Or add to the end
insertAtEnd = $"**{c_exampleText}**";
hasExampleText = true;
}
break;
case HelperControls.VisualHelperTypes.Italic:
if (selLength != 0)
{
// Wrap the selected text
textBox.Text = curText.Insert(selStart + selLength, "*").Insert(selStart, "*");
}
else
{
// Or add to the end
insertAtEnd = $"*{c_exampleText}*";
hasExampleText = true;
}
break;
case HelperControls.VisualHelperTypes.Link:
if (selLength != 0)
{
// Wrap the selected text
textBox.Text = curText.Insert(selStart + selLength, $"]({c_exampleUrl})").Insert(selStart, "[");
}
else
{
// Or add to the end
insertAtEnd = $"[{c_exampleText}]({c_exampleUrl})";
}
isLink = true;
break;
case HelperControls.VisualHelperTypes.NewLine:
int injectPos = selStart;
// Inject the new line at the current pos
textBox.Text = curText.Insert(injectPos, " \r\n");
// Move the selection to the end of insert
textBox.SelectionStart = injectPos + 3;
break;
case HelperControls.VisualHelperTypes.Quote:
insertNewLine = "> ";
break;
case HelperControls.VisualHelperTypes.List:
insertNewLine = "* ";
break;
case HelperControls.VisualHelperTypes.NumberedList:
insertNewLine = "1. ";
break;
case HelperControls.VisualHelperTypes.Code:
insertNewLine = " ";
break;
}
// If the insert on new line is not null we need to find the last new line and
// insert the text.
if (insertNewLine != null)
{
// Search for a new line.
int searchStart = selStart == curText.Length ? selStart - 1 : selStart;
int indexLastNewLine = curText.LastIndexOf('\n', searchStart);
// Insert the text
textBox.Text = curText.Insert(indexLastNewLine + 1, insertNewLine);
// Move the cur to the end.
textBox.SelectionStart = indexLastNewLine + 1 + insertNewLine.Length;
}
// If this isn't null we have something to add at the end of the current text.
//.........这里部分代码省略.........
示例6: GenerateTaskPanel
//.........这里部分代码省略.........
Background = MainPage.MediumGrayBrush,
Margin = new Thickness(5, 36, 0, 7),
HorizontalAlignment = HorizontalAlignment.Left,
BorderThickness = new Thickness(0),
Padding = new Thickness(0)
};
registerItem(infoBorder);
// Edit button for the info box
Button edit = new Button
{
FontFamily = Icons.IconFont,
Content = Icons.Edit,
Width = 20,
Height = 20,
FontSize = 10,
Padding = new Thickness(0),
Background = MainPage.TransparentBrush,
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(parent.Width / 3 + 10, 36, 0, 7)
};
// Toggles edit mode for the info box
edit.Click += (sender, args) =>
{
if (!(bool) info.Tag)
{
info.Tag = true;
edit.Content = Icons.Save;
info.IsReadOnly = false;
info.Background = new SolidColorBrush(Colors.White);
info.IsHitTestVisible = true;
info.Focus(FocusState.Pointer);
info.SelectionStart = info.Text.Length;
info.SelectionLength = 0;
} else
{
info.Tag = false;
edit.Content = Icons.Edit;
info.IsReadOnly = true;
info.Background = MainPage.MediumGrayBrush;
info.IsHitTestVisible = false;
DataHandler.EditTask(task.Id, "Info", info.Text);
}
};
registerItem(edit);
#endregion
#region Circular Progress Bar
// Progress bar background (gray)
CircularProgressBar progressBack = new CircularProgressBar
{
Percentage = 100,
SegmentColor = MainPage.MediumGrayBrush,
Radius = 30,
StrokeThickness = 5,
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(0, 2.75, 2.75 + 30, 0)
};
registerItem(progressBack);
// Progress bar foreground (blue)
示例7: DoEdit
/// <summary>
/// Preforms the selected edit type on the text box given.
/// </summary>
/// <param name="textBox"></param>
/// <param name="editType"></param>
public static void DoEdit(TextBox textBox, VisualHelperTypes editType)
{
// First refocus the text box since the user clicked a button.
// We need to do this quickly so the virt keyboard doesn't move.
textBox.Focus(FocusState.Programmatic);
// Get some vars
int selStart = textBox.SelectionStart;
int selLength = textBox.SelectionLength;
int newLineSelOffset = 0;
string curText = textBox.Text;
string insertNewLine = null;
string insertAtEnd = null;
bool isLink = false;
bool hasExampleText = false;
// For some reason the SelectionStart count /r/n as 1 instead of two. So add one for each /r/n we find.
for(int count = 0; count < selStart + newLineSelOffset; count++)
{
if(curText[count] == '\r' && count + 1 < curText.Length && curText[count + 1] == '\n')
{
newLineSelOffset++;
}
}
// Depending on the type see what we can do.
switch (editType)
{
case HelperControls.VisualHelperTypes.Bold:
if (selLength != 0)
{
// Wrap the selected text
textBox.Text = curText.Insert(selStart + newLineSelOffset + selLength, "**").Insert(selStart + newLineSelOffset, "**");
}
else
{
// Or add to the end
insertAtEnd = $"**{c_exampleText}**";
hasExampleText = true;
}
break;
case HelperControls.VisualHelperTypes.Italic:
if (selLength != 0)
{
// Wrap the selected text
textBox.Text = curText.Insert(selStart + newLineSelOffset + selLength, "*").Insert(selStart + newLineSelOffset, "*");
}
else
{
// Or add to the end
insertAtEnd = $"*{c_exampleText}*";
hasExampleText = true;
}
break;
case HelperControls.VisualHelperTypes.Link:
if (selLength != 0)
{
// Wrap the selected text
textBox.Text = curText.Insert(selStart + newLineSelOffset + selLength, $"]({c_exampleUrl})").Insert(selStart + newLineSelOffset, "[");
}
else
{
// Or add to the end
insertAtEnd = $"[{c_exampleText}]({c_exampleUrl})";
}
isLink = true;
break;
case HelperControls.VisualHelperTypes.NewLine:
int injectPos = selStart + newLineSelOffset;
// Inject the new line at the current pos
textBox.Text = curText.Insert(injectPos, " \r\n");
// Move the selection to the end of insert
textBox.SelectionStart = injectPos + 3 - newLineSelOffset;
break;
case HelperControls.VisualHelperTypes.Quote:
insertNewLine = "> ";
break;
case HelperControls.VisualHelperTypes.List:
insertNewLine = "* ";
break;
case HelperControls.VisualHelperTypes.NumberedList:
insertNewLine = "1. ";
break;
case HelperControls.VisualHelperTypes.Code:
insertNewLine = " ";
break;
}
// If the insert on new line is not null we need to find the last new line and
// insert the text.
if (insertNewLine != null)
{
// Search for a new line.
int offsetSelStart = selStart + newLineSelOffset;
//.........这里部分代码省略.........
示例8: Show
/// <summary>
/// Shows the dialog. It takes <see cref="InputDialogViewModel"/> to populate the system dialog. It also sets the first command to be
/// the default and last one to be the cancel command.
/// </summary>
/// <param name="viewType"> Type of dialog control. </param>
public async void Show(Type viewType)
{
this.Initialize();
var dialog = new ContentDialog
{
Title = this.ViewModel.ActivationData.Title,
MaxWidth = Window.Current.CoreWindow.Bounds.Width
};
var panel = new StackPanel();
panel.Children.Add(new TextBlock
{
Text = this.ViewModel.ActivationData.Message,
TextWrapping = TextWrapping.Wrap,
Margin = new Thickness(0, 6, 0, 12)
});
var tb = new TextBox
{
Text = this.ViewModel.ActivationData.Text
};
panel.Children.Add(tb);
var primaryText = this.ViewModel.ActivationData.Commands.FirstOrDefault() ?? "Ok";
var secondaryText = this.ViewModel.ActivationData.Commands.Skip(1).FirstOrDefault() ?? string.Empty;
var returnValue = secondaryText;
tb.KeyUp += (sender, args) =>
{
if (args.Key == VirtualKey.Enter)
{
returnValue = primaryText;
dialog.Hide();
}
};
dialog.Content = panel;
dialog.PrimaryButtonText = primaryText;
dialog.SecondaryButtonText = secondaryText;
tb.SelectionStart = tb.Text.Length;
tb.Focus(FocusState.Programmatic);
try
{
this.dialogTask = dialog.ShowAsync();
var result = await this.dialogTask;
var resultValue = string.Empty;
this.dialogTask = null;
switch (result)
{
case ContentDialogResult.None:
resultValue = returnValue;
break;
case ContentDialogResult.Primary:
resultValue = primaryText;
break;
case ContentDialogResult.Secondary:
resultValue = secondaryText;
break;
}
await this.NavigationService.GoBackAsync(new InputDialogResult(resultValue, tb.Text));
}
catch (TaskCanceledException ex)
{
// Happens when you call nanavigationSerivce.GoBack(...) while the dialog is still open.
}
}