Windows 模拟器没有';t显示新元素,只需;欢迎来到Xamarin.Forms“;

Windows 模拟器没有';t显示新元素,只需;欢迎来到Xamarin.Forms“;,windows,xamarin,visual-studio-2019,Windows,Xamarin,Visual Studio 2019,我正要进入Xamarin创建一个Windows Mobile应用程序。我正在使用Visual Studio 2019并创建了一个空的移动应用程序。起初,我使用Youtube教程创建了一个简单的计算器()。在设计器中,所有内容都按我的要求显示。当我尝试启动模拟器时,它不会显示我创建的元素。它仍然显示标准文本“欢迎使用Xamarin.Forms!” 在MainPage.xaml中,我基本上只是设计了一些基本元素作为标题和文本框: <forms:WindowsPage x:Class="Tasc

我正要进入Xamarin创建一个Windows Mobile应用程序。我正在使用Visual Studio 2019并创建了一个空的移动应用程序。起初,我使用Youtube教程创建了一个简单的计算器()。在设计器中,所有内容都按我的要求显示。当我尝试启动模拟器时,它不会显示我创建的元素。它仍然显示标准文本“欢迎使用Xamarin.Forms!”

在MainPage.xaml中,我基本上只是设计了一些基本元素作为标题和文本框:

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

<RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock x:Name="titleTextblock"  FontSize="38" RelativePanel.AlignHorizontalCenterWithPanel="True" Margin="0,12">Taschenrechner</TextBlock>

<RelativePanel RelativePanel.AlignHorizontalCenterWithPanel="True" RelativePanel.Below="titleTextblock">
    <TextBlock x:Name="firstNumberTextblock" Margin="0,6,12,0">Zahl 1:</TextBlock>
    <TextBox x:Name="firstNumberTextbox" RelativePanel.RightOf="firstNumberTextblock" Width="250" Margin="0,0,0,8"></TextBox>

    <TextBlock x:Name="secondNumberTextblock" Margin="0,6,12,0" RelativePanel.Below="firstNumberTextbox">Zahl 2:</TextBlock>
    <TextBox x:Name="secondNumberTextbox" RelativePanel.AlignLeftWith="firstNumberTextbox" Width="250" RelativePanel.Below="firstNumberTextbox"></TextBox>

    <Button x:Name="Addition" RelativePanel.RightOf="firstNumberTextbox" Margin="12,0,0,0" Width="30">+</Button>
    <Button x:Name="Subtraktion" RelativePanel.RightOf="Addition" Margin="12,0,0,0" Width="30">-</Button>
    <Button x:Name="Multiplikation" RelativePanel.Below="Addition" RelativePanel.RightOf="secondNumberTextbox" Margin="12,6,0,0" Width="30">*</Button>
    <Button x:Name="Division" RelativePanel.Below="Addition" RelativePanel.RightOf="Multiplikation" Margin="12,6,0,0" Width="30">*</Button>

    <TextBlock x:Name="Ergebnis" RelativePanel.Below="secondNumberTextbox" RelativePanel.AlignLeftWith="secondNumberTextbox" Margin="52,12,0,0" FontSize="24">Ergebnis:</TextBlock>
    <TextBlock x:Name="Wert" RelativePanel.RightOf="Ergebnis" RelativePanel.Below="secondNumberTextbox" Margin="12,12,0,0" FontSize="24">0</TextBlock>

</RelativePanel>

塔什恩雷奇纳
扎尔1:
扎尔2号:
+
-
*
*
Ergebnis:
0

到目前为止,我没有在MainPage.xaml.cs中更改任何内容

在App.xaml.cs之后:

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.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;

