为什么不调用我的WPF页面控件的构造函数?

时间:2014-04-19 22:22:02

标签: winrt-xaml windows-8.1 win-universal-app

我正在尝试使用Visual Studio 2013 Express中的通用应用程序为桌面/平板电脑和手机创建Windows应用商店应用。我在理解WPF中发生的事情时遇到了一些困难,因为我之前的Windows 8开发经验一直是HTML / JS应用程序。

我要求VS创建一个新项目 - > Visual C# - > Store Apps-> Universal Apps-> Blank App。我打开MainPage.xaml.cs并在构造函数的第一行放置一个断点,恰好是this.InitializeComponent()。我点击F5,应用程序编译,我切换到熟悉的全屏现代应用程序视图,但我的断点都没有被击中。

我将一个TextBlock添加到MainPage.xaml只是为了那里有一些东西,但仍然没有命中断点。我错过了什么?下面是(某些)Visual Studio生成的代码。我可能遗漏了一些关于WPF应用程序如何工作和结构化的非常基本的东西,但我所有的google-fu已经化为乌有。

MainPage.xaml中:

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

    <Grid>

    </Grid>
</Page>

MainPage.xaml.cs中:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace SoloCoach
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }
    }
}

1 个答案:

答案 0 :(得分:1)

创建通用应用程序时,它会同时创建Windows 8.1和Windows Phone 8.1应用程序。您显示的代码是Phone版本的默认页面构造函数,因此您可能在错误的项目中,而不是您正在运行的Windows Modern App。