【问题标题】:How to get x:Class property of Page in Windows Phone 8.1?如何在 Windows Phone 8.1 中获取 Page 的 x:Class 属性?
【发布时间】:2015-02-28 01:43:51
【问题描述】:
<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 功能: 私人无效列表控制() { 帧 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();
    }

请帮助我。感谢您的帮助!

【问题讨论】:

  • 你能解释一下你的场景是什么吗?页面的x:Class 是用于实现页面本身的类。
  • 感谢您的建议!我已经修好了。
  • 试图获取 App6.MainPage 的代码在哪里?
  • 我添加了代码。请帮帮我。

标签: testing windows-phone-8.1 app.xaml


【解决方案1】:

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

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

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

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

if (page.GetType().FullName == "App6.MainPage")
{
  ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多