namespace Taschenrechner.UWP
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Application
{
    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="e">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {


        Frame rootFrame = Window.Current.Content as Frame;

        // 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;

            Xamarin.Forms.Forms.Init(e);

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

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

        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(MainPage), e.Arguments);
        }
        // Ensure the current window is active
        Window.Current.Activate();
    }

    /// <summary>
    /// Invoked when Navigation to a certain page fails
    /// </summary>
    /// <param name="sender">The Frame which failed navigation</param>
    /// <param name="e">Details about the navigation failure</param>
    void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
    {
        throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
    }

    /// <summary>
    /// Invoked when application execution is being suspended.  Application state is saved
    /// without knowing whether the application will be terminated or resumed with the contents
    /// of memory still intact.
    /// </summary>
    /// <param name="sender">The source of the suspend request.</param>
    /// <param name="e">Details about the suspend request.</param>
    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        //TODO: Save application state and stop any background activity
        deferral.Complete();
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows.ApplicationModel;
使用Windows.ApplicationModel.Activation;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
名称空间Taschenrechner.UWP
{
/// 
///提供特定于应用程序的行为以补充默认应用程序类。
/// 
密封部分类应用程序:应用程序
{
/// 
///初始化singleton应用程序对象。这是编写代码的第一行
///已执行,因此是main()或WinMain()的逻辑等价物。
/// 
公共应用程序()
{
this.InitializeComponent();
这个.Suspending+=OnSuspending;
}
/// 
///当最终用户正常启动应用程序时调用。其他入口点
///将在启动应用程序以打开特定文件时使用。
/// 
///有关启动请求和过程的详细信息。
仅启动受保护的覆盖无效(启动ActivatedEventArgs e)
{
Frame rootFrame=Window.Current.Content作为Frame;
//当窗口已经有内容时,不要重复应用程序初始化,
//只需确保窗口处于活动状态
if(rootFrame==null)
{
//创建一个框架作为导航上下文并导航到第一页
rootFrame=新框架();
rootFrame.NavigationFailed+=OnNavigationFailed;
Xamarin.Forms.Forms.Init(e);
如果(例如,PreviousExecutionState==ApplicationExecutionState.Terminated)
{
//TODO:从先前挂起的应用程序加载状态
}
//将框架放置在当前窗口中
Window.Current.Content=rootFrame;
}
if(rootFrame.Content==null)
{
//导航堆栈未还原时,请导航到第一页,
//通过将所需信息作为导航传递来配置新页面
//参数
导航(typeof(MainPage),例如参数);
}
//确保当前窗口处于活动状态
Window.Current.Activate();
}
/// 
///当导航到某个页面失败时调用
/// 
///导航失败的帧
///有关导航失败的详细信息
void OnNavigationFailed(对象发送方,NavigationFailedEventArgs e)
{
抛出新异常(“加载页面失败”+e.SourcePageType.FullName);
}
/// 
///在挂起应用程序执行时调用。应用程序状态已保存
///不知道应用程序是否会随内容一起终止或恢复
///记忆仍然完好无损。
/// 
///挂起请求的源。
///有关暂停请求的详细信息。
暂停时的私有void(对象发送方,SuspendingEventArgs e)
{
var deleral=e.SuspendingOperation.getdeleral();
//TODO:保存应用程序状态并停止任何后台活动
延迟。完成();
}
}
}
链接到项目:


我没有收到任何错误消息或警告。你能帮我修改一下什么,让模拟器可以运行吗?

从共享示例项目中,我得到了原因您创建的项目是一个表单应用程序。

每次,应用程序都在Froms中启动到此主页.xaml,而不是在通用Windows中启动

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Taschenrechner.MainPage">

    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
    </StackLayout>

</ContentPage>

如果您想进入Universal Windows MainPage.xaml,则需要使用表单应用程序

但是如果只想创建一个UWP应用程序,在创建新项目时,您应该选择以下选项:

并且项目的结构应该遵循,然后在主页中添加Xaml代码。Xaml可以是您想要的


您好,您能显示
MainPage.xaml
的总代码吗?我会检查的。是的,当然-谢谢!您好,谢谢您的更新,不过您在回复中发布了它。从您的代码来看,没有问题。它在我的项目中正确显示。即使在预览视图中也可以看到结果。你可以显示这样的截图。对不起,我不知道。我编辑了原始帖子并上传了要求的截图。预览也可以正常工作,没有任何问题。只是在模拟器中没有显示新版本。好吧,这种现象很奇怪。我还需要检查另一个文件
App.xaml.cs
。这可以知道何时启动应用程序,哪个页面作为要启动的根页面。