简体   繁体   English

Xamarin.Forms:C#代码与XAML代码

[英]Xamarin.Forms: C# code vs XAML code

Good Day. 美好的一天。 I'm creating a simple Xamarin.Forms (Portable) program. 我正在创建一个简单的Xamarin.Forms(Portable)程序。 I'm just thinking if I could somehow "MERGE" or maybe call my Label in my XAML file to my XAML.cs. 我只是在想,如果我能以某种方式“合并”或者可能将我的XAML文件中的Label调用到我的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. 因为每次我在XAML.cs中编写代码时,我在XAML文件中的现有代码似乎都没有出现。

For example, I have this code in my SalesPage.xaml.cs 例如,我在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. 屏幕上唯一出现的是我在Xaml.cs上编码的那个。

Take a look at this. 看看这个。

I have this code in my XAML file : 我在我的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>

How can I display to the screen the codes I write in XAML.CS and the one I code in XAML? 如何在屏幕上显示我在XAML.CS中编写的代码和我在XAML中编写的代码? How can I display them together? 我怎样才能一起显示它们?

Thanks a lot. 非常感谢。 Really appreciate your help. 非常感谢您的帮助。

First let me describe how the XAML + Code-Behind concept works and then go back to your issue. 首先让我描述一下XAML + Code-Behind概念是如何工作的,然后回到你的问题。 First the XAML file is read and parsed and it creates the UI. 首先读取并解析XAML文件,然后创建UI。 Then the code behind is executed where you can do some adjustments to the UI. 然后执行后面的代码,您可以在其中对UI进行一些调整。 That means you can access all controls of the XAML by its Name property. 这意味着您可以通过其Name属性访问XAML的所有控件。 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. 它会覆盖您在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. 如果要在XAML 之外动态添加内容,则必须访问现有控件并将内容添加到该控件。

The XAML file you defined has an implicit "Content" property. 您定义的XAML文件具有隐式“Content”属性。 I will add this to your XAML code (even though this would most probably return a compiler error) to illustrate this: 我将此添加到您的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>

The Content property is no "aggregation" means you can only have ONE control for the content and not a list of controls. Content属性不是“聚合”意味着您只能对内容进行一次控制而不是控件列表。 To display more than one control, one requires a control that can hold aggregations like a StackLayout . 要显示多个控件,需要一个可以保存聚合的控件,如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. 但是,由于您没有在XAML文件中指定此类控件,而是直接覆盖代码中的Content属性,因此即使在屏幕上看到它之前,它也会在运行时被替换。

Content = new StackLayout
{ <your other code> }

If you remove this in the code behind, you will see the XAML content at runtime. 如果在后面的代码中删除它,您将在运行时看到XAML内容。

Now to solve your problem, create all static parts of your view already in XAML. 现在要解决您的问题,请在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. 对于更复杂的UI或更复杂的解决方案,我至少可以使用动态UI的数据绑定来命名MVVM方法。 MVVM in Xamarin.Forms Xamarin.Forms中的MVVM

You define your layout in XAML, but then you overwrite it with your code-behind, try to change your code like this: 您在XAML中定义了布局,但随后用代码隐藏覆盖它,尝试更改代码,如下所示:

SalesPage.xaml SalesPage.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">
  <StackLayout x:Name="MainLayout">    
     <Label x:Name="SalesLabel"
           VerticalOptions="Center"
           HorizontalOptions="Center" />
  </StackLayout>
</ContentPage>

Then you just create your child elements in code-behind and Add them to MainLayout, not create new Layout. 然后,您只需在代码隐藏中创建子元素并将它们添加到MainLayout,而不是创建新的布局。

SalesPage.xaml.cs SalesPage.xaml.cs

var scrollview = new ScrollView()
   {
       Content = resultsLabel,
       VerticalOptions = LayoutOptions.FillAndExpand
   };
MainLayout.Children.Add(scrollview);

Just one example, do this for all your elements 举个例子,为你的所有元素做这个

EDIT 编辑

Your code should look like this. 您的代码应如下所示。 And don't forget to modify your XAML also, like I do above. 并且不要忘记修改你的XAML,就像我上面做的那样。

public partial class SalesPage : ContentPage
{
    Label resultsLabel;
    SearchBar searchBar;
    public SalesPage()
    {
        resultsLabel = new Label {   //...  };
        searchBar = new SearchBar {   //...  };
        var searchLabel = new Label() 
        {
            HorizontalTextAlignment = TextAlignment.Center,
            Text = "SearchBar",
            FontSize = 50,
            TextColor = Color.Purple
        };
        MainLayout.Add(searchLabel);
        MainLayout.Add(searchBar);
        MainLayout.Add(resultsLabel);
        MainLayout.Add(searchLabel);
        var scrollView = new ScrollView()
        {
            Content = resultsLabel,
            VerticalOptions = LayoutOptions.FillAndExpand
        };
        MainLayout.Add(scrollView);
        MainLayout.VerticalOptions = LayoutOptions.Start;
        MainLayout.Padding =  new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM