形式:C#代码与XAML代码

形式:C#代码与XAML代码,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,你好。我正在创建一个简单的Xamarin.Forms(可移植)程序。我只是在想,我是否可以以某种方式“合并”或将我的XAML文件中的标签调用到我的XAML.cs。可能吗?因为每次我在XAML.cs中编写代码时,我在XAML文件中的现有代码似乎都不会出现 例如,我在SalesPage.xaml.cs中有此代码 public partial class SalesPage : ContentPage { Label resultsLabel; SearchBar searchBar

你好。我正在创建一个简单的Xamarin.Forms(可移植)程序。我只是在想,我是否可以以某种方式“合并”或将我的XAML文件中的标签调用到我的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中编写的代码?如何将它们一起显示


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

您在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>
仅举一个例子,对所有元素执行此操作

编辑

您的代码应该如下所示。别忘了修改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); 
    }
}

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

SalesLabel = "My new Label";
它将覆盖您在XAML中定义的标签。如果需要动态确定标签,这将非常方便

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

您定义的XAML文件具有隐式“内容”属性。我将把它添加到您的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属性为no“aggregation”,这意味着您只能对内容使用一个控件,而不能使用控件列表。要显示多个控件,需要一个可以像StackLayout一样保存聚合的控件

但是,由于您没有在XAML文件中指定这样的控件,而是直接覆盖代码中的Content属性,所以在运行时甚至在您在屏幕上看到它之前,它就会被替换

Content = new StackLayout
{ <your other code> }
Content=newstacklayout
{  }
如果在代码隐藏中删除此项,您将在运行时看到XAML内容

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


这是最基本的方法。对于更复杂的UI或更复杂的解决方案,请允许我至少为动态UI命名具有数据绑定的MVVM方法

你的问题是,当你运行你的应用程序时,你只看到写在你的.xaml.cs文件和.xaml文件中的代码,对吗?@M.Pipal我只看到写在我的xaml.cs文件中的代码,而不是.xaml文件中的代码,先生。你能再添加一点代码吗,在SalesPage.xaml.cs的“内容”之前?这是哪个元素的内容属性?@M.Pipal好的,先生。我添加了代码。我在使用此代码时出错:Sir var scrollview=new scrollview{Content=resultsLabel,VerticalOptions=layoutuoptions.FillAndExpand}MainLayout.Children.Add(scrollview);我应该把它放在哪里?你删除了所有以“Content=newstacklayout{…}”开头的代码,对于你的每个控件,你都要像我上面所做的那样(创建并添加到mainloayout)它是完整的代码,先生?特别是这部分?var scrollview=new scrollview{Content=resultsLabel,VerticalOptions=LayoutOptions.FillAndExpand}MainLayout.Children.Add(scrollview);对不起,我弄错了,先生。XamarinFormsDemo.dll中发生“System.NullReferenceException”类型的异常,但未在用户代码中处理其他信息:对象引用未设置为对象的实例。我无法使用“添加”术语。我犯了这个错误。没有给出与“SetterExtensions.Add(IList,BindableProperty,object)”的必需形式参数“value”对应的参数。有时候,我真的对事情的进展感到困惑。像这样的SalesLabel=“我的新标签”;我不知道在我的代码后面我会把它放在哪里。我应该按原样编码还是怎么编码。诸如此类,先生:(您可以将其放入代码背后的任何方法中。如果您在Init方法中这样做,您将在页面显示时立即看到内容。但您也可以将其放入任何事件处理程序中,并根据请求更改值(例如,按下按钮)。
Content = new StackLayout
{ <your other code> }