微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决即使Xamarin App编译没有错误,它也不会出现在模拟器中?

这是我的文件结构和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

解决方法

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

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

        //other things to do
    }
  • 请在下次上传之前清洗溶液(删除bin和obj文件夹)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。