Xamarin.Forms:C#代码与XAML代码(Xamarin.Forms: C# code vs XAML code)

编程入门 行业动态 更新时间:2024-10-22 02:37:48
Xamarin.Forms:C#代码与XAML代码(Xamarin.Forms: C# code vs XAML code)

美好的一天。 我正在创建一个简单的Xamarin.Forms(便携式)程序。 我只是在想,如果我能以某种方式“合并”或者可能将我的XAML文件中的Label调用到我的XAML.cs. 可能吗? 因为每次我在XAML.cs中编写代码时,我在XAML文件中的现有代码似乎都没有出现。

例如,我在我的SalesPage.xaml.cs中有这样的代码

public partial class SalesPage : ContentPage { Label resultsLabel; SearchBar searchBar; public SalesPage() { resultsLabel = new Label { Text = "Result will appear here.", VerticalOptions = LayoutOptions.FillAndExpand, FontSize = 25 }; searchBar = new SearchBar { Placeholder = "Enter search term", SearchCommand = new Command(() => { resultsLabel.Text = "Result: " + searchBar.Text + " is what you asked for."; }) }; Content = new StackLayout { VerticalOptions = LayoutOptions.Start, Children = { new Label { HorizontalTextAlignment = TextAlignment.Center, Text = "SearchBar", FontSize = 50, TextColor = Color.Purple }, searchBar, resultsLabel, new Label { HorizontalTextAlignment = TextAlignment.Center, Text = "SearchBar", FontSize = 50, TextColor = Color.Purple }, new ScrollView { Content = resultsLabel, VerticalOptions = LayoutOptions.FillAndExpand } }, Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5) }; }

唯一会出现在屏幕上的是我在Xaml.cs上编码的那个。

看看这个。

我在我的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="XamarinFormsDemo.Views.SalesPage" BackgroundImage="bg3.jpg" Title="Sales Page"> <Label x:Name="SalesLabel" VerticalOptions="Center" HorizontalOptions="Center" /> </ContentPage>

如何在屏幕上显示我在XAML.CS中编写的代码以及在XAML中编写的代码? 我怎样才能一起展示他们?

非常感谢。 非常感谢您的帮助。

Good Day. I'm creating a simple Xamarin.Forms (Portable) program. I'm just thinking if I could somehow "MERGE" or maybe call my Label in my XAML file to my XAML.cs. Is it possible? Because everytime I write my code in XAML.cs, the existing code I have in XAML file doesn't seem to appear.

For example, I have this code in my SalesPage.xaml.cs

public partial class SalesPage : ContentPage { Label resultsLabel; SearchBar searchBar; public SalesPage() { resultsLabel = new Label { Text = "Result will appear here.", VerticalOptions = LayoutOptions.FillAndExpand, FontSize = 25 }; searchBar = new SearchBar { Placeholder = "Enter search term", SearchCommand = new Command(() => { resultsLabel.Text = "Result: " + searchBar.Text + " is what you asked for."; }) }; Content = new StackLayout { VerticalOptions = LayoutOptions.Start, Children = { new Label { HorizontalTextAlignment = TextAlignment.Center, Text = "SearchBar", FontSize = 50, TextColor = Color.Purple }, searchBar, resultsLabel, new Label { HorizontalTextAlignment = TextAlignment.Center, Text = "SearchBar", FontSize = 50, TextColor = Color.Purple }, new ScrollView { Content = resultsLabel, VerticalOptions = LayoutOptions.FillAndExpand } }, Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5) }; }

The only thing that will appear on the screen is the one I coded on Xaml.cs.

Take a look at this.

I have this code in my XAML file :

<?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="XamarinFormsDemo.Views.SalesPage" BackgroundImage="bg3.jpg" Title="Sales Page"> <Label x:Name="SalesLabel" VerticalOptions="Center" HorizontalOptions="Center" /> </ContentPage>

How can I display to the screen the codes I write in XAML.CS and the one I code in XAML? How can I display them together?

Thanks a lot. Really appreciate your help.

最满意答案

首先让我描述一下XAML + Code-Behind概念是如何工作的,然后再回到你的问题。 首先读取并分析XAML文件,并创建UI。 然后执行后面的代码,您可以在其中对UI进行一些调整。 这意味着您可以通过其名称属性访问XAML的所有控件。 在上面的示例中,您可以将其添加到后面的代码中:

SalesLabel = "My new Label";

它会覆盖您在XAML中定义的标签。 如果您需要动态确定标签,这可能变得方便。

如果您想动态地将内容添加到XAML中,则必须访问现有控件并将内容添加到该控件。

您定义的XAML文件具有隐式“Content”属性。 我将把它添加到你的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="XamarinFormsDemo.Views.SalesPage" BackgroundImage="bg3.jpg" Title="Sales Page"> <Content> <Label x:Name="SalesLabel" VerticalOptions="Center" HorizontalOptions="Center" /> </Content> </ContentPage>

Content属性不是“聚合”意味着您只能对内容进行一次控制而不是控件列表。 要显示多个控件,需要一个可以保存聚合的控件,如StackLayout

但是,由于您没有在XAML文件中指定此类控件,而是直接覆盖代码中的Content属性,因此即使在您在屏幕上看到它之前,它也会在运行时被替换。

Content = new StackLayout { <your other code> }

如果您在后面的代码中删除此代码,您将在运行时看到XAML内容。

现在要解决您的问题,请在XAML中创建视图的所有静态部分。 当您对结果满意时,请转到后面的代码,选择需要增强或动态调整的控件,然后对这些控件进行调整。

这是非常基本的方法。 对于更复杂的用户界面或更复杂的解决方案,我至少要命名带动态用户界面数据绑定的MVVM方法。 Xamarin.Forms中的MVVM

First let me describe how the XAML + Code-Behind concept works and then go back to your issue. First the XAML file is read and parsed and it creates the UI. Then the code behind is executed where you can do some adjustments to the UI. That means you can access all controls of the XAML by its Name property. In your example above, you could add this to the code behind:

SalesLabel = "My new Label";

and it would overwrite the label you defined in XAML. This can become handy if you need to dynamically determine the label.

If you want to dynamically add content additionally to the XAML, you have to access an existing control and add the content to that control.

The XAML file you defined has an implicit "Content" property. I will add this to your XAML code (even though this would most probably return a compiler error) to illustrate this:

<?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="XamarinFormsDemo.Views.SalesPage" BackgroundImage="bg3.jpg" Title="Sales Page"> <Content> <Label x:Name="SalesLabel" VerticalOptions="Center" HorizontalOptions="Center" /> </Content> </ContentPage>

The Content property is no "aggregation" means you can only have ONE control for the content and not a list of controls. To display more than one control, one requires a control that can hold aggregations like a StackLayout.

But as you did not specify such a control in your XAML file but directly overwrite the Content property in your code behind, it gets replaced at runtime even before your see it on screen.

Content = new StackLayout { <your other code> }

If you remove this in the code behind, you will see the XAML content at runtime.

Now to solve your problem, create all static parts of your view already in XAML. When you are satistfied with the result, go to the code behind, select the controls you need to enhance or adjust dynamically and do the adjustments on those controls.

This is the very basic approach. For more complex UIs or more sophisticated solutions let me at least name the MVVM approach with data binding for dynamic UIs. MVVM in Xamarin.Forms

更多推荐

本文发布于:2023-07-26 15:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   Forms   Xamarin   XAML   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!