AppShell中添加

this.Navigate("MauiViews.MauiDemos.Book._02.Xaml2009");

Xaml2009.xaml代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MauiViews.MauiDemos.Book._02"
             x:Class="MauiViews.MauiDemos.Book._02.Xaml2009"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             Title="Xaml2009"
             HeightRequest="300" WidthRequest="300">
    <StackLayout Margin="10">
        <Label Text="{Binding Path=Text, Source={x:Reference txtFirstName}, StringFormat='First Name: {0}'}"/>
        <Entry x:Name="txtFirstName"/>
        <Label Margin="0,10,0,0" Text="{Binding Path=Text, Source={x:Reference txtLastName}, StringFormat='Last Name: {0}'}"/>
        <Entry x:Name="txtLastName"/>
        <ListView Margin="0,25,0,0" ItemSelected="ListView_ItemSelected">
            <ListView.ItemsSource>
                <x:Array Type="{x:Type x:Object}">
                    <x:String>Item 1</x:String>
                    <x:String>Item 2</x:String>
                    <x:String>Item 3</x:String>
                    <local:Person FirstName="张" LastName="三"/>
                    <local:Person FirstName="李" LastName="四"/>
                    <sys:Guid x:FactoryMethod="NewGuid"></sys:Guid>
                </x:Array>
            </ListView.ItemsSource>
        </ListView>
    </StackLayout>
</ContentPage>

对应的cs文件代码

namespace MauiViews.MauiDemos.Book._02;
public class Person
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }

    public Person()
    {

    }
    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public override string ToString()
    {
        return FirstName + " " + LastName;
    }
}
public partial class Xaml2009 : ContentPage
{
	public Xaml2009()
	{
		//InitializeComponent();
        // 这里可以添加其他初始化代码
        LoadXamlContent("D://Codes//CSharp//Maui//MauiViews//MauiDemos//Book//02//Xaml2009.xaml");
    }
    private void LoadXamlContent(string xamlFile)
    {
        try
        {
            if (!File.Exists(xamlFile))
            {
                DisplayAlert("Error", "XAML file not found", "OK");
                return;
            }

            string xamlContent = File.ReadAllText(xamlFile);
            this.LoadFromXaml(xamlContent);

            //txtFirstName = NameScopeExtensions.FindByName<Entry>(this, "txtFirstName");
            //txtLastName = NameScopeExtensions.FindByName<Entry>(this, "txtLastName");
        }
        catch (Exception ex)
        {
            DisplayAlert("Error", $"Load failed: {ex.Message}", "OK");
        }
    }
    public void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        DisplayAlert(Title, e.SelectedItem?.ToString() ?? "No item selected", "OK");
    }
}

运行效果

 

posted on 2025-06-14 10:47  dalgleish  阅读(13)  评论(0)    收藏  举报