private async void InfoAreaPivot_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (InfoAreaPivot.SelectedItem != null)
            {
                if (DataContext is Sample sample)
                {
                    TrackingManager.TrackEvent("PropertyGrid", (InfoAreaPivot.SelectedItem as FrameworkElement)?.Name, sample.Name);
                }
            }

            if (CurrentSample == null)
            {
                return;
            }

            if (InfoAreaPivot.SelectedItem == PropertiesPivotItem)
            {
                // If we switch to the Properties Panel, we want to use a binded version of the Xaml Code.
                if (CurrentSample.HasXAMLCode)
                {
                    _lastRenderedProperties = true;

                    UpdateXamlRender(CurrentSample.BindedXamlCode);
                }

                return;
            }

            if (CurrentSample.HasXAMLCode && InfoAreaPivot.SelectedItem == XamlPivotItem && _lastRenderedProperties)
            {
                // Use this flag so we don't re-render the XAML tab if we're switching from tabs other than the properties one.
                _lastRenderedProperties = false;

                // If we switch to the Live Preview, then we want to use the Value based Text
                XamlCodeEditor.Text = CurrentSample.UpdatedXamlCode;

                UpdateXamlRender(CurrentSample.UpdatedXamlCode);
                await XamlCodeEditor.ResetPosition();

                XamlCodeEditor.Focus(FocusState.Programmatic);
                return;
            }

            if (CurrentSample.HasXAMLCode && InfoAreaPivot.SelectedItem == XamlReadOnlyPivotItem)
            {
                // Update Read-Only XAML tab on non-desktop devices to show changes to Properties
                XamlReadOnlyCodeRenderer.SetCode(CurrentSample.UpdatedXamlCode, "xaml");
            }

            if (CurrentSample.HasCSharpCode && InfoAreaPivot.SelectedItem == CSharpPivotItem)
            {
                var code = await CurrentSample.GetCSharpSourceAsync();

                CSharpCodeRenderer.SetCode(code, "c#");

                return;
            }
        }
Esempio n. 2
0
        private void UpdateXamlRender(string text)
        {
            if (XamlCodeEditor == null)
            {
                return;
            }

            // Hide any Previous Errors
            XamlCodeEditor.ClearErrors();

            // Try and Render Xaml to a UIElement
            UIElement element = null;

            try
            {
                element = _xamlRenderer.Render(text);
            }
            catch (Exception ex)
            {
                ShowExceptionNotification(ex);
            }

            if (element != null)
            {
                // Add element to main panel
                if (SamplePage == null)
                {
                    return;
                }

                var root = SamplePage.FindDescendantByName("XamlRoot");

                if (root is Panel)
                {
                    // If we've defined a 'XamlRoot' element to host us as a panel, use that.
                    (root as Panel).Children.Clear();
                    (root as Panel).Children.Add(element);
                }
                else
                {
                    // Otherwise, just replace the entire page's content
                    SamplePage.Content = element;
                }

                // Tell the page we've finished with an update to the XAML contents, after the control has rendered.
                if (element is FrameworkElement fe)
                {
                    fe.Loaded += XamlFrameworkElement_Loaded;
                }
            }
            else if (_xamlRenderer.Errors.Count > 0)
            {
                var error = _xamlRenderer.Errors.First();

                XamlCodeEditor.ReportError(error);
            }
        }
        private void ProcessSampleEditorTime()
        {
            if (CurrentSample != null &&
                CurrentSample.HasXAMLCode &&
                _xamlCodeRendererSupported)
            {
                if (XamlCodeEditor.TimeSampleEditedFirst != DateTime.MinValue &&
                    XamlCodeEditor.TimeSampleEditedLast != DateTime.MinValue)
                {
                    int secondsEditingSample = (int)Math.Floor((XamlCodeEditor.TimeSampleEditedLast - XamlCodeEditor.TimeSampleEditedFirst).TotalSeconds);
                    TrackingManager.TrackEvent("xamleditor", "edited", CurrentSample.Name, secondsEditingSample);
                }
                else
                {
                    TrackingManager.TrackEvent("xamleditor", "not_edited", CurrentSample.Name);
                }
            }

            XamlCodeEditor.ResetTimer();
        }
        private void UpdateXamlRender(string text)
        {
            if (XamlCodeEditor == null)
            {
                return;
            }

            // Hide any Previous Errors
            XamlCodeEditor.ClearErrors();

            // Try and Render Xaml to a UIElement
            UIElement element = null;

            try
            {
                element = _xamlRenderer.Render(text);
            }
            catch (Exception ex)
            {
                ShowExceptionNotification(ex);
            }

            if (element != null)
            {
                // Add element to main panel or sub-panel
                FrameworkElement root = null;

                if (CurrentSample.HasType)
                {
                    root = SamplePage?.FindDescendant("XamlRoot");

                    if (root is Panel)
                    {
                        // If we've defined a 'XamlRoot' element to host us as a panel, use that.
                        (root as Panel).Children.Clear();
                        (root as Panel).Children.Add(element);
                    }
                    else if (SamplePage != null) // UNO TODO
                    {
                        // if we didn't find a XamlRoot host, then we replace the entire content of
                        // the provided sample page with the XAML.
                        SamplePage.Content = element;
                    }
                }
                else
                {
                    // Otherwise, just replace our entire presenter's content
                    SampleContent.Content = element;
                }

                // Tell the page we've finished with an update to the XAML contents, after the control has rendered.
                if (element is FrameworkElement fe)
                {
                    fe.Loaded += XamlFrameworkElement_Loaded;

                    // UNO TODO
                    Console.WriteLine($"UpdateXamlRenderAsync attach loaded fe.IsLoaded:{fe.IsLoaded}");

                    if (fe.IsLoaded)
                    {
                        XamlFrameworkElement_Loaded(fe, new RoutedEventArgs());
                    }
                }
            }
            else if (_xamlRenderer.Errors.Count > 0)
            {
                var error = _xamlRenderer.Errors.First();

                XamlCodeEditor.ReportError(error);
            }
        }