InputFocusController.TrySetFocus Method

Definition

Attempts to set focus to the ContentIsland associated with the InputFocusController.

C#
public bool TrySetFocus();

Returns

Boolean

True, if focus was set successfully; otherwise, false.

Examples

The following example shows how to indicate focus is on a TextBox within a ContentIsland based on pointer input.

C#
void OnClick(PointerPoint clickLocation) 
{
    if (IsWithinBoundsOfTextBox(clickLocation))
    {
        InputFocusController focusController = InputFocusController.GetForIsland(myIsland);

        if (!focusController.HasFocus())
        {
            bool nowHasFocus = focusController.TrySetFocus();

            // Change styling of text box based on whether the Island received focus
            if (nowHasFocus)
            {
                DrawTextBoxBorder();
            }
            else
            {
                RemoveTextBoxBorder();
            }
        }
    }
}

Remarks

Due to other message processing requirements, focus might move from the ContentIsland by the time this request is processed.

A user might also move focus before this request is processed.

Processing this request can raise GotFocus and LostFocus events in quick succession.

Applies to

Продукт Версії
Windows App SDK 1.4, 1.5, 1.6