*Xamarin Forms*项目中以*CS.cs*结尾的文件的目的是什么?

4

边缘只是一种视觉方式,用于告诉页面是通过C#代码创建而不是Xaml。 - SushiHangover
XAML对于UI设计师很友好,是代码生成器的输入。您通常无法看到它生成的代码,而且该代码不太美观。因此,作为替代方案,他们还提供了fooCS.cs文件,该文件执行与foo.xaml和foo.xaml.cs文件相同的操作。但是在名为fooCS的另一个类中。该类实际上未在项目中使用。除了演示用途外,它可能会激发程序员编写代码而不是xaml。 - Hans Passant
1个回答

3

从代码上看,LoginPageCS.cs 与 LoginPage.xaml 几乎完全相同 - 因此可以使用 C# 创建页面控件,而不是 XAML。有关差异的讨论,请参见 此问题

请注意相似之处

LoginPage.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" x:Class="LoginNavigation.LoginPage" Title="Login">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Sign Up" Clicked="OnSignUpButtonClicked" />
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
        <StackLayout VerticalOptions="StartAndExpand">
            <Label Text="Username" />
            <Entry x:Name="usernameEntry" Placeholder="username" />
            <Label Text="Password" />
            <Entry x:Name="passwordEntry" IsPassword="true" />
            <Button Text="Login" Clicked="OnLoginButtonClicked" />
            <Label x:Name="messageLabel" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

LoginPageCS.cs:

public LoginPageCS ()
{
    var toolbarItem = new ToolbarItem {
        Text = "Sign Up"
    };
    toolbarItem.Clicked += OnSignUpButtonClicked;
    ToolbarItems.Add (toolbarItem);

    messageLabel = new Label ();
    usernameEntry = new Entry {
        Placeholder = "username"    
    };
    passwordEntry = new Entry {
        IsPassword = true
    };
    var loginButton = new Button {
        Text = "Login"
    };
    loginButton.Clicked += OnLoginButtonClicked;

    Title = "Login";
    Content = new StackLayout { 
        VerticalOptions = LayoutOptions.StartAndExpand,
        Children = {
            new Label { Text = "Username" },
            usernameEntry,
            new Label { Text = "Password" },
            passwordEntry,
            loginButton,
            messageLabel
        }
    };
}

{btsdaf} - Rox
It would appear so - danielmcn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接