Xamarin App 没有出现在模拟器中,即使它编译没有错误?

Posted

技术标签:

【中文标题】Xamarin App 没有出现在模拟器中,即使它编译没有错误?【英文标题】:Xamarin App not showing up in Emulator, even though it compiles without errors? 【发布时间】:2020-11-03 18:47:17 【问题描述】:

这是我的文件结构和 MainPage.Xaml 文件的图片 MainPage.xaml这是该文件中的代码

<?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:local="clr-namespace:AppXam"
             x:Class="AppXam.MainPage">
    <ContentPage.BindingContext>
        <local:MainPage/>
    </ContentPage.BindingContext>
    <Grid>
        <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height=".5*"/>
        <RowDefinition Height="2*"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    
    <Image Source="https://s2.dmcdn.net/v/AmZGT1VXLk5NDRThE/x1080" BackgroundColor="red"
           Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2"/>
    
    <Editor Grid.Column ="0" Grid.ColumnSpan="2" Grid.Row="1" 
            Placeholder="Enter Note Here" Text="Binding TheNote" />
    
    <Button Grid.Row="2" Grid.Column="0" Text="Save" Command="Binding SaveCommand"   />
    
    <Button Grid.Row="2" Grid.Column="1" Text="Erase"    Command="Binding EaseCommand"/>
    
    <CollectionView ItemsSource="Binding AllNotes" Grid.Row="3" Grid.ColumnSpan="2">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Frame>
                        <Label Grid.Row="3" Grid.Column="0" Text="Binding ." FontSize="Large" /> 
                    </Frame>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
    
</Grid>

这是 MainPage.XAML.cs 的图片,它是包含 MVVM 代码的命名空间。 MainPage.xaml.cs

代码如下:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace AppXam

    public partial class MainPage : INotifyPropertyChanged
    
        public event PropertyChangedEventHandler PropertyChanged;
        public MainPage()
        
            EraseCommand = new Command(() =>
            
                TheNote = string.Empty;
            );
            
           SaveCommand = new Command(() =>
           
               AllNotes.Add(TheNote);

               TheNote = string.Empty;
           );
        
        public ObservableCollection<string> AllNotes get; set;
        
        
        
        string theNote;
        public string TheNote
         
             get => theNote;
             set
             
                 theNote = value;
                 var args = new  PropertyChangedEventArgs(nameof(TheNote));
                 PropertyChanged?.Invoke(this,args);
             
         

        public Command SaveCommand  get; 
        public Command EraseCommand  get; 
    

屏幕截图包含屏幕上的输出,这是一个空白页。项目目录可以在这里找到

https://drive.google.com/drive/folders/1sp4qh3Wzse1KVZN1IKdteaGcgncCG4p4?usp=sharing

【问题讨论】:

请不要将代码发布为图像How to Askminimal reproducible example 部署时日志显示什么? @AshLove 我试试 Shaw 的回复,它有效。关于Xamarin.Forms数据绑定mvvm有一个doc,可以看一下:Data Bindings to MVVM 【参考方案1】:

删除 XAML 中的绑定并使用它:

    public MainPage()
    
        InitializeComponent();  //1. no init, no page
        BindingContext = this;  //2. must bind after init

        //other things to do
    
请在下次上传之前清理解决方案(删除 bin 和 obj 文件夹)。

【讨论】:

以上是关于Xamarin App 没有出现在模拟器中,即使它编译没有错误?的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin 不会部署到 Android 模拟器没有错误

Xamarin 项目在部署到 App Store 时未应用一些更改

“你希望应用程序.ios.app接受传入的网络连接”每次都弹出diaplay

Xamarin Forms Android 应用程序在使用 VS Android 模拟器运行调试时崩溃

Xamarin - 如何查看 iOS 模拟器应用程序日志

标签未出现在 xamarin 应用 android 设备上