【问题标题】:UWP App does not create UI elements after deployUWP App 部署后不创建 UI 元素
【发布时间】:2019-01-22 10:38:42
【问题描述】:

我正在开发的 UWP 应用有问题。基本上这个应用已经完成了,我想把它部署到微软商店之外。

应用程序在 Visual Studio 2017 中运行时运行良好,但是当我创建应用程序包并安装我的应用程序时,它只是打开没有任何 ui 组件的空白页面。应用程序不会抛出任何错误,它不会冻结。我可以调整空白窗口的大小、最小化、最大化和关闭。一切正常,只是没有按钮。

我有什么尝试:

  1. 我已将我的页面更改为不同的页面,以检查此错误是针对一页还是在整个应用程序中发生。无论哪个页面是主页面,它始终是空的。
  2. 我创建了新的空白页面并添加了简单的 TextBlock 并将其设置为我的应用程序页面,但它仍然在没有此文本块的情况下打开。
  3. 我通过添加这行将改变应用背景颜色的代码来尝试运行哪部分代码。

    (Application.Current.Resources["AppColorBackground"] as SolidColorBrush).Color = Colors.Crimson;

应用程序构造函数 App() 运行并更改背景颜色。

页面构造函数 MyPage() 运行并更改背景颜色。

页面方法 OnNavigatedTo() 运行并更改背景颜色。

  1. 我认为 InitializateComponent() 方法不会运行并且不会创建 ui 组件。在 initializeComponent() 方法之后,我添加了以下代码行

MyTextBox.Text = "块中的新文本";

查看 MyTextBox 是否存在。应用仍然不抛出错误,根本没有任何反应。

  1. 问朋友有没有类似的问题。
  2. 问谷歌
  3. 现在我问你stackoverflow社区你遇到过类似的问题吗?

知道我尝试在 Visual Studio 中启动新的 uwp 项目并逐步重写我的应用程序并检查哪行代码会导致问题。

一些附加信息。

我使用 Visual Studio 社区 2017 (ver.15.8.5)

目标 Windows 版本 1803 build 17134

最小版本创建者更新 build 15063

我使用的 Nuget 包:SignalR 客户端、Newtonsoft Json、UWP 的 Telerik UI

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.ViewManagement;
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;
using NaviParkManager.Controller;
using NaviParkManager.Model;
using NaviParkManager.Pages;

namespace NaviParkManager
{
    sealed partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;

            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            ApplicationView.PreferredLaunchViewSize = new Size(960, 540);
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
            localSettings.Values["launchedWithPrefSize"] = true;
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto;
        }

        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;

            // TitleBar config
            CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
            var titleBar = ApplicationView.GetForCurrentView().TitleBar;
            titleBar.ButtonBackgroundColor = Colors.Transparent;
            titleBar.ButtonForegroundColor = ((SolidColorBrush)Application.Current.Resources["AppColorText"]).Color;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (e.PrelaunchActivated == false)
            {
                if (rootFrame.Content == null)
                {
                    // When the navigation stack isn't restored navigate to the first page,
                    // configuring the new page by passing required information as a navigation
                    // parameter
                    rootFrame.Navigate(typeof(TestPage), e.Arguments);
                    //rootFrame.Navigate(typeof(UserLogInPage), e.Arguments);
                }
                // Ensure the current window is active
                Window.Current.Activate();
            }
        }

        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }


    }
}

【问题讨论】:

  • 您肯定需要向我们提供一些源代码,这样我们只能猜测发生了什么。另外,尝试在 Visual Studio 中以发布模式运行应用程序,会发生同样的情况吗?
  • 源代码有 567 行代码,我不希望任何人会经历所有这些。但是我可以发布代码片段,但是哪个? Visual Studio 中的调试模式和释放模式在两台不同的计算机上工作正常,没有错误,没有崩溃。但是在安装应用程序后,两台计算机上也会出现问题。
  • 我认为最关键的部分将是App.xaml.cs 文件和要显示的第一页(MainPage 或类似)
  • App.xaml.cs 主要是自动生成的代码。我尝试将我的页面交换到其他页面,但对问题没有影响。
  • 你能在 GitHub 上创建一个显示这种行为的最小 repo 吗?我会在我这边试试。

标签: c# uwp


【解决方案1】:

我找到了问题的原因。 app.xmal 中的以下代码行

<Style TargetType="Grid">
    <Setter Property="Background" Value="{ThemeResource AppColorBackground}"/>
</Style>

它在渲染 UI 时引起了奇怪的问题。部署后整个网格被背景颜色覆盖。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 2020-06-25
    • 2023-03-31
    • 2012-01-26
    • 2017-01-24
    • 2020-12-17
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多