-2
<Page
x:Class="App6.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App6"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

我有一个应用程序。我想得到一个页面的 x:class 来区分页面。如何获取 App6.MainPage ?

我正在尝试这个我有 ListControl 功能: private void ListControl() { Frame currentFrame = null; 尝试 { currentFrame = Window.Current.Content as Frame;

        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
        }
        waitingControls.Add(currentFrame);
        int countChild = 1;

        while (waitingControls.Count > 0)
        {
            DependencyObject currentObject = waitingControls[0];
            waitingControls.RemoveAt(0);
            openedControls.Add(currentObject);

            countChild = VisualTreeHelper.GetChildrenCount(currentObject);
            for (int i = 0; i < countChild; i++)
                waitingControls.Add(VisualTreeHelper.GetChild(currentObject, i));
        }
    }

和 ClearListControl 函数

 private void ClearListControl()
    {
        openedControls.Clear();
        waitingControls.Clear();
    }

. 最后,我得到属性页面:

private async void GetPropertiesPage()
    {
         ListControl();

         for (int i = 0; i < openedControls.Count; i++)
         {
             if (openedControls[i] is Page)
             {
                 string propertiesPage = "";
                 Page page = openedControls[i] as Page;

                 //if (page.Name.Equals(""))
                 //{


                     var temp = page.TransformToVisual(Window.Current.Content as Frame);
                     Point screenCoords = temp.TransformPoint(new Point(0, 0));

                     propertiesPage += ",X," + Math.Round(screenCoords.X, 0) + Environment.NewLine + ",Y," + Math.Round(screenCoords.Y, 0) + Environment.NewLine;
                     propertiesPage += ",Content," + page.Content + Environment.NewLine + ",Name," + page.Name + Environment.NewLine;
                     propertiesPage += ",AllowDrop," + page.AllowDrop.ToString() + Environment.NewLine;
                     StackPanel panel = (StackPanel)page.Content;

                     if (propertiesPage.Length > 0)
                     {
                         Task.Run(async () =>
                         {
                             SendData(propertiesPage);
                         }).Wait();
                     }

                     for (int j = 0; j < openedControls.Count; j++)
                     {
                         if (openedControls[j] is Grid)
                         {
                             Grid gr = (Grid)openedControls[j];
                             gr.Children.Remove(ca);
                             ca = new Canvas();
                             line = new Rectangle()
                             {
                                 StrokeThickness = 2,
                                 Stroke = new SolidColorBrush(Colors.Red),
                                 Height = page.ActualHeight,
                                 Width = page.ActualWidth,
                             };
                             page.UpdateLayout();
                             ca.Children.Add(line);
                             gr.Children.Add(ca);
                             Canvas.SetTop(line, screenCoords.Y - 25);
                             Canvas.SetLeft(line, screenCoords.X);
                             await Task.Delay(2000);
                             gr.Children.Remove(ca);
                             break;

                         }
                     }

                 //}
             }
         }

         ClearListControl();
    }

请帮我。感谢您的帮助!

4

1 回答 1

0

你有两个选择。您可以检查类型或类型名称。

使用is运算符检查类型可能会更好,因为您将在编译时检查它是否是有效类型:

if (page is App6.MainPage)
{
  ...
}

如果由于某种原因这不起作用,您可以使用 typename 代替:

if (page.GetType().FullName == "App6.MainPage")
{
  ...
}
于 2015-03-04T05:23:43.223 回答