简体   繁体   English

Android 模拟器的 Xamarin 按钮单击事件失败

[英]Xamarin Button Click event failure with Android Emulator

I'm having trouble making Button clicks work using Xamarin Forms for Android.我在使用 Xamarin Forms for Android 使按钮点击工作时遇到问题。 My code is just the blank template with a Button and a Click handler added.我的代码只是添加了一个 Button 和一个 Click 处理程序的空白模板。 I'm using Visual Studio 2019 Enterprise.我正在使用 Visual Studio 2019 企业版。 The iOS and UWP projects are present, but disabled. iOS 和 UWP 项目存在,但已禁用。

I've updated all NuGet packages and Android SDKs and Tools to the latest (Xamarin.Forms v4.4.0.991477).我已将所有 NuGet 包以及 Android SDK 和工具更新到最新版本 (Xamarin.Forms v4.4.0.991477)。 I've tried Android 9.0 API 28 and 6.0 API 23.我试过 Android 9.0 API 28 和 6.0 API 23。

A breakpoint set in OnButtonClicked is never hit. OnButtonClicked设置的断点永远不会被命中。 MainPage itself seems to load fine; MainPage本身似乎加载良好; no obvious errors or layout problems.没有明显的错误或布局问题。 No Exceptions are thrown.不会抛出异常。

I tried downloading a sample project with similar code and running it unmodified -- same problem.我尝试下载一个具有类似代码的示例项目并未经修改地运行它——同样的问题。

It's acting as though the mouse click / tap on the emulator is being ignored.就好像模拟器上的鼠标单击/点击被忽略了一样。

<?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"
             Title="Home"
             x:Class="Sched.MainPage">
    <ContentPage.Content>
        <StackLayout>
            <Button Text="Button" Clicked="OnButtonClicked" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
using System;
using System.ComponentModel;
using Xamarin.Forms;

namespace Sched
{
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        async void OnButtonClicked(object sender, EventArgs e)
        {
            var taskPage = new TaskPage();
            await this.Navigation.PushAsync(taskPage);
        }
    }
}
<?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"
             Title="Task"
             x:Class="Sched.TaskPage">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Problem solved.问题解决了。

The code is fine.代码没问题。 The issue was that my primary monitor is 3840 x 2160 px and is normally configured for 150% scaling, using Windows Display Settings.问题是我的主显示器是 3840 x 2160 像素,通常使用 Windows 显示设置配置为 150% 缩放。 If I set it to 100% scaling, the problem goes away.如果我将其设置为 100% 缩放,问题就会消失。 Equivalently, if I drag the Android emulator window to my secondary monitor, which has 100% scaling, that also works.等效地,如果我将 Android 模拟器窗口拖到我的辅助显示器上,它具有 100% 的缩放比例,这也有效。

Another side-effect of 150% scaling is that it causes the emulator to render as a large solid black box, with the Android screen in the upper-left. 150% 缩放的另一个副作用是它会导致模拟器呈现为一个大的纯黑框,Android 屏幕位于左上角。 With 100% scaling, the black box goes away, leaving only the Android screen. 100% 缩放后,黑框消失,只留下 Android 屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM