2016-07-15 100 views
3

美好的一天。我正在创建一个简单的Xamarin.Forms(便携式)程序。我只是想,如果我能以某种方式“合并”,或者可能将我的XAML文件中的标签调用到我的XAML.cs中。可能吗?因为每次我在XAML.cs中编写代码时,我在XAML文件中的现有代码似乎都没有出现。Xamarin.Forms:C#代码与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的一个我的代码?我怎样才能一起展示他们?

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

+1

您的问题是,当您运行您的应用程序时,您会看到只写入.xaml.cs文件和.xaml文件中的代码,对吗? –

+0

@ M.Pipal我只看到代码写在我的Xaml.cs文件中,而不是在.Xaml文件中的代码。 –

+0

您可以在SalesPage.xaml.cs的“内容”之前添加更多的代码吗?它是哪个元素的内容属性? –

回答

5

首先我给大家介绍的XAML +代码隐藏概念作品再怎么回到你的问题。 首先读取并解析XAML文件并创建UI。然后执行后面的代码,您可以在其中对UI进行一些调整。这意味着您可以通过其名称属性访问XAML的所有控件。在上面的示例中,您可以将其添加到后面的代码中:

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> 

内容属性没有“聚合”意味着你只能有一个控制的内容而不是控件列表。要显示多个控件,需要一个控件,该控件可以存储聚合,如StackLayout

但是当你没有在你的XAML文件中指定这样的控制,但在你的代码直接覆盖内容财产的背后,它能够在运行时,即使你看到它在屏幕上之前所取代。

Content = new StackLayout 
{ <your other code> } 

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

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

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

+1

'所需的形式参数“值”。有时候,我对事情的进展感到困惑。像这样SalesLabel =“我的新标签”;我不知道我的代码在哪里,我会把它放在里面。我应该按原样编写它。像这样的事情:( –

+0

)你可以在你的代码中使用任何方法,如果你在Init方法中使用它,你将在你的页面显示时马上看到内容,但是你也可以把它放到任何事件中处理程序并根据请求更改值(例如按下按钮)。 – eX0du5

1

你定义在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> 

然后你只需创建您的子代码元素在代码隐藏和添加他们到MainLayout,而不是创建新的布局。

SalesPage.xaml.cs

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

仅举一例,为您的所有元素

编辑

你的代码看起来应该是这样做到这一点。别忘了修改你的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); 
    } 
} 
+1

我使用这个代码变种爵士滚动视图=新滚动型 { 含量= resultsLabel, VerticalOptions = LayoutOptions.FillAndExpand } MainLayout.Children.Add(滚动视图)具有错误;我应该把这个放在哪里? –

+0

您从“Content = new StackLayout {....};”开始删除所有代码;“并为你的每一个控制,你会做我上面做的(创建并添加到MainLayout) –

+0

它的完整代码先生?特别是这部分? var scrollview = new ScrollView { Content = resultsLabel, VerticalOptions = LayoutOptions.FillAndExpand } MainLayout.Children.Add(scrollview); –