/// <summary> /// Sets the content of the test app to a simple <see cref="FrameworkElement"/> to load into the visual tree. /// Waits for that element to be loaded and rendered before returning. /// </summary> /// <param name="content">Content to set in test app.</param> /// <returns>When UI is loaded.</returns> protected Task SetTestContentAsync(FrameworkElement content) { return(App.DispatcherQueue.EnqueueAsync(() => { var taskCompletionSource = new TaskCompletionSource <bool>(); async void Callback(object sender, RoutedEventArgs args) { content.Loaded -= Callback; // Wait for first Render pass await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); taskCompletionSource.SetResult(true); } // Going to wait for our original content to unload content.Loaded += Callback; // Trigger that now try { App.ContentRoot = content; } catch (Exception e) { taskCompletionSource.SetException(e); } return taskCompletionSource.Task; })); }
private void GuardAgainstPlaceholderTextLayoutIssue() { // If the *PlaceholderText is visible* on the last AutoSuggestBox, it can incorrectly layout itself // when the *ASB has focus*. We think this is an optimization in the platform, but haven't been able to // isolate a straight-reproduction of this issue outside of this control (though we have eliminated // most Toolkit influences like ASB/TextBox Style, the InterspersedObservableCollection, etc...). // The only Toolkit component involved here should be WrapPanel (which is a straight-forward Panel). // We also know the ASB itself is adjusting it's size correctly, it's the inner component. // // To combat this issue: // We toggle the visibility of the Placeholder ContentControl in order to force it's layout to update properly var placeholder = ContainerFromItem(_lastTextEdit)?.FindDescendant("PlaceholderTextContentPresenter"); if (placeholder?.Visibility == Visibility.Visible) { placeholder.Visibility = Visibility.Collapsed; // After we ensure we've hid the control, make it visible again (this is imperceptible to the user). _ = CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { placeholder.Visibility = Visibility.Visible; }); } }
public async Task Test_ConstrainedBox_Combined_All() { await App.DispatcherQueue.EnqueueAsync(async() => { // We turn LayoutRounding off as we're doing between pixel calculation here to test. var treeRoot = XamlReader.Load(@"<Page xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""> <Grid x:Name=""ParentGrid"" Width=""100"" Height=""100""> <controls:ConstrainedBox x:Name=""ConstrainedBox"" ScaleX=""0.9"" ScaleY=""0.9"" MultipleX=""32"" MultipleY=""32"" AspectRatio=""3:1"" UseLayoutRounding=""False"" HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch""> <Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/> </controls:ConstrainedBox> </Grid> </Page>") as FrameworkElement; Assert.IsNotNull(treeRoot, "Could not load XAML tree."); // Initialize Visual Tree await SetTestContentAsync(treeRoot); var grid = treeRoot.FindChild("ParentGrid") as Grid; var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox; Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree."); // Force Layout calculations panel.UpdateLayout(); var child = panel.Content as Border; Assert.IsNotNull(child, "Could not find inner Border"); // Check Size Assert.AreEqual(64, child.ActualWidth, 0.01, "Actual width does not meet expected value of 64"); Assert.AreEqual(21.333, child.ActualHeight, 0.01, "Actual height does not meet expected value of 21.33"); // Check inner Positioning, we do this from the Grid as the ConstainedBox also modifies its own size // and is hugging the child. var position = grid.CoordinatesTo(child); Assert.AreEqual(18, position.X, 0.01, "X position does not meet expected value of 18"); Assert.AreEqual(39.333, position.Y, 0.01, "Y position does not meet expected value of 39.33"); // Update Aspect Ratio and Re-check panel.AspectRatio = new AspectRatio(1, 3); // Wait to ensure we've redone layout await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { // Check Size Assert.AreEqual(21.333, child.ActualWidth, 0.01, "Actual width does not meet expected value of 21.33"); Assert.AreEqual(64, child.ActualHeight, 0.01, "Actual height does not meet expected value of 64"); // Check inner Positioning, we do this from the Grid as the ConstainedBox also modifies its own size // and is hugging the child. position = grid.CoordinatesTo(child); Assert.AreEqual(39.333, position.X, 0.01, "X position does not meet expected value of 39.33"); Assert.AreEqual(18, position.Y, 0.01, "Y position does not meet expected value of 18"); }); }); }